forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathuse.go
More file actions
112 lines (94 loc) · 2.48 KB
/
use.go
File metadata and controls
112 lines (94 loc) · 2.48 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ide
import (
"context"
"fmt"
"maps"
"strings"
"github.com/skevetter/devpod/cmd/flags"
"github.com/skevetter/devpod/pkg/config"
"github.com/skevetter/devpod/pkg/ide"
"github.com/skevetter/devpod/pkg/ide/ideparse"
options2 "github.com/skevetter/devpod/pkg/options"
"github.com/spf13/cobra"
)
// UseCmd holds the use cmd flags.
type UseCmd struct {
*flags.GlobalFlags
Options []string
}
// NewUseCmd creates a new command.
func NewUseCmd(flags *flags.GlobalFlags) *cobra.Command {
cmd := &UseCmd{
GlobalFlags: flags,
}
useCmd := &cobra.Command{
Use: "use",
Short: "Configure the default IDE to use (list available IDEs with 'devpod ide list')",
Long: `Configure the default IDE to use
Available IDEs can be listed with 'devpod ide list'`,
RunE: func(cobraCmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf(
"please specify the ide to use, list available IDEs with 'devpod ide list'",
)
}
return cmd.Run(cobraCmd.Context(), args[0])
},
}
useCmd.Flags().
StringArrayVarP(&cmd.Options, "option", "o", []string{}, "IDE option in the form KEY=VALUE")
return useCmd
}
// Run runs the command logic.
func (cmd *UseCmd) Run(ctx context.Context, ide string) error {
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}
ide = strings.ToLower(ide)
ideOptions, err := ideparse.GetIDEOptions(ide)
if err != nil {
return err
}
// check if there are user options set
if len(cmd.Options) > 0 {
err = setOptions(devPodConfig, ide, cmd.Options, ideOptions)
if err != nil {
return err
}
}
devPodConfig.Current().DefaultIDE = ide
err = config.SaveConfig(devPodConfig)
if err != nil {
return fmt.Errorf("save config: %w", err)
}
return nil
}
func setOptions(
devPodConfig *config.Config,
ide string,
userOptions []string,
ideOptions ide.Options,
) error {
userOptions = options2.InheritOptionsFromEnvironment(
userOptions,
ideOptions,
config.EnvIDEPrefix+ide+"_",
)
optionValues, err := ideparse.ParseOptions(userOptions, ideOptions)
if err != nil {
return err
}
if devPodConfig.Current().IDEs == nil {
devPodConfig.Current().IDEs = map[string]*config.IDEConfig{}
}
newValues := map[string]config.OptionValue{}
if devPodConfig.Current().IDEs[ide] != nil {
maps.Copy(newValues, devPodConfig.Current().IDEs[ide].Options)
}
maps.Copy(newValues, optionValues)
devPodConfig.Current().IDEs[ide] = &config.IDEConfig{
Options: newValues,
}
return nil
}