/
init.go
51 lines (47 loc) · 1.13 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
package command
import (
"fmt"
"os"
"github.com/codegangsta/cli"
comp "github.com/gen0cide/laforge/competition"
input "github.com/tcnksm/go-input"
)
func CmdInit(c *cli.Context) {
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
query := "What directory would you like to set LF_HOME to?"
newHome, err := ui.Ask(query, &input.Options{
// Read the default val from env var
Required: true,
Loop: true,
})
if err != nil {
comp.LogFatal("Fatal Error: " + err.Error())
}
comp.SetHome(newHome)
if !comp.HomeExists() {
query = "LF_HOME doesn't exist. Create it? [Y/n]"
createDir, err := ui.Ask(query, &input.Options{
Required: true,
Loop: true,
ValidateFunc: func(s string) error {
if s != "Y" && s != "n" {
return fmt.Errorf("input must be Y or n")
}
return nil
},
})
if err != nil {
comp.LogFatal("Fatal Error: " + err.Error())
}
if createDir == "Y" {
comp.Log("Creating LF_HOME...")
comp.CreateHome()
comp.Log("Created! Be sure to edit config/config.yml")
}
}
comp.ValidateHome()
comp.Log("LF_HOME has been defined. Laforge is now chrooted into " + newHome)
}