-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
78 lines (65 loc) · 2.06 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"io"
"log"
"os"
"path/filepath"
"gopkg.in/alecthomas/kingpin.v2"
)
type InitCommand struct {
appPath string // Path to where the app will be placed after the scaffold has taken place
}
// Configures the command line on how to deal with the init command
func configureInitCommand(app *kingpin.Application) {
cmd := &InitCommand{}
appCmd := app.Command("init", "Scaffold a new application into the specified folder").
Action(cmd.init).
Alias("i")
appCmd.Arg("appPath", "Path to an empty folder. (default: current folder)").
Default(".").
ExistingDirVar(&cmd.appPath)
}
func (cmd *InitCommand) init(context *kingpin.ParseContext) error {
// grab the absolute path
appPathRelative := cmd.appPath
appPathAbsolute, err := filepath.Abs(appPathRelative)
if err != nil {
log.Printf("Failed to obtain absolute path for %s\n%v", appPathRelative, err)
return err
}
// tell the user what we're planning on doing
log.Print("Initializing new application")
if verbose {
log.Printf("Specified appPath to be %s", appPathRelative)
log.Printf("Absolute appPath is %s", appPathAbsolute)
}
// First we'll check to see if the directory is empty. It's purely for safety purposes, to ensure we don't overwrite
// anyting special. The command line handling has already validated that the folder actually exists
isEmptyPath, err := isEmptyAppPath(appPathAbsolute)
if !isEmptyPath || err != nil {
log.Printf("The specified appPath '%s' does not appear to be an empty directory\n%v", appPathRelative, err)
return err
}
// Scaffold
err = scaffoldNewApp(appPathAbsolute)
if err != nil {
return err
}
log.Print("All done!")
return nil
}
func isEmptyAppPath(appPath string) (bool, error) {
// See http://stackoverflow.com/questions/30697324/how-to-check-if-directory-on-path-is-empty
// Open the directory, which must not fail
f, err := os.Open(appPath)
if err != nil {
return false, err
}
defer f.Close()
// See if there's anything in the directory at all
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}