Skip to content

Commit

Permalink
feat: update kusion tui implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 committed Apr 23, 2024
1 parent e77a5fd commit ec41d75
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 176 deletions.
15 changes: 0 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module kusionstack.io/kusion
go 1.22.1

require (
github.com/AlecAivazis/survey/v2 v2.3.4
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Azure/go-autorest/autorest/azure/auth v0.5.12
Expand Down Expand Up @@ -93,7 +92,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 +106,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 +126,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 +239,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 All @@ -260,7 +247,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
Expand All @@ -271,7 +257,6 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand Down
49 changes: 0 additions & 49 deletions go.sum

Large diffs are not rendered by default.

40 changes: 18 additions & 22 deletions pkg/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"
"sync"

"github.com/AlecAivazis/survey/v2"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
Expand Down Expand Up @@ -92,16 +91,16 @@ type ApplyOptions struct {
}

// NewApplyFlags returns a default ApplyFlags
func NewApplyFlags(streams genericiooptions.IOStreams) *ApplyFlags {
func NewApplyFlags(ui *pretty.UI, streams genericiooptions.IOStreams) *ApplyFlags {
return &ApplyFlags{
PreviewFlags: preview.NewPreviewFlags(streams),
PreviewFlags: preview.NewPreviewFlags(ui, streams),
IOStreams: streams,
}
}

// NewCmdApply creates the `apply` command.
func NewCmdApply(ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewApplyFlags(ioStreams)
func NewCmdApply(ui *pretty.UI, ioStreams genericiooptions.IOStreams) *cobra.Command {
flags := NewApplyFlags(ui, ioStreams)

cmd := &cobra.Command{
Use: "apply",
Expand Down Expand Up @@ -172,11 +171,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.UI, o.NoStyle)
if err != nil {
return err
}
Expand All @@ -200,7 +198,7 @@ func (o *ApplyOptions) Run() error {
}

// summary preview table
changes.Summary(o.IOStreams.Out, false)
changes.Summary(o.IOStreams.Out, o.NoStyle)

// detail detection
if o.Detail && o.All {
Expand All @@ -213,14 +211,14 @@ func (o *ApplyOptions) Run() error {
// prompt
if !o.Yes {
for {
input, err := prompt()
input, err := prompt(o.UI)
if err != nil {
return err
}
if input == "yes" {
break
} else if input == "details" {
target, err := changes.PromptDetails()
target, err := changes.PromptDetails(o.UI)
if err != nil {
return err
}
Expand Down Expand Up @@ -303,10 +301,11 @@ func Apply(
var ls lineSummary

// progress bar, print dag walk detail
progressbar, err := pterm.DefaultProgressbar.
progressbar, err := o.UI.ProgressbarPrinter.
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 Expand Up @@ -517,21 +516,18 @@ func allUnChange(changes *models.Changes) bool {
return true
}

func prompt() (string, error) {
func prompt(ui *pretty.UI) (string, error) {
// don`t display yes item when only preview
options := []string{"yes", "details", "no"}

p := &survey.Select{
Message: `Do you want to apply these diffs?`,
Options: options,
Default: "details",
}

var input string
err := survey.AskOne(p, &input)
input, err := ui.InteractiveSelectPrinter.
WithDefaultText(`Do you want to apply these diffs?`).
WithOptions(options).
WithDefaultOption("details").
Show()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
fmt.Printf("Prompt failed: %v\n", err)
return "", err
}

return input, nil
}
17 changes: 8 additions & 9 deletions pkg/cmd/apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"

"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 All @@ -39,6 +38,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/engine/runtime/kubernetes"
statestorages "kusionstack.io/kusion/pkg/engine/state/storages"
"kusionstack.io/kusion/pkg/util/pretty"
workspacestorages "kusionstack.io/kusion/pkg/workspace/storages"
)

Expand Down Expand Up @@ -72,6 +72,7 @@ func NewApplyOptions() *ApplyOptions {
NoStyle: false,
Output: "",
IgnoreFields: nil,
UI: pretty.DefaultUI(),
},
}
}
Expand Down Expand Up @@ -112,6 +113,7 @@ func mockGenerateSpecWithSpinner() {
stack *apiv1.Stack,
workspace *apiv1.Workspace,
parameters map[string]string,
ui *pretty.UI,
noStyle bool,
) (*apiv1.Spec, error) {
return &apiv1.Spec{Resources: []apiv1.Resource{sa1, sa2, sa3}}, nil
Expand Down Expand Up @@ -317,21 +319,18 @@ func mockOperationApply(res models.OpResult) {

func TestPrompt(t *testing.T) {
mockey.PatchConvey("prompt error", t, func() {
mockey.Mock(survey.AskOne).Return(errors.New("mock error")).Build()
_, err := prompt()
mockey.Mock((*pterm.InteractiveSelectPrinter).Show).Return("", errors.New("mock error")).Build()
_, err := prompt(pretty.DefaultUI())
assert.NotNil(t, err)
})

mockey.PatchConvey("prompt yes", t, func() {
mockPromptOutput("yes")
_, err := prompt()
_, err := prompt(pretty.DefaultUI())
assert.Nil(t, err)
})
}

func mockPromptOutput(res string) {
mockey.Mock(survey.AskOne).To(func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
reflect.ValueOf(response).Elem().Set(reflect.ValueOf(res))
return nil
}).Build()
mockey.Mock((*pterm.InteractiveSelectPrinter).Show).Return(res, nil).Build()
}
13 changes: 6 additions & 7 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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 +22,14 @@ 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
UI *pretty.UI

genericiooptions.IOStreams
}
Expand All @@ -39,7 +38,7 @@ type KusionctlOptions struct {
func NewDefaultKusionctlCommand() *cobra.Command {
return NewDefaultKusionctlCommandWithArgs(KusionctlOptions{
Arguments: os.Args,
UI: terminal.ConsoleUI(context.Background()),
UI: pretty.DefaultUI(),
IOStreams: genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
})
}
Expand Down Expand Up @@ -117,9 +116,9 @@ func NewKusionctlCmd(o KusionctlOptions) *cobra.Command {
{
Message: "Runtime Commands:",
Commands: []*cobra.Command{
preview.NewCmdPreview(o.IOStreams),
apply.NewCmdApply(o.IOStreams),
destroy.NewCmdDestroy(o.IOStreams),
preview.NewCmdPreview(o.UI, o.IOStreams),
apply.NewCmdApply(o.UI, o.IOStreams),
destroy.NewCmdDestroy(o.UI, o.IOStreams),
},
},
{
Expand Down
Loading

0 comments on commit ec41d75

Please sign in to comment.