/
deleteprofile.go
57 lines (45 loc) · 1.48 KB
/
deleteprofile.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
package cmd
import (
"errors"
"fmt"
"io/fs"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/mchlumsky/mracek/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
func NewDeleteProfileCommand() *cobra.Command {
return &cobra.Command{
Use: "delete-profile PROFILE",
Short: "Delete profile",
Long: "Delete profile",
RunE: deleteProfileCommandRunE(),
ValidArgsFunction: func() ValidArgsFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
opts := config.YAMLOpts{Directory: viper.GetString("os-config-dir")}
return validArgsFunction(opts.AllProfileNames)(cmd, args, toComplete)
}
}(),
}
}
func deleteProfileCommandRunE() func(cmd *cobra.Command, args []string) error {
return func(_ *cobra.Command, args []string) error {
opts := config.YAMLOpts{Directory: viper.GetString("os-config-dir")}
profiles, err := config.LoadAndCheckOSConfigfile("clouds-public.yaml", opts.LoadPublicCloudsYAML, "")
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("try creating some profiles first with 'mracek create-profile': %w", err)
}
return err
}
delete(profiles, args[0])
pc := map[string]map[string]clientconfig.Cloud{"public-clouds": profiles}
publicCloudsOut, err := yaml.Marshal(&pc)
if err != nil {
return err
}
err = config.WriteOSConfig(viper.GetString("os-config-dir"), nil, nil, publicCloudsOut)
return err
}
}