Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate cmd use terminal ui framework #1035

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading