This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
context.go
195 lines (174 loc) · 5.48 KB
/
context.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package cmd
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/vmware/dispatch/pkg/dispatchcli/i18n"
)
var (
contextLong = i18n.T(`Manage configuration context.`)
contextExample = i18n.T(`
# list all contexts:
$ dispatch context`)
currentContextLong = i18n.T(`Show current configuration context.`)
setContextLong = i18n.T(`Set configuration context.`)
setContextExample = i18n.T(`
# set current context to localhost:
$ dispatch context set localhost
Set context to localhost`)
deleteContextLong = i18n.T(`Delete configuration context.`)
deleteContextExample = i18n.T(`
# delete context localhost:
$ dispatch context delete localhost
Deleted context localhost`)
)
// NewCmdContext handles configuration context operations
func NewCmdContext(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "context",
Short: i18n.T("Manage context configuration"),
Long: contextLong,
Example: contextExample,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return formatContextOutput(out)
},
}
cmd.AddCommand(NewCmdContextCurrent(out, errOut))
cmd.AddCommand(NewCmdContextSet(out, errOut))
cmd.AddCommand(NewCmdContextDelete(out, errOut))
return cmd
}
func setContext(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
currentContext := args[0]
if _, ok := cmdConfig.Contexts[currentContext]; !ok {
return errors.Errorf("No such context %s", currentContext)
}
cmdConfig.Current = currentContext
b, _ := json.MarshalIndent(cmdConfig, "", " ")
path := viper.ConfigFileUsed()
err := ioutil.WriteFile(path, b, 0644)
if err != nil {
return errors.Errorf("Failed to write config file %s", path)
}
fmt.Fprintf(out, "Set context to %s\n", currentContext)
return nil
}
func deleteContext(out, errOut io.Writer, cmd *cobra.Command, args []string) error {
ctxName := args[0]
if _, ok := cmdConfig.Contexts[ctxName]; !ok {
return errors.Errorf("No such context %s", ctxName)
}
delete(cmdConfig.Contexts, ctxName)
b, _ := json.MarshalIndent(cmdConfig, "", " ")
path := viper.ConfigFileUsed()
err := ioutil.WriteFile(path, b, 0644)
if err != nil {
return errors.Errorf("Failed to write config file %s", path)
}
fmt.Fprintf(out, "Deleted context %s\n", ctxName)
return nil
}
func formatContextOutput(out io.Writer) error {
if w, err := formatOutput(out, false, cmdConfig); w {
return err
}
headers := []string{"Context", "Config"}
table := tablewriter.NewWriter(out)
table.SetHeader(headers)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetCenterSeparator("")
table.SetAutoWrapText(false)
for context, config := range cmdConfig.Contexts {
// Remove the cookie from output
config.Cookie = ""
configContent, _ := json.MarshalIndent(config, "", " ")
if context == cmdConfig.Current {
context = fmt.Sprintf("* %s", context)
}
row := []string{context, string(configContent)}
table.Append(row)
}
table.Render()
return nil
}
func formatCurrentContextOutput(out io.Writer) error {
if w, err := formatOutput(out, false, cmdConfig); w {
return err
}
context := cmdConfig.Current
if config, ok := cmdConfig.Contexts[context]; ok {
headers := []string{"Context", "Config"}
table := tablewriter.NewWriter(out)
table.SetHeader(headers)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetCenterSeparator("")
table.SetAutoWrapText(false)
// Remove the cookie from output
config.Cookie = ""
configContent, _ := json.MarshalIndent(config, "", " ")
context = fmt.Sprintf("* %s", context)
row := []string{context, string(configContent)}
table.Append(row)
table.Render()
} else {
fmt.Fprintf(out, "No such current context was found %s\n", context)
}
return nil
}
func formatContextName(name string) string {
return strings.ToLower(strings.Replace(name, ".", "-", -1))
}
// NewCmdContextCurrent show current context
func NewCmdContextCurrent(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "current",
Short: i18n.T("Show current context"),
Long: currentContextLong,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return formatCurrentContextOutput(out)
},
}
return cmd
}
// NewCmdContextSet handles set context operations
func NewCmdContextSet(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "set CONTEXT",
Short: i18n.T("Set context"),
Long: setContextLong,
Example: setContextExample,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := setContext(out, errOut, cmd, args)
CheckErr(err)
},
}
return cmd
}
// NewCmdContextDelete handles delete context operations
func NewCmdContextDelete(out io.Writer, errOut io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "delete CONTEXT",
Short: i18n.T("Delete context"),
Long: deleteContextLong,
Example: deleteContextExample,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
err := deleteContext(out, errOut, cmd, args)
CheckErr(err)
},
}
return cmd
}