Skip to content

Commit d86ee19

Browse files
committed
Merge branch 'master' into plan-cloud-server-create
2 parents 1c0f820 + 0ed97f9 commit d86ee19

File tree

7 files changed

+101
-68
lines changed

7 files changed

+101
-68
lines changed

.goreleaser.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ builds:
88
- env:
99
- CGO_ENABLED=0
1010
goos:
11-
- freebsd
1211
- windows
1312
- linux
1413
- darwin

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
@@ -30,6 +30,7 @@ import (
3030

3131
var cfgFile string
3232
var lwCliInst instance.Client
33+
var useContext string
3334

3435
var rootCmd = &cobra.Command{
3536
Use: "lw",
@@ -63,6 +64,7 @@ func init() {
6364
cobra.OnInitialize(initConfig)
6465

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

6870
func initConfig() {
@@ -86,6 +88,15 @@ func initConfig() {
8688
vp.AutomaticEnv()
8789
vp.ReadInConfig()
8890

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

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
114114
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
115115
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
116116
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
117-
github.com/spf13/viper v1.6.1 h1:VPZzIkznI1YhVMRi6vNFLHSwhnhReBfgTxIPccpfdZk=
118-
github.com/spf13/viper v1.6.1/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
119117
github.com/spf13/viper v1.6.2 h1:7aKfF+e8/k68gda3LOjo5RxiUqddoFxVq4BKBPrxk5E=
120118
github.com/spf13/viper v1.6.2/go.mod h1:t3iDnF5Jlj76alVNuyFBk5oUMCvsrkbvZK0WQdfDi5k=
121119
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -135,8 +133,6 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
135133
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
136134
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
137135
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
138-
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d h1:2+ZP7EfsZV7Vvmx3TIqSlSzATMkTAKqM14YGFPoSKjI=
139-
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
140136
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d h1:9FCpayM9Egr1baVnV1SX0H87m+XB0B8S0hAMi99X/3U=
141137
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
142138
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -159,8 +155,6 @@ golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5h
159155
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
160156
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
161157
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162-
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
163-
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
164158
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
165159
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
166160
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
@@ -191,8 +185,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
191185
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
192186
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
193187
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
194-
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
195-
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
196188
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
197189
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
198190
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

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 {

scripts/build/release-build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
source scripts/build/.variables
44
set -e
5-
VERSION=${VERSION} GITCOMMIT=${GITCOMMIT} BUILDTIME=${BUILDTIME} goreleaser --snapshot --skip-publish --rm-dist
5+
VERSION=${VERSION} GITCOMMIT=${GITCOMMIT} BUILDTIME=${BUILDTIME} goreleaser --rm-dist
66
exit 0

0 commit comments

Comments
 (0)