This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
112 lines (93 loc) · 2.32 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"os"
"github.com/segmentio/robo/cli"
"github.com/segmentio/robo/config"
"github.com/tj/docopt"
analytics "gopkg.in/segmentio/analytics-go.v3"
)
// Set by linker flags
var version = "dev"
const analyticsWriteKey = "JX2IUwzrdfKoGilkImNmjRdjNFPmSMfe"
var (
username string
analyticsClient analytics.Client
)
const usage = `
Usage:
robo [--config file]
robo [--xtrace] <task> [<arg>...] [--config file]
robo help [<task>] [--config file]
robo variables [--config file]
robo -h | --help
robo --version
Options:
-c, --config file config file to load
-x, --xtrace set xtrace when running scripts or commands
-h, --help output help information
-v, --version output version
Examples:
output tasks
$ robo
output task help
$ robo help mytask
`
func main() {
args, err := docopt.Parse(usage, nil, true, version, true)
if err != nil {
cli.Fatalf("error parsing arguments: %s", err)
}
analyticsClient, _ = analytics.NewWithConfig(analyticsWriteKey, analytics.Config{
BatchSize: 1,
})
username = os.Getenv("USER")
analyticsClient.Enqueue(analytics.Identify{
UserId: username,
Traits: analytics.NewTraits().
Set("robo-version", version),
})
c := args["--config"]
if c == nil {
c = os.Getenv("ROBO_CONFIG")
if c == nil || c == "" {
cli.Fatalf("robo requires a config file passed via --config or the ROBO_CONFIG env var")
}
}
file := c.(string)
conf, err := config.New(file)
if err != nil {
cli.Fatalf("error loading configuration: %s", err)
}
switch {
case args["help"].(bool):
if name, ok := args["<task>"].(string); ok {
cli.Help(conf, name)
} else {
cli.List(conf)
}
case args["variables"].(bool):
cli.ListVariables(conf)
default:
if name, ok := args["<task>"].(string); ok {
arguments := args["<arg>"].([]string)
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("robo-version", version).
Set("config", file).
Set("task", name).
Set("args", arguments),
})
xtrace, ok := args["--xtrace"].(bool)
if xtrace && ok {
cli.RunTrace(conf, name, arguments)
} else {
cli.Run(conf, name, arguments)
}
} else {
cli.List(conf)
}
}
analyticsClient.Close()
}