Skip to content

Commit b57cc2d

Browse files
authored
Some UI improvements around current profile (#1273)
`elastic-package profiles use` can be used to select the profile that will be used by default. This change includes some UI improvements around it. * Current profile is shown now in `elastic-package profiles list`. * If current profile is deleted, current profile is replaced by the default profile, that cannot be removed. * A confirmation message is shown now after changing the current profile.
1 parent d02a94a commit b57cc2d

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

cmd/profiles.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ User profiles can be configured with a "config.yml" file in the profile director
6363
}
6464

6565
if fromName == "" {
66-
fmt.Printf("Created profile %s.\n", newProfileName)
66+
fmt.Printf("Created profile %q.\n", newProfileName)
6767
} else {
68-
fmt.Printf("Created profile %s from %s.\n", newProfileName, fromName)
68+
fmt.Printf("Created profile %q from %q.\n", newProfileName, fromName)
6969
}
7070

7171
return nil
@@ -82,12 +82,32 @@ User profiles can be configured with a "config.yml" file in the profile director
8282
}
8383
profileName := args[0]
8484

85-
err := profile.DeleteProfile(profileName)
85+
config, err := install.Configuration()
86+
if err != nil {
87+
return fmt.Errorf("failed to load current configuration: %w", err)
88+
}
89+
90+
err = profile.DeleteProfile(profileName)
8691
if err != nil {
8792
return errors.Wrap(err, "error deleting profile")
8893
}
8994

90-
fmt.Printf("Deleted profile %s\n", profileName)
95+
if currentProfile := config.CurrentProfile(); currentProfile == profileName {
96+
config.SetCurrentProfile(profile.DefaultProfile)
97+
98+
location, err := locations.NewLocationManager()
99+
if err != nil {
100+
return fmt.Errorf("error fetching profile: %w", err)
101+
}
102+
err = install.WriteConfigFile(location, config)
103+
if err != nil {
104+
return fmt.Errorf("failed to store configuration: %w", err)
105+
}
106+
107+
cmd.Printf("%q was the current profile. Default profile will be used now.\n", profileName)
108+
}
109+
110+
fmt.Printf("Deleted profile %q.\n", profileName)
91111

92112
return nil
93113
},
@@ -117,7 +137,11 @@ User profiles can be configured with a "config.yml" file in the profile director
117137

118138
switch format {
119139
case tableFormat:
120-
return formatTable(profileList)
140+
config, err := install.Configuration()
141+
if err != nil {
142+
return fmt.Errorf("failed to load current configuration: %w", err)
143+
}
144+
return formatTable(profileList, config.CurrentProfile())
121145
case jsonFormat:
122146
return formatJSON(profileList)
123147
default:
@@ -156,6 +180,8 @@ User profiles can be configured with a "config.yml" file in the profile director
156180
if err != nil {
157181
return fmt.Errorf("failed to store configuration: %w", err)
158182
}
183+
184+
cmd.Printf("Current profile set to %q.\n", profileName)
159185
return nil
160186
},
161187
}
@@ -181,9 +207,9 @@ func formatJSON(profileList []profile.Metadata) error {
181207
return nil
182208
}
183209

184-
func formatTable(profileList []profile.Metadata) error {
210+
func formatTable(profileList []profile.Metadata, currentProfile string) error {
185211
table := tablewriter.NewWriter(os.Stdout)
186-
var profilesTable = profileToList(profileList)
212+
var profilesTable = profileToList(profileList, currentProfile)
187213

188214
table.SetHeader([]string{"Name", "Date Created", "User", "Version", "Path"})
189215
table.SetHeaderColor(
@@ -209,10 +235,14 @@ func formatTable(profileList []profile.Metadata) error {
209235
return nil
210236
}
211237

212-
func profileToList(profiles []profile.Metadata) [][]string {
238+
func profileToList(profiles []profile.Metadata, currentProfile string) [][]string {
213239
var profileList [][]string
214240
for _, profile := range profiles {
215-
profileList = append(profileList, []string{profile.Name, profile.DateCreated.Format(time.RFC3339), profile.User, profile.Version, profile.Path})
241+
name := profile.Name
242+
if name == currentProfile {
243+
name = name + " (current)"
244+
}
245+
profileList = append(profileList, []string{name, profile.DateCreated.Format(time.RFC3339), profile.User, profile.Version, profile.Path})
216246
}
217247

218248
return profileList

0 commit comments

Comments
 (0)