diff --git a/cmd/vela/main.go b/cmd/vela/main.go index 5d31a7724..c64bb9310 100644 --- a/cmd/vela/main.go +++ b/cmd/vela/main.go @@ -107,6 +107,7 @@ func newCommand() *cobra.Command { cmd.NewAppsCommand(commandArgs, ioStream), cmd.NewDeleteCommand(commandArgs, ioStream, os.Args[1:]), cmd.NewAppStatusCommand(commandArgs, ioStream), + cmd.NewAppShowCommand(commandArgs, ioStream), // Others cmd.NewAddonConfigCommand(ioStream), diff --git a/pkg/cmd/app_show.go b/pkg/cmd/app_show.go new file mode 100644 index 000000000..52052707b --- /dev/null +++ b/pkg/cmd/app_show.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/cloud-native-application/rudrx/api/types" + cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util" + corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" + + "github.com/spf13/cobra" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func NewAppShowCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command { + ctx := context.Background() + cmd := &cobra.Command{ + Use: "app:show", + Short: "get detail spec of your app", + Long: "get detail spec of your app, including its workload and trait", + Example: `vela app:show `, + RunE: func(cmd *cobra.Command, args []string) error { + argsLength := len(args) + if argsLength == 0 { + ioStreams.Errorf("Hint: please specify an application") + os.Exit(1) + } + appName := args[0] + env, err := GetEnv() + if err != nil { + ioStreams.Errorf("Error: failed to get Env: %s", err) + return err + } + + newClient, err := client.New(c.Config, client.Options{Scheme: c.Schema}) + if err != nil { + return err + } + + return printApplication(ctx, newClient, cmd, env, appName) + }, + } + cmd.SetOut(ioStreams.Out) + return cmd +} + +func printApplication(ctx context.Context, c client.Client, cmd *cobra.Command, env *types.EnvMeta, appName string) error { + var application corev1alpha2.ApplicationConfiguration + + if err := c.Get(ctx, client.ObjectKey{Name: appName, Namespace: env.Namespace}, &application); err != nil { + return fmt.Errorf("Fetch application with Err: %s", err) + } + + workload, err := cmdutil.GetWorkloadDefinitionByName(context.TODO(), c, env.Namespace, appName) + if err != nil { + return fmt.Errorf("Fetch WorkloadDefinitionByName with Err: %s", err) + } + + traitDefinitions := cmdutil.ListTraitDefinitionsByApplicationConfiguration(application) + + cmd.Println("About:") + cmd.Println(workload.Name) + cmd.Println(len(traitDefinitions)) + return nil +}