forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse.go
65 lines (54 loc) · 1.41 KB
/
use.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
package context
import (
"context"
"fmt"
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/config"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// UseCmd holds the use cmd flags
type UseCmd struct {
flags.GlobalFlags
Options []string
}
// NewUseCmd uses a new command
func NewUseCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd := &UseCmd{
GlobalFlags: *flags,
}
useCmd := &cobra.Command{
Use: "use",
Short: "Set a DevPod context as the default",
RunE: func(_ *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("please specify the context to use")
}
return cmd.Run(context.Background(), args[0])
},
}
useCmd.Flags().StringArrayVarP(&cmd.Options, "option", "o", []string{}, "context option in the form KEY=VALUE")
return useCmd
}
// Run runs the command logic
func (cmd *UseCmd) Run(ctx context.Context, context string) error {
devPodConfig, err := config.LoadConfig("", cmd.Provider)
if err != nil {
return err
} else if devPodConfig.Contexts[context] == nil {
return fmt.Errorf("context '%s' doesn't exist", context)
}
// check if there are use options set
if len(cmd.Options) > 0 {
err = setOptions(devPodConfig, context, cmd.Options)
if err != nil {
return err
}
}
devPodConfig.DefaultContext = context
err = config.SaveConfig(devPodConfig)
if err != nil {
return errors.Wrap(err, "save config")
}
return nil
}