diff --git a/cmd/minikube/cmd/config/profile_list.go b/cmd/minikube/cmd/config/profile_list.go index 1189fc14ff0c..fbf5c1e51546 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -41,6 +41,7 @@ import ( ) var output string +var isLight bool var profileListCmd = &cobra.Command{ Use: "list", @@ -58,8 +59,18 @@ var profileListCmd = &cobra.Command{ }, } +func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error) { + if isLight { + validProfiles, err = config.ListValidProfiles() + } else { + validProfiles, invalidProfiles, err = config.ListProfiles() + } + + return validProfiles, invalidProfiles, err +} + func printProfilesTable() { - validProfiles, invalidProfiles, err := config.ListProfiles() + validProfiles, invalidProfiles, err := listProfiles() if err != nil { klog.Warningf("error loading profiles: %v", err) @@ -75,6 +86,13 @@ func printProfilesTable() { } func updateProfilesStatus(profiles []*config.Profile) { + if isLight { + for _, p := range profiles { + p.Status = "Skipped" + } + return + } + api, err := machine.NewAPIClient() if err != nil { klog.Errorf("failed to get machine api client %v", err) @@ -168,7 +186,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) { } func printProfilesJSON() { - validProfiles, invalidProfiles, err := config.ListProfiles() + validProfiles, invalidProfiles, err := listProfiles() updateProfilesStatus(validProfiles) @@ -195,5 +213,6 @@ func profilesOrDefault(profiles []*config.Profile) []*config.Profile { func init() { profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'") + profileListCmd.Flags().BoolVarP(&isLight, "light", "l", false, "If true, only display vaild profiles and do not check status.") ProfileCmd.AddCommand(profileListCmd) } diff --git a/site/content/en/docs/commands/profile.md b/site/content/en/docs/commands/profile.md index 6bd445f828a1..5bdfec2f43c8 100644 --- a/site/content/en/docs/commands/profile.md +++ b/site/content/en/docs/commands/profile.md @@ -89,6 +89,7 @@ minikube profile list [flags] ### Options ``` + -l, --light If true, only display vaild profiles and do not check status. -o, --output string The output format. One of 'json', 'table' (default "table") ``` diff --git a/test/integration/functional_test.go b/test/integration/functional_test.go index d52ae2f864c7..cd1bb29de44a 100644 --- a/test/integration/functional_test.go +++ b/test/integration/functional_test.go @@ -813,6 +813,58 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) { } }) + + t.Run("profile_list_light", func(t *testing.T) { + // List profiles + rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-l")) + if err != nil { + t.Errorf("failed to list profiles: args %q : %v", rr.Command(), err) + } + + // Table output + listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n") + profileExists := false + for i := 3; i < (len(listLines) - 1); i++ { + profileLine := listLines[i] + if strings.Contains(profileLine, profile) && strings.Contains(profileLine, "SKIPPED") { + profileExists = true + break + } + } + if !profileExists { + t.Errorf("expected 'profile list' output to include %q with 'SKIPPED' status but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command()) + } + }) + + t.Run("profile_list_light_json", func(t *testing.T) { + // Json output + rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--light", "--output", "json")) + if err != nil { + t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err) + } + var jsonObject map[string][]config.Profile + err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject) + if err != nil { + t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err) + } + validProfiles := jsonObject["valid"] + profileExists := false + for _, profileObject := range validProfiles { + if profileObject.Name != profile { + continue + } + + profileExists = true + if profileObject.Status != "SKIPPED" { + t.Errorf("expected the json of 'profile list --light' to include 'SKIPPED' status for %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command()) + } + break + } + if !profileExists { + t.Errorf("expected the json of 'profile list --light' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command()) + } + + }) } // validateServiceCmd asserts basic "service" command functionality