-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand.go
More file actions
151 lines (120 loc) · 4.19 KB
/
command.go
File metadata and controls
151 lines (120 loc) · 4.19 KB
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
package main
import (
"fmt"
"log"
"os"
)
func runCommand(trekOptions trekOptions) error {
trekState := new(trekStateType)
trekState.nomadConnectConfiguration.addEnvironment("default", trekOptions.nomadAddress)
trekState.selectedClusterIndex = 0
if err := trekState.Connect(); err != nil {
log.Panicln(err)
return err
}
switch trekOptions.trekMode {
case ListJobsMode:
if trekOptions.displayFormat == "" {
trekOptions.displayFormat = jobsListFormat
}
provider := jobsFormatProvider{
Jobs: buildJobs(trekState.Jobs()),
}
trekPrintDetails(os.Stdout, trekOptions.displayFormat, provider)
case JobMode:
for index, job := range trekState.Jobs() {
if *job.Name == trekOptions.jobID {
trekState.selectedJob = index
}
}
if trekOptions.taskGroup == "" {
if trekOptions.displayFormat == "" {
trekOptions.displayFormat = taskGroupsListFormat
}
provider := jobFormatProvider{
TaskGroups: buildTaskGroups(trekState.CurrentTaskGroups()),
}
trekPrintDetails(os.Stdout, trekOptions.displayFormat, provider)
} else {
// Find task group provided by the user
trekState.selectedAllocationGroup = -1
for index, tg := range trekState.CurrentTaskGroups() {
if *tg.Name == trekOptions.taskGroup {
trekState.selectedAllocationGroup = index
}
}
if trekState.selectedAllocationGroup < 0 || trekState.selectedAllocationGroup > len(trekState.CurrentTaskGroups())-1 {
// No such task group found, display available ones
fmt.Printf("Unknown task group. Available task groups:\n")
for _, tg := range trekState.CurrentTaskGroups() {
fmt.Printf("* %s\n", *tg.Name)
}
} else {
// No allocation provided by the user, display all of them
if trekOptions.allocationIndex == -1 {
if trekOptions.displayFormat == "" {
trekOptions.displayFormat = allocationsFormat
}
provider := taskGroupFormatProvider{
Allocations: buildAllocations(trekState.CurrentAllocations()),
}
trekPrintDetails(os.Stdout, trekOptions.displayFormat, provider)
} else {
trekState.selectedAllocationIndex = trekOptions.allocationIndex
if trekOptions.allocationIndex < 0 || trekOptions.allocationIndex > len(trekState.CurrentAllocations())-1 {
// out of bounds, show existing ones
fmt.Printf("Allocation index %d out-of-bounds. Valid indices:\n", trekOptions.allocationIndex)
for index, alloc := range trekState.CurrentAllocations() {
fmt.Printf("(%d) %s\n", index, alloc.Name)
}
} else {
// Allocation found, no task provided by user
if trekOptions.taskName == "" {
if trekOptions.displayFormat == "" {
trekOptions.displayFormat = allocationDetailsFormat
}
alloc, _ := trekState.CurrentAllocation()
provider := allocationFormatProvider{
IP: alloc.IP(),
Tasks: buildTasks(trekState.Tasks()),
}
trekPrintDetails(os.Stdout, trekOptions.displayFormat, provider)
} else {
// Find task
trekState.selectedTask = -1
for index, task := range trekState.Tasks() {
if task.Name == trekOptions.taskName {
trekState.selectedTask = index
}
}
// No task? Show all of them
if trekState.selectedTask == -1 {
fmt.Printf("Task %s not found. Available tasks:\n", trekOptions.taskName)
for _, task := range trekState.Tasks() {
fmt.Printf("* %s\n", task.Name)
}
} else {
if trekOptions.displayFormat == "" {
trekOptions.displayFormat = taskDetailsFormat
}
alloc, _ := trekState.CurrentAllocation()
task := trekState.CurrentTask()
provider := taskFormatProvider{
Task: trekTask{Name: task.Name, Driver: task.Driver, Config: task.Config},
Node: trekNode{Name: alloc.node.Name, IP: alloc.IP()},
Network: buildNetwork(alloc.allocation.TaskResources[task.Name].Networks),
Environment: buildEnv(task.Env),
}
trekPrintDetails(os.Stdout, trekOptions.displayFormat, provider)
}
}
}
}
}
}
default:
fmt.Printf("Unknown mode: %s. Exiting.\n", trekOptions.trekMode)
os.Exit(1)
}
return nil
}