-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
135 lines (106 loc) · 2.95 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package tachyon
import (
"fmt"
"github.com/jessevdk/go-flags"
"os"
"path/filepath"
)
type Options struct {
Vars map[string]string `short:"s" long:"set" description:"Set a variable"`
ShowOutput bool `short:"o" long:"output" description:"Show command output"`
Host string `short:"t" long:"host" description:"Run the playbook on another host"`
Development bool `long:"dev" description:"Use a dev version of tachyon"`
CleanHost bool `long:"clean-host" description:"Clean the host cache before using"`
Debug bool `short:"d" long:"debug" description:"Show all information about commands"`
Release string `long:"release" description:"The release to use when remotely invoking tachyon"`
JSON bool `long:"json" description:"Output the run details in chunked json"`
Install bool `long:"install" description:"Install tachyon a remote machine"`
}
var Release string = "dev"
var Arg0 string
func Main(args []string) int {
var opts Options
abs, err := filepath.Abs(args[0])
if err != nil {
panic(err)
}
Arg0 = abs
parser := flags.NewParser(&opts, flags.Default)
for _, o := range parser.Command.Group.Groups()[0].Options() {
if o.LongName == "release" {
o.Default = []string{Release}
}
}
args, err = parser.ParseArgs(args)
if err != nil {
if serr, ok := err.(*flags.Error); ok {
if serr.Type == flags.ErrHelp {
return 2
}
}
fmt.Printf("Error parsing options: %s", err)
return 1
}
if !opts.Install && len(args) != 2 {
fmt.Printf("Usage: tachyon [options] <playbook>\n")
return 1
}
if opts.Host != "" {
return runOnHost(&opts, args)
}
cfg := &Config{ShowCommandOutput: opts.ShowOutput}
ns := NewNestedScope(nil)
for k, v := range opts.Vars {
ns.Set(k, v)
}
env := NewEnv(ns, cfg)
defer env.Cleanup()
if opts.JSON {
env.ReportJSON()
}
playbook, err := NewPlaybook(env, args[1])
if err != nil {
fmt.Printf("Error loading plays: %s\n", err)
return 1
}
cur, err := os.Getwd()
if err != nil {
fmt.Printf("Unable to figure out the current directory: %s\n", err)
return 1
}
defer os.Chdir(cur)
os.Chdir(playbook.baseDir)
runner := NewRunner(env, playbook.Plays)
err = runner.Run(env)
if err != nil {
fmt.Fprintf(os.Stderr, "Error running playbook: %s\n", err)
return 1
}
return 0
}
func runOnHost(opts *Options, args []string) int {
if opts.Install {
fmt.Printf("=== Installing tachyon on %s\n", opts.Host)
} else {
fmt.Printf("=== Executing playbook on %s\n", opts.Host)
}
var playbook string
if !opts.Install {
playbook = args[1]
}
t := &Tachyon{
Target: opts.Host,
Debug: opts.Debug,
Clean: opts.CleanHost,
Dev: opts.Development,
Playbook: playbook,
Release: opts.Release,
InstallOnly: opts.Install,
}
_, err := RunAdhocCommand(t, "")
if err != nil {
fmt.Printf("Error: %s\n", err)
return 1
}
return 0
}