-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
136 lines (110 loc) · 3.81 KB
/
cli.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
package main
import (
"os"
"github.com/james-nesbitt/coach/conf"
"github.com/james-nesbitt/coach/log"
"github.com/james-nesbitt/coach/libs"
"github.com/james-nesbitt/coach/operation"
)
var (
operationName string
mainTargets []string
globalFlags map[string]string
operationFlags []string
environment string
logger log.Log // Logger interface for tracking messages
project *conf.Project // project configuration
)
func init() {
operationName, mainTargets, globalFlags, operationFlags, environment = parseGlobalFlags(os.Args)
// verbosity
var verbosity int = log.VERBOSITY_MESSAGE
if globalFlags["verbosity"] != "" {
switch globalFlags["verbosity"] {
case "message":
verbosity = log.VERBOSITY_MESSAGE
case "info":
verbosity = log.VERBOSITY_INFO
case "warning":
verbosity = log.VERBOSITY_WARNING
case "verbose":
verbosity = log.VERBOSITY_DEBUG_LOTS
case "debug":
verbosity = log.VERBOSITY_DEBUG_WOAH
case "staaap":
verbosity = log.VERBOSITY_DEBUG_STAAAP
}
}
logger = log.MakeCliLog("coach-cli", os.Stdout, verbosity)
logger.Debug(log.VERBOSITY_DEBUG, "Reporting initialization", logger.Verbosity())
workingDir, _ := os.Getwd()
logger.Debug(log.VERBOSITY_DEBUG, "Working Directory", workingDir)
project = conf.MakeCoachProject(logger.MakeChild("conf"), workingDir, environment)
logger.Debug(log.VERBOSITY_DEBUG, "Project configuration", *project)
logger.Debug(log.VERBOSITY_DEBUG, "Finished initialization", nil)
}
func main() {
if !(operationName == "init" || operationName == "init-generate" || operationName == "help" || project.IsValid(logger.MakeChild("Sanity Check"))) {
logger.Error("Coach project configuration is not processable. Execution halted. [" + operationName + "]")
return
}
logger.Debug(log.VERBOSITY_DEBUG, "Starting CLI Processing", nil)
logger.Debug(log.VERBOSITY_DEBUG, "Creating client factories", nil)
// get a list of client factories that we can use for nodes
clientFactories := libs.MakeClientFactories(logger.MakeChild("client-factories"), project)
logger.Debug(log.VERBOSITY_DEBUG, "Factories", *clientFactories)
// Build our list of nodes
nodes := libs.MakeNodes(logger.MakeChild("nodes"), project, clientFactories)
logger.Debug(log.VERBOSITY_DEBUG, "Nodes", *nodes)
/**
* prepare the whole node set
*
* this means that the nodes building is completed, and now any
* dependencies and settings should already be included. The prepare
* should now connect pieces together down the chain.
*/
nodes.Prepare(logger.MakeChild("nodes"))
/**
* convert the target string list from the arguments into a target set
*/
targets := nodes.Targets(logger.MakeChild("targets"), mainTargets)
logger.Debug(log.VERBOSITY_DEBUG, "Sorted Targets", mainTargets, targets)
/**
* Maybe we can come up with a better default operation?
*
* Various scenarios:
*
* A. all nodes are of the same type, then pick a decent default operation per node type.
*
*/
if operationName == operation.DEFAULT_OPERATION {
// Check target node types
targetType := ""
for _, targetId := range targets.TargetOrder() {
target, _ := targets.Target(targetId)
node, _ := target.Node()
if targetType == "" {
targetType = node.Type()
} else if targetType == node.Type() {
} else {
targetType = ""
break
}
}
switch targetType {
case "command":
// Command containers default to run
operationName = "run"
default:
// default to info
operationName = "info"
}
}
/**
* Create an operation set
*/
operations := operation.MakeOperation(logger.MakeChild("operations"), project, operationName, operationFlags, targets)
logger.Debug(log.VERBOSITY_DEBUG, "OPERATION:", operationName, operationFlags, operations)
operations.Run(logger.MakeChild("operation"))
logger.Debug(log.VERBOSITY_DEBUG, "Finished CLI Processing", nil)
}