forked from skevetter/devpod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.go
More file actions
188 lines (166 loc) · 5.12 KB
/
add.go
File metadata and controls
188 lines (166 loc) · 5.12 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package provider
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/skevetter/devpod/cmd/flags"
"github.com/skevetter/devpod/pkg/config"
"github.com/skevetter/devpod/pkg/provider"
"github.com/skevetter/devpod/pkg/types"
"github.com/skevetter/devpod/pkg/workspace"
"github.com/skevetter/log"
"github.com/spf13/cobra"
)
// AddCmd holds the cmd flags.
type AddCmd struct {
*flags.GlobalFlags
Use bool
SingleMachine bool
Options []string
Name string
FromExisting string
}
// NewAddCmd creates a new command.
func NewAddCmd(f *flags.GlobalFlags) *cobra.Command {
cmd := &AddCmd{
GlobalFlags: f,
}
addCmd := &cobra.Command{
Use: "add [URL or path]",
Short: "Adds a new provider to DevPod",
PreRunE: func(cobraCommand *cobra.Command, args []string) error {
if cmd.FromExisting != "" {
return cobraCommand.MarkFlagRequired("name")
}
return nil
},
RunE: func(_ *cobra.Command, args []string) error {
ctx := context.Background()
devPodConfig, err := config.LoadConfig(cmd.Context, cmd.Provider)
if err != nil {
return err
}
return cmd.Run(ctx, devPodConfig, args)
},
}
addCmd.Flags().
BoolVar(&cmd.SingleMachine, "single-machine", false, "If enabled will use a single machine for all workspaces")
addCmd.Flags().
StringVar(&cmd.Name, "name", "",
"The name for the new provider. If not specified, the name from the provider's configuration file will be used.")
addCmd.Flags().
StringVar(&cmd.FromExisting, "from-existing", "",
"The name of an existing provider to use as a template. Needs to be used in conjunction with the --name flag")
addCmd.Flags().
BoolVar(&cmd.Use, "use", true, "If enabled will automatically activate the provider")
addCmd.Flags().
StringArrayVarP(&cmd.Options, "option", "o", []string{}, "Provider option in the form KEY=VALUE")
return addCmd
}
func (cmd *AddCmd) Run(ctx context.Context, devPodConfig *config.Config, args []string) error {
if len(args) != 1 && cmd.FromExisting == "" {
return fmt.Errorf("please specify either a local file, URL or Git repository. " +
"E.g. devpod provider add https://path/to/my/provider.yaml")
} else if cmd.Name != "" && provider.ProviderNameRegEx.MatchString(cmd.Name) {
return fmt.Errorf("provider name can only include smaller case letters, numbers or dashes")
} else if cmd.Name != "" && len(cmd.Name) > 32 {
return fmt.Errorf("provider name cannot be longer than 32 characters")
} else if cmd.FromExisting != "" && devPodConfig.Current() != nil && devPodConfig.Current().Providers[cmd.FromExisting] == nil {
return fmt.Errorf("provider %s does not exist", cmd.FromExisting)
}
var providerConfig *provider.ProviderConfig
var options []string
if cmd.FromExisting != "" {
providerWithOptions, err := workspace.CloneProvider(
devPodConfig,
cmd.Name,
cmd.FromExisting,
log.Default,
)
if err != nil {
return err
}
providerConfig = providerWithOptions.Config
options = mergeOptions(
providerWithOptions.Config.Options,
providerWithOptions.State.Options,
cmd.Options,
)
} else {
c, err := workspace.AddProvider(devPodConfig, cmd.Name, args[0], log.Default)
if err != nil {
return err
}
providerConfig = c
options = cmd.Options
}
log.WithFields(logrus.Fields{
"providerName": providerConfig.Name,
}).Done("installed provider")
if cmd.Use {
configureErr := ConfigureProvider(ctx, ProviderOptionsConfig{
Provider: providerConfig,
Context: devPodConfig.DefaultContext,
UserOptions: options,
Reconfigure: true,
SkipRequired: false,
SkipInit: false,
SkipSubOptions: false,
SingleMachine: &cmd.SingleMachine,
Log: log.Default,
})
if configureErr != nil {
devPodConfig, err := config.LoadConfig(cmd.Context, "")
if err != nil {
return err
}
err = DeleteProvider(ctx, devPodConfig, providerConfig.Name, true, true, log.Default)
if err != nil {
return fmt.Errorf("delete provider: %w", err)
}
return fmt.Errorf("configure provider: %w", configureErr)
}
return nil
}
log.Default.Infof("To use the provider, please run the following command:")
log.Default.Infof("devpod provider use %s", providerConfig.Name)
return nil
}
// mergeOptions combines user options with existing options, user provided options take precedence.
func mergeOptions(
desiredOptions map[string]*types.Option,
stateOptions map[string]config.OptionValue,
userOptions []string,
) []string {
retOptions := []string{}
for key := range desiredOptions {
userOption, ok := getUserOption(userOptions, key)
if ok {
retOptions = append(retOptions, userOption)
continue
}
stateOption, ok := stateOptions[key]
if !ok {
continue
}
retOptions = append(retOptions, fmt.Sprintf("%s=%s", key, stateOption.Value))
}
return retOptions
}
func getUserOption(allOptions []string, optionKey string) (string, bool) {
if len(allOptions) == 0 {
return "", false
}
for _, option := range allOptions {
splitted := strings.Split(option, "=")
if len(splitted) == 1 {
// ignore
continue
}
if splitted[0] == optionKey {
return option, true
}
}
return "", false
}