From 838995e816d8f1bcc847ec584577312d5ab2e8c0 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 30 Oct 2020 15:07:58 +0800 Subject: [PATCH 1/3] Refactor `vela show` Merge `vela app show` and `vela svc show` in `vela show` to display all details information of an application --- pkg/commands/app.go | 2 - pkg/commands/cli.go | 2 + pkg/commands/comp.go | 1 - pkg/commands/show.go | 132 ++++++++++++++++++++----------------------- 4 files changed, 64 insertions(+), 73 deletions(-) diff --git a/pkg/commands/app.go b/pkg/commands/app.go index 2b355a768..2a705dcd1 100644 --- a/pkg/commands/app.go +++ b/pkg/commands/app.go @@ -21,8 +21,6 @@ func NewAppsCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command { cmd.AddCommand( NewAppStatusCommand(c, ioStreams), - - NewAppShowCommand(ioStreams), NewRunCommand(c, ioStreams)) return cmd } diff --git a/pkg/commands/cli.go b/pkg/commands/cli.go index f3b289f2f..339ccdccd 100644 --- a/pkg/commands/cli.go +++ b/pkg/commands/cli.go @@ -72,6 +72,8 @@ func NewCommand() *cobra.Command { NewAppsCommand(commandArgs, ioStream), NewListCommand(commandArgs, ioStream), NewDeleteCommand(commandArgs, ioStream), + NewAppShowCommand(ioStream), + // Workloads AddCompCommands(commandArgs, ioStream), diff --git a/pkg/commands/comp.go b/pkg/commands/comp.go index cb2ca88a0..033c99e19 100644 --- a/pkg/commands/comp.go +++ b/pkg/commands/comp.go @@ -43,7 +43,6 @@ func AddCompCommands(c types.Args, ioStreams util.IOStreams) *cobra.Command { compCommands.AddCommand( NewCompDeployCommands(c, ioStreams), - NewCompShowCommand(ioStreams), NewCompStatusCommand(c, ioStreams), ) return compCommands diff --git a/pkg/commands/show.go b/pkg/commands/show.go index 7681a26f1..683e98741 100644 --- a/pkg/commands/show.go +++ b/pkg/commands/show.go @@ -3,22 +3,23 @@ package commands import ( "fmt" "os" - "strings" + "github.com/AlecAivazis/survey/v2" "github.com/gosuri/uitable" "github.com/oam-dev/kubevela/api/types" "github.com/oam-dev/kubevela/pkg/application" cmdutil "github.com/oam-dev/kubevela/pkg/commands/util" "github.com/spf13/cobra" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +const DefaultChosenSvc = "ALL SERVICES" + func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command { cmd := &cobra.Command{ Use: "show ", - Short: "get details of your app", - Long: "get details of your app, including its workload and trait", - Example: `vela app show `, + Short: "Get details of an application", + Long: "Get details of an application", + Example: `vela show `, RunE: func(cmd *cobra.Command, args []string) error { argsLength := len(args) if argsLength == 0 { @@ -38,24 +39,39 @@ func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command { types.TagCommandType: types.TypeApp, }, } + cmd.Flags().StringP("svc", "s", "", "service name") cmd.SetOut(ioStreams.Out) return cmd } -type Unkown struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` - Spec interface{} `json:"spec"` - Status interface{} `json:"status"` -} - func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) error { - app, err := application.Load(env.Name, appName) if err != nil { return err } + var chosenSvc string + var validInputtedSvc bool + inputtedSvc := cmd.Flag("svc").Value.String() + + var services []string + for svcName := range app.Services { + services = append(services, svcName) + if inputtedSvc == svcName { + validInputtedSvc = true + } + } + + if inputtedSvc != "" && !validInputtedSvc || inputtedSvc == "" && len(services) > 1 { + if inputtedSvc != "" && !validInputtedSvc { + cmd.Printf("The service name '%s' is not valid\n", inputtedSvc) + } + chosenSvc, err = chooseSvc(services) + if err != nil { + return err + } + } + cmd.Printf("About:\n\n") table := uitable.New() table.AddRow(" Name:", appName) @@ -71,56 +87,21 @@ func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) err table = uitable.New() cmd.Printf("Services:\n\n") - table.AddRow(" Name", "Type", "Traits") - - for compName := range app.Services { - wtype, _ := app.GetWorkload(compName) - var outPutTraits []string - traits, _ := app.GetTraits(compName) - for k := range traits { - outPutTraits = append(outPutTraits, k) + 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 + } } - table.AddRow(" "+compName, wtype, strings.Join(outPutTraits, ",")) } cmd.Println(table.String()) cmd.Println() return nil } -func NewCompShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command { - cmd := &cobra.Command{ - Use: "show ", - Short: "show service details", - Long: "show service details, including arguments of workload and traits", - Example: `vela svc show `, - RunE: func(cmd *cobra.Command, args []string) error { - argsLength := len(args) - if argsLength == 0 { - ioStreams.Errorf("Hint: please specify the service name\n") - os.Exit(1) - } - compName := args[0] - env, err := GetEnv(cmd) - if err != nil { - ioStreams.Errorf("Error: failed to get Env: %s", err) - return err - } - - appName, err := cmd.Flags().GetString(App) - if err != nil { - return err - } - - return showComponent(cmd, env, compName, appName) - }, - Annotations: map[string]string{ - types.TagCommandType: types.TypeApp, - }, - } - cmd.SetOut(ioStreams.Out) - return cmd -} - func showComponent(cmd *cobra.Command, env *types.EnvMeta, compName, appName string) error { var app *application.Application var err error @@ -137,37 +118,48 @@ func showComponent(cmd *cobra.Command, env *types.EnvMeta, compName, appName str if cname != compName { continue } - cmd.Printf("About:\n\n") wtype, data := app.GetWorkload(compName) table := uitable.New() - table.AddRow(" Name:", compName) - table.AddRow(" WorkloadType:", wtype) - table.AddRow(" Application:", app.Name) - cmd.Printf("%s\n\n", table.String()) - cmd.Printf("Environment:\n\n") - cmd.Printf(" Namespace:\t%s\n\n", env.Namespace) - cmd.Printf("Arguments:\n\n") + table.AddRow(" - Name:", compName) + table.AddRow(" WorkloadType:", wtype) + cmd.Printf(table.String()) + cmd.Printf("\n Arguments:\n") table = uitable.New() for k, v := range data { - table.AddRow(fmt.Sprintf(" %s:", k), v) + table.AddRow(fmt.Sprintf(" %s: ", k), v) } - cmd.Printf("%s\n\n", table.String()) + cmd.Printf("%s", table.String()) traits, err := app.GetTraits(compName) if err != nil { cmd.PrintErr(err) continue } cmd.Println() - cmd.Printf("Traits:\n\n") + cmd.Printf(" Traits:\n") for k, v := range traits { - cmd.Printf(" %s:\n", k) + cmd.Printf(" - %s:\n", k) table = uitable.New() for kk, vv := range v { - table.AddRow(fmt.Sprintf(" %s:", kk), vv) + table.AddRow(fmt.Sprintf(" %s:", kk), vv) } - cmd.Printf("%s\n\n", table.String()) + cmd.Printf("%s\n", table.String()) } cmd.Println() } return nil } + +func chooseSvc(services []string) (string, error) { + var svcName string + services = append(services, DefaultChosenSvc) + prompt := &survey.Select{ + Message: "Please choose one service: ", + Options: services, + Default: DefaultChosenSvc, + } + err := survey.AskOne(prompt, &svcName) + if err != nil { + return "", fmt.Errorf("failed to retrieve services of the application, err %v", err) + } + return svcName, nil +} From 6ffee730ec57c019cf2a8a3d454105495de3e7c1 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 30 Oct 2020 15:29:20 +0800 Subject: [PATCH 2/3] Update cli docs --- docs/cli/vela.md | 1 + docs/cli/vela_app.md | 1 - docs/cli/{vela_app_show.md => vela_show.md} | 15 +++++---- docs/cli/vela_svc.md | 1 - docs/cli/vela_svc_show.md | 36 --------------------- 5 files changed, 9 insertions(+), 45 deletions(-) rename docs/cli/{vela_app_show.md => vela_show.md} (50%) delete mode 100644 docs/cli/vela_svc_show.md diff --git a/docs/cli/vela.md b/docs/cli/vela.md index 25e79454b..cc49b0a35 100644 --- a/docs/cli/vela.md +++ b/docs/cli/vela.md @@ -35,6 +35,7 @@ vela [flags] * [vela port-forward](vela_port-forward.md) - Forward one or more local ports to a Pod of a service in an application * [vela route](vela_route.md) - Attach route trait to an app * [vela scaler](vela_scaler.md) - Attach scaler trait to an app +* [vela show](vela_show.md) - Get details of an application * [vela svc](vela_svc.md) - Manage services * [vela system](vela_system.md) - System management utilities * [vela template](vela_template.md) - Manage templates diff --git a/docs/cli/vela_app.md b/docs/cli/vela_app.md index a5287ca56..5c9da9424 100644 --- a/docs/cli/vela_app.md +++ b/docs/cli/vela_app.md @@ -28,7 +28,6 @@ vela app * [vela](vela.md) - * [vela app run](vela_app_run.md) - Run a bundle of OAM Applications -* [vela app show](vela_app_show.md) - get details of your app * [vela app status](vela_app_status.md) - get status of an application ###### Auto generated by spf13/cobra on 30-Oct-2020 diff --git a/docs/cli/vela_app_show.md b/docs/cli/vela_show.md similarity index 50% rename from docs/cli/vela_app_show.md rename to docs/cli/vela_show.md index a0a208442..45467b3b5 100644 --- a/docs/cli/vela_app_show.md +++ b/docs/cli/vela_show.md @@ -1,25 +1,26 @@ -## vela app show +## vela show -get details of your app +Get details of an application ### Synopsis -get details of your app, including its workload and trait +Get details of an application ``` -vela app show [flags] +vela show [flags] ``` ### Examples ``` -vela app show +vela show ``` ### Options ``` - -h, --help help for show + -h, --help help for show + -s, --svc string service name ``` ### Options inherited from parent commands @@ -30,6 +31,6 @@ vela app show ### SEE ALSO -* [vela app](vela_app.md) - Manage applications +* [vela](vela.md) - ###### Auto generated by spf13/cobra on 30-Oct-2020 diff --git a/docs/cli/vela_svc.md b/docs/cli/vela_svc.md index 1f46354e9..2d1a656aa 100644 --- a/docs/cli/vela_svc.md +++ b/docs/cli/vela_svc.md @@ -23,7 +23,6 @@ Manage services * [vela](vela.md) - * [vela svc deploy](vela_svc_deploy.md) - Initialize and run a service -* [vela svc show](vela_svc_show.md) - show service details * [vela svc status](vela_svc_status.md) - get status of a service ###### Auto generated by spf13/cobra on 30-Oct-2020 diff --git a/docs/cli/vela_svc_show.md b/docs/cli/vela_svc_show.md deleted file mode 100644 index 805573d33..000000000 --- a/docs/cli/vela_svc_show.md +++ /dev/null @@ -1,36 +0,0 @@ -## vela svc show - -show service details - -### Synopsis - -show service details, including arguments of workload and traits - -``` -vela svc show [flags] -``` - -### Examples - -``` -vela svc show -``` - -### Options - -``` - -h, --help help for show -``` - -### Options inherited from parent commands - -``` - -a, --app string specify the name of application containing the services - -e, --env string specify environment name for application -``` - -### SEE ALSO - -* [vela svc](vela_svc.md) - Manage services - -###### Auto generated by spf13/cobra on 30-Oct-2020 From 0a3b3ffdb1eaff2e8a4b2ae8c86acabbc25031f3 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Sun, 1 Nov 2020 10:02:49 +0800 Subject: [PATCH 3/3] address Wonderflow's offline advice --- e2e/application/application_test.go | 2 +- e2e/commonContext.go | 2 +- pkg/commands/show.go | 56 +++++++++++++++++------------ pkg/oam/application.go | 7 ++++ 4 files changed, 43 insertions(+), 24 deletions(-) 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