From 5b5c3b68bd2fc63ba7c1e50555498fab02d0a4cd Mon Sep 17 00:00:00 2001 From: TonyAdo <71679464+adohe@users.noreply.github.com> Date: Tue, 16 Apr 2024 10:28:29 +0800 Subject: [PATCH] feat: generate cmd use terminal ui framework (#1035) --- pkg/cmd/cmd.go | 8 +++++++- pkg/cmd/generate/generate.go | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index ab775cb3..e2b1f0a7 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -1,8 +1,10 @@ package cmd import ( + "context" "os" + "github.com/hashicorp/waypoint-plugin-sdk/terminal" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericiooptions" cliflag "k8s.io/component-base/cli/flag" @@ -24,6 +26,9 @@ import ( type KusionctlOptions struct { Arguments []string + // UI is used to write to the CLI. + UI terminal.UI + genericiooptions.IOStreams } @@ -31,6 +36,7 @@ type KusionctlOptions struct { func NewDefaultKusionctlCommand() *cobra.Command { return NewDefaultKusionctlCommandWithArgs(KusionctlOptions{ Arguments: os.Args, + UI: terminal.ConsoleUI(context.Background()), IOStreams: genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}, }) } @@ -94,7 +100,7 @@ func NewKusionctlCmd(o KusionctlOptions) *cobra.Command { workspace.NewCmd(), cmdinit.NewCmd(), config.NewCmd(), - generate.NewCmdGenerate(o.IOStreams), + generate.NewCmdGenerate(o.UI, o.IOStreams), }, }, { diff --git a/pkg/cmd/generate/generate.go b/pkg/cmd/generate/generate.go index d2a1a97e..e5552e03 100644 --- a/pkg/cmd/generate/generate.go +++ b/pkg/cmd/generate/generate.go @@ -21,6 +21,7 @@ import ( "os" "strings" + "github.com/hashicorp/waypoint-plugin-sdk/terminal" "github.com/pterm/pterm" "github.com/spf13/cobra" yamlv3 "gopkg.in/yaml.v3" @@ -59,6 +60,8 @@ type GenerateFlags struct { Output string Values []string + UI terminal.UI + genericiooptions.IOStreams } @@ -69,20 +72,23 @@ type GenerateOptions struct { Output string Values []string + UI terminal.UI + genericiooptions.IOStreams } // NewGenerateFlags returns a default GenerateFlags -func NewGenerateFlags(streams genericiooptions.IOStreams) *GenerateFlags { +func NewGenerateFlags(ui terminal.UI, streams genericiooptions.IOStreams) *GenerateFlags { return &GenerateFlags{ MetaFlags: meta.NewMetaFlags(), + UI: ui, IOStreams: streams, } } // NewCmdGenerate creates the `generate` command. -func NewCmdGenerate(ioStreams genericiooptions.IOStreams) *cobra.Command { - flags := NewGenerateFlags(ioStreams) +func NewCmdGenerate(ui terminal.UI, ioStreams genericiooptions.IOStreams) *cobra.Command { + flags := NewGenerateFlags(ui, ioStreams) cmd := &cobra.Command{ Use: "generate", @@ -126,6 +132,7 @@ func (flags *GenerateFlags) ToOptions() (*GenerateOptions, error) { Output: flags.Output, Values: flags.Values, + UI: flags.UI, IOStreams: flags.IOStreams, } @@ -153,13 +160,19 @@ func (o *GenerateOptions) Run() error { parameters := o.buildParameters() // call default generator to generate Spec + o.UI.Output("Generating...", terminal.WithHeaderStyle()) spec, err := GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, parameters, true) if err != nil { return err } // write Spec to output file or a writer - return write(spec, o.Output, o.Out) + err = write(spec, o.Output, o.Out) + if err != nil { + o.UI.Output("Error writing generated Spec: %s", err.Error(), terminal.WithErrorStyle()) + return err + } + return nil } // buildParameters builds parameters with given values.