Skip to content

Commit 0eafd17

Browse files
authored
Merge pull request #27 from liquidweb/override-auth-context
add a global --use-context flag
2 parents f93b37a + 1fda9aa commit 0eafd17

File tree

4 files changed

+100
-58
lines changed

4 files changed

+100
-58
lines changed

cmd/authUseContext.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/spf13/cobra"
2323

2424
"github.com/liquidweb/liquidweb-cli/instance"
25-
"github.com/liquidweb/liquidweb-cli/types/cmd"
2625
)
2726

2827
var authUseContextCmd = &cobra.Command{
@@ -46,22 +45,8 @@ If you've never setup any contexts, check "auth init".`,
4645
wantedContext := args[0]
4746

4847
// verify wantedContext is valid
49-
isValid := false
50-
contexts := lwCliInst.Viper.GetStringMap("liquidweb.api.contexts")
51-
for _, contextInter := range contexts {
52-
var context cmdTypes.AuthContext
53-
if err := instance.CastFieldTypes(contextInter, &context); err != nil {
54-
lwCliInst.Die(err)
55-
}
56-
57-
if context.ContextName == wantedContext {
58-
isValid = true
59-
break
60-
}
61-
}
62-
63-
if !isValid {
64-
lwCliInst.Die(fmt.Errorf("given context [%s] is not a valid context", wantedContext))
48+
if err := instance.ValidateContext(wantedContext, lwCliInst.Viper); err != nil {
49+
lwCliInst.Die(err)
6550
}
6651

6752
// looks valid, set

cmd/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
var cfgFile string
3434
var lwCliInst instance.Client
35+
var useContext string
3536

3637
var rootCmd = &cobra.Command{
3738
Use: "lw",
@@ -65,6 +66,7 @@ func init() {
6566
cobra.OnInitialize(initConfig)
6667

6768
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.liquidweb-cli.yaml)")
69+
rootCmd.PersistentFlags().StringVar(&useContext, "use-context", "", "forces current context, without persisting the context change")
6870
}
6971

7072
func initConfig() {
@@ -88,6 +90,15 @@ func initConfig() {
8890
vp.AutomaticEnv()
8991
vp.ReadInConfig()
9092

93+
if useContext != "" {
94+
if err := instance.ValidateContext(useContext, vp); err != nil {
95+
utils.PrintRed("error using auth context:\n\n")
96+
fmt.Printf("%s\n\n", err)
97+
os.Exit(1)
98+
}
99+
vp.Set("liquidweb.api.current_context", useContext)
100+
}
101+
91102
var lwCliInstErr error
92103
lwCliInst, lwCliInstErr = instance.New(vp)
93104
if lwCliInstErr != nil {

instance/authContext.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright © LiquidWeb
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+
*/
16+
package instance
17+
18+
import (
19+
"bytes"
20+
"encoding/json"
21+
"fmt"
22+
23+
"github.com/spf13/viper"
24+
25+
"github.com/liquidweb/liquidweb-cli/types/cmd"
26+
)
27+
28+
func (client *Client) RemoveContext(context string) error {
29+
// review this function once Unset (or similar) in viper is available
30+
// https://github.com/spf13/viper/pull/519
31+
if context == "" {
32+
return fmt.Errorf("context cannot be empty")
33+
}
34+
35+
currentContext := client.Viper.GetString("liquidweb.api.current_context")
36+
if context == currentContext {
37+
return fmt.Errorf("cannot remove context currently set as current context")
38+
}
39+
40+
if err := ValidateContext(context, client.Viper); err != nil {
41+
return fmt.Errorf("context %s doesnt exist, cannot remove", context)
42+
}
43+
44+
// save current config into a map, then delete requested context
45+
cfgMap := client.Viper.AllSettings()
46+
delete(cfgMap["liquidweb"].(map[string]interface{})["api"].(map[string]interface{})["contexts"].(map[string]interface{}), context)
47+
48+
// json encode modified map
49+
encodedCfg, err := json.MarshalIndent(cfgMap, "", " ")
50+
if err != nil {
51+
return err
52+
}
53+
54+
// read newly encoded config back into viper
55+
if err := client.Viper.ReadConfig(bytes.NewBuffer(encodedCfg)); err != nil {
56+
return err
57+
}
58+
59+
// write the new viper configuration to file
60+
if err := client.Viper.WriteConfig(); err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
67+
func ValidateContext(wantedContext string, vp *viper.Viper) error {
68+
var isValid bool
69+
contexts := vp.GetStringMap("liquidweb.api.contexts")
70+
for _, contextInter := range contexts {
71+
var context cmdTypes.AuthContext
72+
if err := CastFieldTypes(contextInter, &context); err != nil {
73+
return err
74+
}
75+
76+
if context.ContextName == wantedContext {
77+
isValid = true
78+
break
79+
}
80+
}
81+
82+
if !isValid {
83+
return fmt.Errorf("given context [%s] is not a valid context", wantedContext)
84+
}
85+
86+
return nil
87+
}

instance/instance.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -77,47 +77,6 @@ func (*Client) JsonPrettyPrint(inJson string) (string, error) {
7777
return outJson.String(), nil
7878
}
7979

80-
func (client *Client) RemoveContext(context string) error {
81-
// this function should be deleted and Unset in viper used instead once
82-
// https://github.com/spf13/viper/pull/519
83-
// is merged or similar functionality is added.
84-
if context == "" {
85-
return fmt.Errorf("context cannot be empty")
86-
}
87-
88-
currentContext := client.Viper.GetString("liquidweb.api.current_context")
89-
if context == currentContext {
90-
return fmt.Errorf("cannot remove context currently set as current context")
91-
}
92-
93-
contexts := client.Viper.GetStringMap("liquidweb.api.contexts")
94-
if _, exists := contexts[context]; !exists {
95-
return fmt.Errorf("context %s doesnt exist, cannot remove", context)
96-
}
97-
98-
// save current config into a map, then delete requested context
99-
cfgMap := client.Viper.AllSettings()
100-
delete(cfgMap["liquidweb"].(map[string]interface{})["api"].(map[string]interface{})["contexts"].(map[string]interface{}), context)
101-
102-
// json encode modified map
103-
encodedCfg, err := json.MarshalIndent(cfgMap, "", " ")
104-
if err != nil {
105-
return err
106-
}
107-
108-
// read newly encoded config back into viper
109-
if err := client.Viper.ReadConfig(bytes.NewBuffer(encodedCfg)); err != nil {
110-
return err
111-
}
112-
113-
// write the new viper configuration to file
114-
if err := client.Viper.WriteConfig(); err != nil {
115-
return err
116-
}
117-
118-
return nil
119-
}
120-
12180
func (client *Client) CallLwApiInto(method string, methodArgs map[string]interface{}, obj interface{}) (err error) {
12281
got, err := client.LwCliApiClient.Call(method, methodArgs)
12382
if err != nil {

0 commit comments

Comments
 (0)