forked from pkallberg/cx
/
stack-configure.go
202 lines (168 loc) · 4.48 KB
/
stack-configure.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
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/cloud66/cloud66"
"github.com/cloud66/cx/term"
"github.com/cloud66/cli"
)
type ConfigureFile struct {
Name string
ListFunc func(*cli.Context, string)
DownloadFunc func(*cli.Context, string)
UploadFunc func(*cli.Context, string)
}
func mustFile(c *cli.Context) ConfigureFile {
var file ConfigureFile
switch c.String("file") {
case "service.yml":
file = ConfigureFile{
Name: c.String("file"),
ListFunc: runServiceYamlList,
DownloadFunc: runDownloadServiceYaml,
UploadFunc: runUploadServiceYaml}
case "manifest.yml":
file = ConfigureFile{
Name: c.String("file"),
ListFunc: runManifestYamlList,
DownloadFunc: runDownloadManifestYaml,
UploadFunc: runUploadManifestYaml}
default:
printFatal("No file type specified or file type is not supported. Use --file flag to choose file type. supported values are: service.yml , manifest.yml")
}
return file
}
func runStackConfigureFileList(c *cli.Context) {
stack := mustStack(c)
file := mustFile(c)
file.ListFunc(c, stack.Uid)
}
func runStackConfigureDownloadFile(c *cli.Context) {
stack := mustStack(c)
file := mustFile(c)
file.DownloadFunc(c, stack.Uid)
}
func runStackConfigureUploadFile(c *cli.Context) {
stack := mustStack(c)
file := mustFile(c)
file.UploadFunc(c, stack.Uid)
}
// --
func runServiceYamlList(c *cli.Context, stackUid string) {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
serviceYamls, err := client.ServiceYamlList(stackUid, false)
must(err)
printServiceYamlList(w, serviceYamls)
}
func printServiceYamlList(w io.Writer, serviceYamls []cloud66.ServiceYaml) {
for _, a := range serviceYamls {
if a.Uid != "" {
listRec(w,
a.Uid,
a.Comments,
prettyTime{a.CreatedAt},
)
}
}
}
func runManifestYamlList(c *cli.Context, stackUid string) {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)
defer w.Flush()
manifestYamls, err := client.ManifestYamlList(stackUid, false)
must(err)
printManifestYamlList(w, manifestYamls)
}
func printManifestYamlList(w io.Writer, manifestYamls []cloud66.ManifestYaml) {
for _, a := range manifestYamls {
if a.Uid != "" {
listRec(w,
a.Uid,
a.Comments,
prettyTime{a.CreatedAt},
)
}
}
}
func runDownloadServiceYaml(c *cli.Context, stackUid string) {
version := c.String("version")
if version == "" {
version = "latest"
}
serviceYaml, err := client.ServiceYamlInfo(stackUid, version)
must(err)
output := c.String("output")
if output != "" {
err := writeFile(output, serviceYaml.Body)
must(err)
} else {
fmt.Println(serviceYaml.Body)
}
}
func runDownloadManifestYaml(c *cli.Context, stackUid string) {
version := c.String("version")
if version == "" {
version = "latest"
}
manifestYaml, err := client.ManifestYamlInfo(stackUid, version)
must(err)
output := c.String("output")
if output != "" {
err := writeFile(output, manifestYaml.Body)
must(err)
} else {
fmt.Println(manifestYaml.Body)
}
}
func runUploadServiceYaml(c *cli.Context, stackUid string) {
serviceYamlFile := c.Args().First()
if serviceYamlFile == "" {
printFatal("service_yaml file path is required")
} else {
serviceYamlFile = expandPath(serviceYamlFile)
}
serviceYamlBytes, err := ioutil.ReadFile(serviceYamlFile)
must(err)
serviceYaml := string(serviceYamlBytes)
comments := c.String("comments")
if comments == "" {
fmt.Println("\nComments can't be blank, Please add one:")
if term.IsTerminal(os.Stdin) {
fmt.Printf("> ")
}
reader := bufio.NewReader(os.Stdin)
if comments, err = reader.ReadString('\n'); err != nil {
printFatal(err.Error())
}
}
_, err = client.CreateServiceYaml(stackUid, serviceYaml, comments)
must(err)
}
func runUploadManifestYaml(c *cli.Context, stackUid string) {
manifestYamlFile := c.Args().First()
if manifestYamlFile == "" {
printFatal("manifest_yaml file path is required")
} else {
manifestYamlFile = expandPath(manifestYamlFile)
}
manifestYamlBytes, err := ioutil.ReadFile(manifestYamlFile)
must(err)
manifestYaml := string(manifestYamlBytes)
comments := c.String("comments")
if comments == "" {
fmt.Println("\nComments can't be blank, Please add one:")
if term.IsTerminal(os.Stdin) {
fmt.Printf("> ")
}
reader := bufio.NewReader(os.Stdin)
if comments, err = reader.ReadString('\n'); err != nil {
printFatal(err.Error())
}
}
_, err = client.CreateManifestYaml(stackUid, manifestYaml, comments)
must(err)
}