/
init.go
69 lines (56 loc) · 1.73 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
package commands
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/codegangsta/cli"
)
var InitCMD = cli.Command{
Name: "init",
Usage: "create a " + defaultTenetCfgPath + " config file in the current directory",
Action: initLingo,
}
var configSeed = `
# This config file is in TOML format. Manually add and configure tenets
# following these guidelines: http://lingo.reviews/getting-started#the-lingo-
# file. Alternatively, use the add/remove commands on the lingo app.
`[1:]
// TODO(waigani) set lingo-home flag and test init creates correct home dir.
func initLingo(c *cli.Context) {
if err := maxArgs(c, 1); err != nil {
oserrf(err.Error())
return
}
// Create a new tenet config file at either the provided directory or
// a location from flags or environment, or the current directory
cfgPath, _ := filepath.Abs(desiredTenetCfgPath(c))
if len(c.Args()) > 0 {
cfgPath, _ = filepath.Abs(c.Args()[0])
// Check that it exists and is a directory
if pathInfo, err := os.Stat(cfgPath); os.IsNotExist(err) {
oserrf("directory %q not found", cfgPath)
} else if !pathInfo.IsDir() {
oserrf("%q is not a directory", cfgPath)
}
// Use default config filename
cfgPath = filepath.Join(cfgPath, defaultTenetCfgPath)
}
if _, err := os.Stat(cfgPath); err == nil {
oserrf("Already initialised using tenet config file %q", cfgPath)
}
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
err := enc.Encode(&config{Include: "*", Cascade: true})
if err != nil {
oserrf(err.Error())
return
}
if err = ioutil.WriteFile(cfgPath, buf.Bytes(), 0644); err != nil {
oserrf(err.Error())
return
}
fmt.Printf("Successfully initialised. Lingo config file written to %q\n", cfgPath)
}