diff --git a/e2e/application/application_test.go b/e2e/application/application_test.go index 8881b4fab..098177a69 100644 --- a/e2e/application/application_test.go +++ b/e2e/application/application_test.go @@ -25,7 +25,7 @@ var _ = ginkgo.Describe("Application", func() { workloadType, applicationName)) e2e.ComponentListContext("ls", applicationName, "") e2e.TraitManualScalerAttachContext("vela attach scaler trait", traitAlias, applicationName) - e2e.ApplicationShowContext("app show", applicationName, workloadType) + e2e.ApplicationShowContext("show", applicationName, workloadType) e2e.ApplicationStatusContext("app status", applicationName, workloadType) e2e.ApplicationCompStatusContext("svc status", applicationName, workloadType, envName) e2e.ApplicationExecContext("exec -- COMMAND", applicationName) diff --git a/e2e/commonContext.go b/e2e/commonContext.go index 8607985e2..003b8e6f5 100644 --- a/e2e/commonContext.go +++ b/e2e/commonContext.go @@ -218,7 +218,7 @@ var ( ApplicationShowContext = func(context string, applicationName string, workloadType string) bool { return ginkgo.Context(context, func() { ginkgo.It("should show app information", func() { - cli := fmt.Sprintf("vela app show %s", applicationName) + cli := fmt.Sprintf("vela show %s", applicationName) output, err := Exec(cli) gomega.Expect(err).NotTo(gomega.HaveOccurred()) // TODO(zzxwill) need to check workloadType after app show is refined diff --git a/pkg/commands/show.go b/pkg/commands/show.go index 683e98741..dc9d23f2c 100644 --- a/pkg/commands/show.go +++ b/pkg/commands/show.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + "github.com/oam-dev/kubevela/pkg/oam" + "github.com/AlecAivazis/survey/v2" "github.com/gosuri/uitable" "github.com/oam-dev/kubevela/api/types" @@ -12,8 +14,6 @@ import ( "github.com/spf13/cobra" ) -const DefaultChosenSvc = "ALL SERVICES" - func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "show ", @@ -50,26 +50,43 @@ func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) err return err } - var chosenSvc string - var validInputtedSvc bool - inputtedSvc := cmd.Flag("svc").Value.String() - + var svcFlag, chosenSvc string + var svcFlagStatus string + // to store the value of flag `--svc` set in Cli, or selected value in survey + var targetServices []string + if svcFlag = cmd.Flag("svc").Value.String(); svcFlag == "" { + svcFlagStatus = oam.FlagNotSet + } else { + svcFlagStatus = oam.FlagIsInvalid + } + // all services name of the application `appName` var services []string for svcName := range app.Services { services = append(services, svcName) - if inputtedSvc == svcName { - validInputtedSvc = true + if svcFlag == svcName { + svcFlagStatus = oam.FlagIsValid + targetServices = append(targetServices, svcName) } } - - if inputtedSvc != "" && !validInputtedSvc || inputtedSvc == "" && len(services) > 1 { - if inputtedSvc != "" && !validInputtedSvc { - cmd.Printf("The service name '%s' is not valid\n", inputtedSvc) + totalServices := len(services) + if svcFlagStatus == oam.FlagNotSet && totalServices == 1 { + targetServices = services + } + if svcFlagStatus == oam.FlagIsInvalid || (svcFlagStatus == oam.FlagNotSet && totalServices > 1) { + if svcFlagStatus == oam.FlagIsInvalid { + cmd.Printf("The service name '%s' is not valid\n", svcFlag) } chosenSvc, err = chooseSvc(services) if err != nil { return err } + + if chosenSvc == oam.DefaultChosenAllSvc { + targetServices = services + } else { + targetServices = targetServices[:0] + targetServices = append(targetServices, chosenSvc) + } } cmd.Printf("About:\n\n") @@ -87,14 +104,9 @@ func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) err table = uitable.New() cmd.Printf("Services:\n\n") - for svcName := range app.Services { - if inputtedSvc != "" && validInputtedSvc && svcName == inputtedSvc || - inputtedSvc != "" && !validInputtedSvc && (svcName == chosenSvc || chosenSvc == DefaultChosenSvc) || - inputtedSvc == "" && len(services) == 1 || - inputtedSvc == "" && len(services) > 1 && (svcName == chosenSvc || chosenSvc == DefaultChosenSvc) { - if err := showComponent(cmd, env, svcName, appName); err != nil { - return err - } + for _, svcName := range targetServices { + if err := showComponent(cmd, env, svcName, appName); err != nil { + return err } } cmd.Println(table.String()) @@ -151,11 +163,11 @@ func showComponent(cmd *cobra.Command, env *types.EnvMeta, compName, appName str func chooseSvc(services []string) (string, error) { var svcName string - services = append(services, DefaultChosenSvc) + services = append(services, oam.DefaultChosenAllSvc) prompt := &survey.Select{ Message: "Please choose one service: ", Options: services, - Default: DefaultChosenSvc, + Default: oam.DefaultChosenAllSvc, } err := survey.AskOne(prompt, &svcName) if err != nil { diff --git a/pkg/oam/application.go b/pkg/oam/application.go index fb8604d74..16e6035c0 100644 --- a/pkg/oam/application.go +++ b/pkg/oam/application.go @@ -17,6 +17,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +const ( + DefaultChosenAllSvc = "ALL SERVICES" + FlagNotSet = "FlagNotSet" + FlagIsInvalid = "FlagIsInvalid" + FlagIsValid = "FlagIsValid" +) + type componentMetaList []apis.ComponentMeta type ApplicationMetaList []apis.ApplicationMeta