Skip to content

Commit

Permalink
feat: update implementation of terminal ui for kusion
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 committed Apr 22, 2024
1 parent e77a5fd commit 9de105e
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 106 deletions.
12 changes: 0 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ require (
cloud.google.com/go/storage v1.36.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/hcsshim v0.11.0 // indirect
github.com/VividCortex/ewma v1.1.1 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/alibabacloud-go/darabonba-array v0.1.0 // indirect
github.com/alibabacloud-go/darabonba-encode-util v0.0.2 // indirect
Expand All @@ -108,9 +107,6 @@ require (
github.com/aliyun/alibabacloud-dkms-gcs-go-sdk v0.5.1 // indirect
github.com/aliyun/alibabacloud-dkms-transfer-go-sdk v0.1.8 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/briandowns/spinner v1.11.1 // indirect
github.com/cheggaaa/pb/v3 v3.0.5 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
Expand All @@ -131,25 +127,18 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lab47/vterm v0.0.0-20201001232628-a9dd795f94c2 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mitchellh/go-glint v0.0.0-20201015034436-f80573c636de // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/tj/go-spin v1.1.0 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/vbatts/tar-split v0.11.3 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/y0ssar1an/q v1.0.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
Expand Down Expand Up @@ -251,7 +240,6 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hc-install v0.6.2
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/waypoint-plugin-sdk v0.0.0-20230412210808-dcdb2a03f714
github.com/imdario/mergo v0.3.16
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
Expand Down
36 changes: 0 additions & 36 deletions go.sum

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions pkg/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ type ApplyOptions struct {
}

// NewApplyFlags returns a default ApplyFlags
func NewApplyFlags(streams genericiooptions.IOStreams) *ApplyFlags {
func NewApplyFlags(sp *pterm.SpinnerPrinter, streams genericiooptions.IOStreams) *ApplyFlags {
return &ApplyFlags{
PreviewFlags: preview.NewPreviewFlags(streams),
PreviewFlags: preview.NewPreviewFlags(sp, streams),
IOStreams: streams,
}
}

// NewCmdApply creates the `apply` command.
func NewCmdApply(ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewApplyFlags(ioStreams)
func NewCmdApply(sp *pterm.SpinnerPrinter, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewApplyFlags(sp, ioStreams)

cmd := &cobra.Command{
Use: "apply",
Expand Down Expand Up @@ -172,11 +172,10 @@ func (o *ApplyOptions) Run() error {
// set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.DisableColor()
}

// Generate Spec
spec, err := generate.GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, nil, o.NoStyle)
spec, err := generate.GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, nil, o.SpinnerPrinter, o.NoStyle)
if err != nil {
return err
}
Expand Down Expand Up @@ -307,6 +306,7 @@ func Apply(
WithMaxWidth(0). // Set to 0, the terminal width will be used
WithTotal(len(changes.StepKeys)).
WithWriter(out).
WithRemoveWhenDone().
Start()
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/bytedance/mockey"
"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"

apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
Expand Down Expand Up @@ -112,6 +113,7 @@ func mockGenerateSpecWithSpinner() {
stack *apiv1.Stack,
workspace *apiv1.Workspace,
parameters map[string]string,
sp *pterm.SpinnerPrinter,
noStyle bool,
) (*apiv1.Spec, error) {
return &apiv1.Spec{Resources: []apiv1.Resource{sa1, sa2, sa3}}, nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cmd

import (
"context"
"os"

"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
cliflag "k8s.io/component-base/cli/flag"
Expand All @@ -24,23 +23,24 @@ import (
"kusionstack.io/kusion/pkg/cmd/version"
"kusionstack.io/kusion/pkg/cmd/workspace"
"kusionstack.io/kusion/pkg/util/i18n"
"kusionstack.io/kusion/pkg/util/pretty"
)

type KusionctlOptions struct {
Arguments []string

// UI is used to write to the CLI.
UI terminal.UI
// SpinnerPrinter is used to display outputs through CLI progress.
SpinnerPrinter *pterm.SpinnerPrinter

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},
Arguments: os.Args,
SpinnerPrinter: &pretty.SpinnerT,
IOStreams: genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
})
}

Expand Down Expand Up @@ -111,14 +111,14 @@ func NewKusionctlCmd(o KusionctlOptions) *cobra.Command {
workspace.NewCmd(),
project.NewCmd(),
stack.NewCmd(),
generate.NewCmdGenerate(o.UI, o.IOStreams),
generate.NewCmdGenerate(o.SpinnerPrinter, o.IOStreams),
},
},
{
Message: "Runtime Commands:",
Commands: []*cobra.Command{
preview.NewCmdPreview(o.IOStreams),
apply.NewCmdApply(o.IOStreams),
preview.NewCmdPreview(o.SpinnerPrinter, o.IOStreams),
apply.NewCmdApply(o.SpinnerPrinter, o.IOStreams),
destroy.NewCmdDestroy(o.IOStreams),
},
},
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ func (o *DeleteOptions) destroy(planResources *apiv1.Spec, changes *models.Chang
var deleted int

// progress bar, print dag walk detail
progressbar, err := pterm.DefaultProgressbar.WithMaxWidth(0).WithTotal(len(changes.StepKeys)).Start()
progressbar, err := pterm.DefaultProgressbar.
WithMaxWidth(0).
WithTotal(len(changes.StepKeys)).
WithRemoveWhenDone().
Start()
if err != nil {
return err
}
Expand Down
52 changes: 28 additions & 24 deletions pkg/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"strings"

"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
yamlv3 "gopkg.in/yaml.v3"
Expand All @@ -33,7 +32,6 @@ import (
"kusionstack.io/kusion/pkg/engine/api/generate/generator"
"kusionstack.io/kusion/pkg/engine/api/generate/run"
"kusionstack.io/kusion/pkg/util/i18n"
"kusionstack.io/kusion/pkg/util/pretty"
)

var (
Expand All @@ -57,10 +55,11 @@ var (
type GenerateFlags struct {
MetaFlags *meta.MetaFlags

Output string
Values []string
Output string
Values []string
NoStyle bool

UI terminal.UI
SpinnerPrinter *pterm.SpinnerPrinter

genericiooptions.IOStreams
}
Expand All @@ -69,26 +68,27 @@ type GenerateFlags struct {
type GenerateOptions struct {
*meta.MetaOptions

Output string
Values []string
Output string
Values []string
NoStyle bool

UI terminal.UI
SpinnerPrinter *pterm.SpinnerPrinter

genericiooptions.IOStreams
}

// NewGenerateFlags returns a default GenerateFlags
func NewGenerateFlags(ui terminal.UI, streams genericiooptions.IOStreams) *GenerateFlags {
func NewGenerateFlags(sp *pterm.SpinnerPrinter, streams genericiooptions.IOStreams) *GenerateFlags {
return &GenerateFlags{
MetaFlags: meta.NewMetaFlags(),
UI: ui,
IOStreams: streams,
MetaFlags: meta.NewMetaFlags(),
SpinnerPrinter: sp,
IOStreams: streams,
}
}

// NewCmdGenerate creates the `generate` command.
func NewCmdGenerate(ui terminal.UI, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewGenerateFlags(ui, ioStreams)
func NewCmdGenerate(sp *pterm.SpinnerPrinter, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewGenerateFlags(sp, ioStreams)

cmd := &cobra.Command{
Use: "generate",
Expand Down Expand Up @@ -117,6 +117,7 @@ func (flags *GenerateFlags) AddFlags(cmd *cobra.Command) {

cmd.Flags().StringVarP(&flags.Output, "output", "o", flags.Output, i18n.T("File to write generated Spec resources to"))
cmd.Flags().StringArrayVar(&flags.Values, "set", []string{}, i18n.T("Set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"))
cmd.Flags().BoolVarP(&flags.NoStyle, "no-style", "", false, i18n.T("no-style sets to RawOutput mode and disables all of styling"))
}

// ToOptions converts from CLI inputs to runtime inputs.
Expand All @@ -131,9 +132,10 @@ func (flags *GenerateFlags) ToOptions() (*GenerateOptions, error) {
MetaOptions: metaOptions,
Output: flags.Output,
Values: flags.Values,
NoStyle: flags.NoStyle,

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

return o, nil
Expand All @@ -156,20 +158,24 @@ func (o *GenerateOptions) Validate(cmd *cobra.Command, args []string) error {

// Run executes the `generate` command.
func (o *GenerateOptions) Run() error {
// set no style
if o.NoStyle {
pterm.DisableStyling()
}

// build parameters
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)
spec, err := GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, parameters, o.SpinnerPrinter, o.NoStyle)
if err != nil {
return err
}

// write Spec to output file or a writer
err = write(spec, o.Output, o.Out)
if err != nil {
o.UI.Output("Error writing generated Spec: %s", err.Error(), terminal.WithErrorStyle())
// o.UI.Output("Error writing generated Spec: %s", err.Error(), terminal.WithErrorStyle())
return err
}
return nil
Expand All @@ -194,6 +200,7 @@ func GenerateSpecWithSpinner(
stack *v1.Stack,
workspace *v1.Workspace,
parameters map[string]string,
sp *pterm.SpinnerPrinter,
noStyle bool,
) (*v1.Spec, error) {
// Construct generator instance
Expand All @@ -204,13 +211,10 @@ func GenerateSpecWithSpinner(
Runner: &run.KPMRunner{},
}

var sp *pterm.SpinnerPrinter
if noStyle {
fmt.Printf("Generating Spec in the Stack %s...\n", stack.Name)
} else {
sp = &pretty.SpinnerT
sp, _ = sp.Start(fmt.Sprintf("Generating Spec in the Stack %s...", stack.Name))
pterm.DisableStyling()
}
sp, _ = sp.Start(fmt.Sprintf("Generating Spec in the Stack %s...", stack.Name))

// style means color and prompt here. Currently, sp will be nil only when o.NoStyle is true
style := !noStyle && sp != nil
Expand Down
35 changes: 20 additions & 15 deletions pkg/cmd/preview/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type PreviewFlags struct {
Output string
IgnoreFields []string

SpinnerPrinter *pterm.SpinnerPrinter

genericiooptions.IOStreams
}

Expand All @@ -91,20 +93,23 @@ type PreviewOptions struct {
Output string
IgnoreFields []string

SpinnerPrinter *pterm.SpinnerPrinter

genericiooptions.IOStreams
}

// NewPreviewFlags returns a default PreviewFlags
func NewPreviewFlags(streams genericiooptions.IOStreams) *PreviewFlags {
func NewPreviewFlags(sp *pterm.SpinnerPrinter, streams genericiooptions.IOStreams) *PreviewFlags {
return &PreviewFlags{
MetaFlags: meta.NewMetaFlags(),
IOStreams: streams,
MetaFlags: meta.NewMetaFlags(),
SpinnerPrinter: sp,
IOStreams: streams,
}
}

// NewCmdPreview creates the `preview` command.
func NewCmdPreview(ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewPreviewFlags(ioStreams)
func NewCmdPreview(sp *pterm.SpinnerPrinter, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewPreviewFlags(sp, ioStreams)

cmd := &cobra.Command{
Use: "preview",
Expand Down Expand Up @@ -148,14 +153,15 @@ func (f *PreviewFlags) ToOptions() (*PreviewOptions, error) {
}

o := &PreviewOptions{
MetaOptions: metaOptions,
Operator: f.Operator,
Detail: f.Detail,
All: f.All,
NoStyle: f.NoStyle,
Output: f.Output,
IgnoreFields: f.IgnoreFields,
IOStreams: f.IOStreams,
MetaOptions: metaOptions,
Operator: f.Operator,
Detail: f.Detail,
All: f.All,
NoStyle: f.NoStyle,
Output: f.Output,
IgnoreFields: f.IgnoreFields,
SpinnerPrinter: f.SpinnerPrinter,
IOStreams: f.IOStreams,
}

return o, nil
Expand All @@ -175,11 +181,10 @@ func (o *PreviewOptions) Run() error {
// set no style
if o.NoStyle || o.Output == jsonOutput {
pterm.DisableStyling()
pterm.DisableColor()
}

// Generate spec
spec, err := generate.GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, nil, o.NoStyle)
spec, err := generate.GenerateSpecWithSpinner(o.RefProject, o.RefStack, o.RefWorkspace, nil, o.SpinnerPrinter, o.NoStyle)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 9de105e

Please sign in to comment.