blob: 9a894ccd9b1012ad9185ee1f0305d553d5240d6c [file] [log] [blame]
David K. Bainbridge9189c632021-03-26 21:52:21 +00001/*
2 * Copyright 2021-present Ciena Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package commands
17
18import (
19 "fmt"
20 "os"
21
22 flags "github.com/jessevdk/go-flags"
23 configv3 "github.com/opencord/voltctl/internal/pkg/apis/config/v3"
24 "github.com/opencord/voltctl/pkg/format"
25 yaml "gopkg.in/yaml.v2"
26)
27
28const (
29 DefaultOutputFormat = "table{{.Current}}\t{{.Name}}\t{{.Server}}\t{{.KvStore}}\t{{.Kafka}}"
30)
31
32type StackUse struct {
33 Args struct {
34 Name string `positional-arg-name:"NAME" required:"yes"`
35 } `positional-args:"yes"`
36}
37
38type StackAdd struct {
39 Args struct {
40 Name string `positional-arg-name:"NAME" required:"yes"`
41 } `positional-args:"yes"`
42}
43
44type StackDelete struct {
45 Args struct {
46 Name string `positional-arg-name:"NAME" required:"yes"`
47 } `positional-args:"yes"`
48}
49
50type StackList struct {
51 ListOutputOptions
52}
53
54type StackOptions struct {
55 List StackList `command:"list" description:"list all configured stacks"`
56 Add StackAdd `command:"add" description:"add or update the named stack using command line options"`
57 Delete StackDelete `command:"delete" description:"delete the specified stack configuration"`
58 Use StackUse `command:"use" description:"perist the specified stack to be used by default"`
59}
60
61func RegisterStackCommands(parent *flags.Parser) {
62 if _, err := parent.AddCommand("stack", "generate voltctl configuration", "Commands to generate voltctl configuration", &StackOptions{}); err != nil {
63 Error.Fatalf("Unexpected error while attempting to register config commands : %s", err)
64 }
65}
66
67type StackInfo struct {
68 Current string `json:"current"`
69 Name string `json:"name"`
70 Server string `json:"server"`
71 Kafka string `json:"kafka"`
72 KvStore string `json:"kvstore"`
73}
74
75func write() error {
76 w, err := os.OpenFile(GlobalOptions.Config, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644)
77 if err != nil {
78 return err
79 }
80 defer w.Close()
81 encode := yaml.NewEncoder(w)
82 if err := encode.Encode(GlobalConfig); err != nil {
83 return err
84 }
85 return nil
86}
87
88func (options *StackList) Execute(args []string) error {
89
90 ReadConfig()
91 ApplyOptionOverrides(nil)
92
93 var data []StackInfo
94 for _, stack := range GlobalConfig.Stacks {
95 s := StackInfo{
96 Name: stack.Name,
97 Server: stack.Server,
98 Kafka: stack.Kafka,
99 KvStore: stack.KvStore,
100 }
101 if stack.Name == GlobalConfig.CurrentStack {
102 s.Current = "*"
103 }
104 data = append(data, s)
105 }
106
107 outputFormat := CharReplacer.Replace(options.Format)
108 if outputFormat == "" {
109 outputFormat = GetCommandOptionWithDefault("stack-list", "format",
110 DefaultOutputFormat)
111 }
112 if options.Quiet {
113 outputFormat = "{{.Name}}"
114 }
115 orderBy := options.OrderBy
116 if orderBy == "" {
117 orderBy = GetCommandOptionWithDefault("stack-list", "order", "")
118 }
119
120 result := CommandResult{
121 Format: format.Format(outputFormat),
122 Filter: options.Filter,
123 OrderBy: orderBy,
124 OutputAs: toOutputType(options.OutputAs),
125 NameLimit: options.NameLimit,
126 Data: data,
127 }
128
129 GenerateOutput(&result)
130 return nil
131}
132
133func (options *StackUse) Execute(args []string) error {
134
135 ReadConfig()
136
137 for _, stack := range GlobalConfig.Stacks {
138 if stack.Name == options.Args.Name {
139 GlobalConfig.CurrentStack = stack.Name
140 ApplyOptionOverrides(stack)
141 if err := write(); err != nil {
142 Error.Fatal(err.Error())
143 }
144 fmt.Printf("wrote: '%s'\n", GlobalOptions.Config)
145 return nil
146 }
147 }
148
149 Error.Fatalf("unknown stack: '%s'", options.Args.Name)
150
151 return nil
152}
153
154func (options *StackDelete) Execute(args []string) error {
155
156 ReadConfig()
157 ApplyOptionOverrides(nil)
158
159 for i, stack := range GlobalConfig.Stacks {
160 if stack.Name == options.Args.Name {
161 GlobalConfig.Stacks = append(GlobalConfig.Stacks[:i], GlobalConfig.Stacks[i+1:]...)
162 if GlobalConfig.CurrentStack == stack.Name {
163 GlobalConfig.CurrentStack = ""
164 }
165 if err := write(); err != nil {
166 Error.Fatal(err.Error())
167 }
168 fmt.Printf("wrote: '%s'\n", GlobalOptions.Config)
169 return nil
170 }
171 }
172
173 Error.Fatalf("stack not found, '%s'", options.Args.Name)
174 return nil
175}
176
177func (options *StackAdd) Execute(args []string) error {
178
179 ReadConfig()
180 stack := GlobalConfig.StackByName(options.Args.Name)
181
182 if stack == nil {
183 stack = configv3.NewDefaultStack(options.Args.Name)
184 GlobalConfig.Stacks = append(GlobalConfig.Stacks, stack)
185 }
186 if GlobalConfig.CurrentStack == "" {
187 GlobalConfig.CurrentStack = options.Args.Name
188 }
189 ApplyOptionOverrides(stack)
190 if err := write(); err != nil {
191 Error.Fatal(err.Error())
192 }
193 return nil
194}