Skip to content

Commit 1d772cf

Browse files
committed
feature: add switch profile command
Fixes #182 Adds a switch profile command to safely load an existing profile without creating a new profile. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 20c53ca commit 1d772cf

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

cmd/switch.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package cmd
19+
20+
import (
21+
"errors"
22+
"fmt"
23+
"reflect"
24+
"strings"
25+
26+
"github.com/apache/cloudstack-cloudmonkey/config"
27+
)
28+
29+
func init() {
30+
AddCommand(&Command{
31+
Name: "switch",
32+
Help: "Switches profile",
33+
SubCommands: map[string][]string{
34+
"profile": {},
35+
},
36+
Handle: func(r *Request) error {
37+
if len(r.Args) < 1 {
38+
fmt.Println("Please provide one of the sub-commands: ", reflect.ValueOf(r.Command.SubCommands).MapKeys())
39+
return nil
40+
}
41+
subCommand := r.Args[0]
42+
value := strings.Trim(strings.Join(r.Args[1:], " "), " ")
43+
config.Debug("Switch command received:", subCommand, " values:", value)
44+
if r.Args[len(r.Args)-1] == "-h" {
45+
fmt.Println("Usage: switch <subcommand> <option>. Press tab-tab to see available subcommands and options.")
46+
return nil
47+
}
48+
validArgs := r.Command.SubCommands[subCommand]
49+
if len(validArgs) != 0 {
50+
if !config.CheckIfValuePresent(validArgs, value) {
51+
return errors.New("Invalid value for " + subCommand + ". Supported values: " + strings.Join(validArgs, ", "))
52+
}
53+
}
54+
55+
if subCommand == "profile" {
56+
if err := r.Config.LoadProfile(value, false); err != nil {
57+
if r.Config.HasShell {
58+
fmt.Printf("Failed to switch to server profile: %s due to: %v\n", value, err)
59+
return nil
60+
}
61+
return err
62+
}
63+
if r.Config.HasShell {
64+
ap := r.Config.ActiveProfile
65+
fmt.Printf("Loaded server profile: %s\nUrl: %s\nUsername: %s\nDomain: %s\nAPI Key: %s\nTotal APIs: %d\n\n",
66+
r.Config.Core.ProfileName, ap.URL, ap.Username, ap.Domain, ap.APIKey, len(r.Config.GetCache()))
67+
}
68+
}
69+
return nil
70+
},
71+
})
72+
}

cmk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func main() {
8585
}
8686

8787
if *profile != "" {
88-
cfg.LoadProfile(*profile)
88+
cfg.LoadProfile(*profile, true)
8989
}
9090
config.LoadCache(cfg)
9191
cli.SetConfig(cfg)

config/config.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,18 +340,22 @@ func saveConfig(cfg *Config) *Config {
340340
}
341341

342342
// LoadProfile loads an existing profile
343-
func (c *Config) LoadProfile(name string) {
343+
func (c *Config) LoadProfile(name string, isExit bool) error {
344344
Debug("Trying to load profile: " + name)
345345
conf := readConfig(c)
346346
section, err := conf.GetSection(name)
347347
if err != nil || section == nil {
348-
fmt.Printf("Unable to load profile '%s': %v", name, err)
349-
os.Exit(1)
348+
if isExit {
349+
fmt.Printf("Unable to load profile '%s': %v", name, err)
350+
os.Exit(1)
351+
}
352+
return err
350353
}
351354
profile := new(ServerProfile)
352355
conf.Section(name).MapTo(profile)
353356
setActiveProfile(c, profile)
354357
c.Core.ProfileName = name
358+
return nil
355359
}
356360

357361
// UpdateConfig updates and saves config

0 commit comments

Comments
 (0)