-
Notifications
You must be signed in to change notification settings - Fork 0
/
crusher.go
192 lines (175 loc) · 4.99 KB
/
crusher.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"os"
"github.com/murdinc/cli"
"github.com/murdinc/crusher/config"
"github.com/murdinc/crusher/specr"
"github.com/murdinc/terminal"
)
// Main Function
////////////////..........
func main() {
var class string
var sequence string
var locale string
app := cli.NewApp()
app.Name = "crusher"
app.Usage = "Command Line Configuration Management System"
app.Version = "1.2.0"
app.Author = "Ahmad Abdo"
app.Email = "[email protected]"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "list-servers",
ShortName: "l",
Usage: "crusher list-servers",
Description: "List all configured remote servers",
Action: func(c *cli.Context) error {
cfg := getConfig()
terminal.Information(fmt.Sprintf("There are [%d] remote servers configured currently", len(cfg.Servers)))
cfg.Servers.PrintAllServerInfo()
return nil
},
},
{
Name: "remote-configure",
ShortName: "rc",
Usage: "crusher remote-configure hello_world",
Description: "Configure one or many remote servers",
Arguments: []cli.Argument{
cli.Argument{Name: "search", Description: "The server or spec group to remote configure", Optional: false},
},
Action: func(c *cli.Context) error {
specList, err := specr.GetSpecs()
if err != nil {
terminal.ShowErrorMessage("Error Reading Spec Files!", err.Error())
return err
}
cfg := getConfig()
cfg.Servers.RemoteConfigure(c.NamedArg("search"), specList)
return nil
},
},
{
Name: "local-configure",
ShortName: "lc",
Usage: "crusher local-configure hello_world",
Description: "Configure this local machine with a given spec",
Arguments: []cli.Argument{
cli.Argument{Name: "spec", Description: "The spec to configure on this machine", Optional: false},
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "class",
Destination: &class,
Usage: "server class",
},
cli.StringFlag{
Name: "sequence",
Destination: &sequence,
Usage: "server sequence",
},
cli.StringFlag{
Name: "locale",
Destination: &locale,
Usage: "server location",
},
},
Action: func(c *cli.Context) error {
specList, err := specr.GetSpecs()
if err != nil {
terminal.ShowErrorMessage("Error Reading Spec Files!", err.Error())
return err
}
specName := c.NamedArg("spec")
if !specList.SpecExists(specName) {
terminal.ShowErrorMessage("Unable to find Spec!", fmt.Sprintf("I was unable to find a spec named [%s].", specName))
return nil
}
specList.LocalConfigure(specName, c.String("class"), c.String("sequence"), c.String("locale"))
return nil
},
},
{
Name: "add-server",
ShortName: "a",
Usage: "crusher add-server",
Description: "Add a new remote server to the config",
Action: func(c *cli.Context) error {
cfg := getConfig()
return cfg.AddServer()
},
},
{
Name: "delete-server",
ShortName: "d",
Usage: "crusher delete-server",
Description: "Delete a remote server from the config",
Action: func(c *cli.Context) error {
cfg := getConfig()
return cfg.DeleteServer()
},
},
{
Name: "available-specs",
ShortName: "s",
Usage: "crusher available-specs",
Description: "List all available specs",
Action: func(c *cli.Context) error {
specList, err := specr.GetSpecs()
if err != nil {
terminal.ShowErrorMessage("Error Reading Spec Files!", err.Error())
return err
}
terminal.Information(fmt.Sprintf("There are [%d] specs available currently", len(specList.Specs)))
specList.PrintSpecInformation()
return nil
},
},
{
Name: "show-spec",
ShortName: "ss",
Usage: "crusher show-spec",
Description: "Show what a given spec will build",
Arguments: []cli.Argument{
cli.Argument{Name: "spec", Description: "The spec to show", Optional: false},
},
Action: func(c *cli.Context) error {
specList, err := specr.GetSpecs()
if err != nil {
terminal.ShowErrorMessage("Error Reading Spec Files!", err.Error())
}
specName := c.NamedArg("spec")
if !specList.SpecExists(specName) {
terminal.ShowErrorMessage("Unable to find Spec!", fmt.Sprintf("I was unable to find a spec named [%s].", specName))
return nil
}
specList.ShowSpecBuild(specName)
return nil
},
},
}
app.Run(os.Args)
}
func getConfig() *config.CrusherConfig {
// Check Config
cfg, err := config.ReadConfig()
if err != nil || len(cfg.Servers) == 0 {
// No Config Found, ask if we want to create one
create := terminal.BoxPromptBool("Crusher configuration file not found or empty!", "Do you want to add some servers now?")
if !create {
terminal.Information("Alright then, maybe next time.. ")
os.Exit(0)
return nil
}
// Add Some Servers to our config
cfg.AddServer()
os.Exit(0)
return nil
}
return cfg
}
//
////////////////..........