Merge pull request #478 from zzxwill/app-show

Refactor `vela show`
This commit is contained in:
Zheng Xi Zhou
2020-11-01 11:44:04 +08:00
committed by GitHub
12 changed files with 94 additions and 111 deletions
+1
View File
@@ -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
-1
View File
@@ -28,7 +28,6 @@ vela app <command>
* [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
@@ -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 <APPLICATION-NAME> [flags]
vela show <APPLICATION-NAME> [flags]
```
### Examples
```
vela app show <APPLICATION-NAME>
vela show <APPLICATION-NAME>
```
### 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 <APPLICATION-NAME>
### SEE ALSO
* [vela app](vela_app.md) - Manage applications
* [vela](vela.md) -
###### Auto generated by spf13/cobra on 30-Oct-2020
-1
View File
@@ -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
-36
View File
@@ -1,36 +0,0 @@
## vela svc show
show service details
### Synopsis
show service details, including arguments of workload and traits
```
vela svc show <SERVICE_NAME> [flags]
```
### Examples
```
vela svc show <SERVICE_NAME>
```
### 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
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -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
-2
View File
@@ -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
}
+2
View File
@@ -72,6 +72,8 @@ func NewCommand() *cobra.Command {
NewAppsCommand(commandArgs, ioStream),
NewListCommand(commandArgs, ioStream),
NewDeleteCommand(commandArgs, ioStream),
NewAppShowCommand(ioStream),
// Workloads
AddCompCommands(commandArgs, ioStream),
-1
View File
@@ -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
+74 -61
View File
@@ -3,8 +3,10 @@ package commands
import (
"fmt"
"os"
"strings"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/AlecAivazis/survey/v2"
"github.com/gosuri/uitable"
"github.com/oam-dev/kubevela/api/types"
"github.com/oam-dev/kubevela/pkg/application"
@@ -15,9 +17,9 @@ import (
func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "show <APPLICATION-NAME>",
Short: "get details of your app",
Long: "get details of your app, including its workload and trait",
Example: `vela app show <APPLICATION-NAME>`,
Short: "Get details of an application",
Long: "Get details of an application",
Example: `vela show <APPLICATION-NAME>`,
RunE: func(cmd *cobra.Command, args []string) error {
argsLength := len(args)
if argsLength == 0 {
@@ -37,6 +39,7 @@ func NewAppShowCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
types.TagCommandType: types.TypeApp,
},
}
cmd.Flags().StringP("svc", "s", "", "service name")
cmd.SetOut(ioStreams.Out)
return cmd
}
@@ -47,6 +50,45 @@ func showApplication(cmd *cobra.Command, env *types.EnvMeta, appName string) err
return err
}
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 svcFlag == svcName {
svcFlagStatus = oam.FlagIsValid
targetServices = append(targetServices, svcName)
}
}
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")
table := uitable.New()
table.AddRow(" Name:", appName)
@@ -62,56 +104,16 @@ 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 targetServices {
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 <SERVICE_NAME>",
Short: "show service details",
Long: "show service details, including arguments of workload and traits",
Example: `vela svc show <SERVICE_NAME>`,
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
@@ -128,37 +130,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, oam.DefaultChosenAllSvc)
prompt := &survey.Select{
Message: "Please choose one service: ",
Options: services,
Default: oam.DefaultChosenAllSvc,
}
err := survey.AskOne(prompt, &svcName)
if err != nil {
return "", fmt.Errorf("failed to retrieve services of the application, err %v", err)
}
return svcName, nil
}
+7
View File
@@ -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