Skip to content

Commit

Permalink
feat: generate cmd use terminal ui framework (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Apr 16, 2024
1 parent 4a08b0f commit 5b5c3b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -24,13 +26,17 @@ import (
type KusionctlOptions struct {
Arguments []string

// UI is used to write to the CLI.
UI terminal.UI

genericiooptions.IOStreams
}

// NewDefaultKusionctlCommand creates the `kusionctl` command with default arguments
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},
})
}
Expand Down Expand Up @@ -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),
},
},
{
Expand Down
21 changes: 17 additions & 4 deletions pkg/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -59,6 +60,8 @@ type GenerateFlags struct {
Output string
Values []string

UI terminal.UI

genericiooptions.IOStreams
}

Expand All @@ -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",
Expand Down Expand Up @@ -126,6 +132,7 @@ func (flags *GenerateFlags) ToOptions() (*GenerateOptions, error) {
Output: flags.Output,
Values: flags.Values,

UI: flags.UI,
IOStreams: flags.IOStreams,
}

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5b5c3b6

Please sign in to comment.