Skip to content

Commit ae24022

Browse files
committed
Unify code to load profiles in stack subcommands
1 parent 6a89b90 commit ae24022

File tree

1 file changed

+26
-50
lines changed

1 file changed

+26
-50
lines changed

cmd/stack.go

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,11 @@ func setupStackCommand() *cobraext.Command {
7878
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
7979
}
8080

81-
profileName, err := getProfileNameFlag(cmd)
81+
userProfile, err := getProfileFlag(cmd)
8282
if err != nil {
8383
return err
8484
}
8585

86-
userProfile, err := profile.LoadProfile(profileName)
87-
if errors.Is(err, profile.ErrNotAProfile) {
88-
pList, err := availableProfilesAsAList()
89-
if err != nil {
90-
return errors.Wrap(err, "error listing known profiles")
91-
}
92-
return fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(pList, ", "))
93-
}
94-
if err != nil {
95-
return errors.Wrap(err, "error loading profile")
96-
}
97-
9886
// Print information before starting the stack, for cases where
9987
// this is executed in the foreground, without daemon mode.
10088
cmd.Printf("Using profile %s.\n", userProfile.ProfilePath)
@@ -129,24 +117,11 @@ func setupStackCommand() *cobraext.Command {
129117
RunE: func(cmd *cobra.Command, args []string) error {
130118
cmd.Println("Take down the Elastic stack")
131119

132-
profileName, err := getProfileNameFlag(cmd)
120+
userProfile, err := getProfileFlag(cmd)
133121
if err != nil {
134122
return err
135123
}
136124

137-
userProfile, err := profile.LoadProfile(profileName)
138-
if errors.Is(err, profile.ErrNotAProfile) {
139-
pList, err := availableProfilesAsAList()
140-
if err != nil {
141-
return errors.Wrap(err, "error listing known profiles")
142-
}
143-
return fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, pList)
144-
}
145-
146-
if err != nil {
147-
return errors.Wrap(err, "error loading profile")
148-
}
149-
150125
err = stack.TearDown(stack.Options{
151126
Profile: userProfile,
152127
})
@@ -165,16 +140,11 @@ func setupStackCommand() *cobraext.Command {
165140
RunE: func(cmd *cobra.Command, args []string) error {
166141
cmd.Println("Update the Elastic stack")
167142

168-
profileName, err := getProfileNameFlag(cmd)
143+
profile, err := getProfileFlag(cmd)
169144
if err != nil {
170145
return err
171146
}
172147

173-
profile, err := profile.LoadProfile(profileName)
174-
if err != nil {
175-
return errors.Wrap(err, "error loading profile")
176-
}
177-
178148
stackVersion, err := cmd.Flags().GetString(cobraext.StackVersionFlagName)
179149
if err != nil {
180150
return cobraext.FlagParsingError(err, cobraext.StackVersionFlagName)
@@ -198,11 +168,6 @@ func setupStackCommand() *cobraext.Command {
198168
Use: "shellinit",
199169
Short: "Export environment variables",
200170
RunE: func(cmd *cobra.Command, args []string) error {
201-
profileName, err := getProfileNameFlag(cmd)
202-
if err != nil {
203-
return err
204-
}
205-
206171
shellName, err := cmd.Flags().GetString(cobraext.ShellInitShellFlagName)
207172
if err != nil {
208173
return cobraext.FlagParsingError(err, cobraext.ShellInitShellFlagName)
@@ -216,9 +181,9 @@ func setupStackCommand() *cobraext.Command {
216181
fmt.Fprintf(cmd.OutOrStderr(), "Detected shell: %s\n", shellName)
217182
}
218183

219-
profile, err := profile.LoadProfile(profileName)
184+
profile, err := getProfileFlag(cmd)
220185
if err != nil {
221-
return errors.Wrap(err, "error loading profile")
186+
return err
222187
}
223188

224189
shellCode, err := stack.ShellInit(profile, shellName)
@@ -241,16 +206,11 @@ func setupStackCommand() *cobraext.Command {
241206
return cobraext.FlagParsingError(err, cobraext.StackDumpOutputFlagName)
242207
}
243208

244-
profileName, err := getProfileNameFlag(cmd)
209+
profile, err := getProfileFlag(cmd)
245210
if err != nil {
246211
return err
247212
}
248213

249-
profile, err := profile.LoadProfile(profileName)
250-
if err != nil {
251-
return errors.Wrap(err, "error loading profile")
252-
}
253-
254214
target, err := stack.Dump(stack.DumpOptions{
255215
Output: output,
256216
Profile: profile,
@@ -367,19 +327,35 @@ func getParentInfo(ppid int) (types.ProcessInfo, error) {
367327
return parentInfo, nil
368328
}
369329

370-
func getProfileNameFlag(cmd *cobra.Command) (string, error) {
330+
func getProfileFlag(cmd *cobra.Command) (*profile.Profile, error) {
371331
profileName, err := cmd.Flags().GetString(cobraext.ProfileFlagName)
372332
if err != nil {
373-
return "", cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
333+
return nil, cobraext.FlagParsingError(err, cobraext.ProfileFlagName)
374334
}
375335
if profileName == "" {
376336
config, err := install.Configuration()
377337
if err != nil {
378-
return "", fmt.Errorf("cannot read configuration: %w", err)
338+
return nil, fmt.Errorf("cannot read configuration: %w", err)
379339
}
380340
profileName = config.CurrentProfile()
381341
}
382-
return profileName, nil
342+
343+
userProfile, err := profile.LoadProfile(profileName)
344+
if errors.Is(err, profile.ErrNotAProfile) {
345+
list, err := availableProfilesAsAList()
346+
if err != nil {
347+
return nil, errors.Wrap(err, "error listing known profiles")
348+
}
349+
if len(list) == 0 {
350+
return nil, fmt.Errorf("%s is not a valid profile", profileName)
351+
}
352+
return nil, fmt.Errorf("%s is not a valid profile, known profiles are: %s", profileName, strings.Join(list, ", "))
353+
}
354+
if err != nil {
355+
return nil, errors.Wrap(err, "error loading profile")
356+
}
357+
358+
return userProfile, nil
383359
}
384360

385361
func getShellName(exe string) string {

0 commit comments

Comments
 (0)