/
list.go
65 lines (56 loc) · 1.32 KB
/
list.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
package commands
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/codegangsta/cli"
"github.com/lingo-reviews/lingo/util"
)
var ListCMD = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "list tenets",
Description: "Lists all tenets added to .lingo run.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "installed",
Usage: "list all tenets installed on this machine",
},
},
Action: listAction,
}
func listAction(c *cli.Context) {
// TODO(waigani) DEMOWARE
if c.Bool("installed") {
var stdout bytes.Buffer
cmd := exec.Command("tree", filepath.Join(util.MustLingoHome(), "tenets"))
cmd.Stdout = &stdout
// cmd.Stderr = &stderr
cmd.Run()
fmt.Print(string(stdout.Bytes()))
return
}
for _, t := range listTenets(c) {
fmt.Printf("%s (%s)\n", t.Name, t.Driver)
}
}
func listTenets(c *cli.Context) []TenetConfig {
cfgPath, err := tenetCfgPath(c)
if err != nil {
if _, ok := err.(*os.PathError); ok {
// For missing config files, there are just no tenets
return nil
}
// For malformed config show the error to the user
oserrf("could not read configuration: %s", err.Error())
return nil
}
cfg, err := buildConfig(cfgPath, CascadeNone)
if err != nil {
oserrf("could not read configuration: %s", err.Error())
return nil
}
return cfg.AllTenets()
}