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: refine no-style and remove no-prompt command option #367

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/a8m/envsubst v1.3.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/agext/levenshtein v1.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/a8m/envsubst v1.3.0 h1:GmXKmVssap0YtlU3E230W98RWtWCyIZzjtf1apWWyAg=
github.com/a8m/envsubst v1.3.0/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGtlsbY=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *ApplyOptions) Run() error {
// Set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.EnableColor()
pterm.DisableColor()
}

// Parse project and stack of work directory
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func NewCmdCompile() *cobra.Command {
i18n.T("Disable dumping None values"))
cmd.Flags().BoolVarP(&o.OverrideAST, "override-AST", "a", false,
i18n.T("Specify the override option"))
cmd.Flags().BoolVarP(&o.NoStyle, "no-style", "s", false,
i18n.T("Disable the output style and color"))

return cmd
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/compile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

"github.com/pterm/pterm"
yamlv2 "gopkg.in/yaml.v2"

"kusionstack.io/kusion/pkg/cmd/spec"
Expand All @@ -26,6 +27,7 @@ type CompileFlags struct {
Overrides []string
DisableNone bool
OverrideAST bool
NoStyle bool
}

const Stdout = "stdout"
Expand Down Expand Up @@ -60,6 +62,12 @@ func (o *CompileOptions) Validate() error {
}

func (o *CompileOptions) Run() error {
// Set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.DisableColor()
}

// Parse project and stack of work directory
project, stack, err := projectstack.DetectProjectAndStack(o.WorkDir)
if err != nil {
Expand All @@ -74,6 +82,7 @@ func (o *CompileOptions) Run() error {
Overrides: o.Overrides,
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
NoStyle: o.NoStyle,
}, project, stack)
if err != nil {
// only print err in the check command
Expand Down
83 changes: 83 additions & 0 deletions pkg/cmd/compile/options_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
package compile

import (
"io/fs"
"os"
"testing"

"bou.ke/monkey"
"github.com/stretchr/testify/assert"
"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/engine"
"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/generator"
"kusionstack.io/kusion/pkg/projectstack"
)

var (
apiVersion = "v1"
kind = "ServiceAccount"
namespace = "test-ns"

project = &projectstack.Project{
ProjectConfiguration: projectstack.ProjectConfiguration{
Name: "testdata",
Tenant: "admin",
},
}
stack = &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{
Name: "dev",
},
}

sa1 = newSA("sa1")
sa2 = newSA("sa2")
sa3 = newSA("sa3")
)

func TestCompileOptions_preSet(t *testing.T) {
Expand Down Expand Up @@ -52,3 +82,56 @@ func TestCompileOptions_preSet(t *testing.T) {
})
}
}

func TestCompileOptions_Run(t *testing.T) {
defer func() {
os.Remove("kusion_state.json")
}()

t.Run("no style is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockWriteFile()

o := NewCompileOptions()
o.NoStyle = true
err := o.Run()
assert.Nil(t, err)
})
}

func newSA(name string) models.Resource {
return models.Resource{
ID: engine.BuildID(apiVersion, kind, namespace, name),
Type: "Kubernetes",
Attributes: map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
},
}
}

func mockDetectProjectAndStack() {
monkey.Patch(projectstack.DetectProjectAndStack, func(stackDir string) (*projectstack.Project, *projectstack.Stack, error) {
project.Path = stackDir
stack.Path = stackDir
return project, stack, nil
})
}

func mockGenerateSpec() {
monkey.Patch(spec.GenerateSpecWithSpinner, func(o *generator.Options, project *projectstack.Project, stack *projectstack.Stack) (*models.Spec, error) {
return &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, nil
})
}

func mockWriteFile() {
monkey.Patch(os.WriteFile, func(name string, data []byte, perm fs.FileMode) error {
return nil
})
}
5 changes: 2 additions & 3 deletions pkg/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (o *PreviewOptions) Run() error {
// Set no style
if o.NoStyle {
pterm.DisableStyling()
pterm.EnableColor()
pterm.DisableColor()
}
if o.Output == jsonOutput {
pterm.DisableStyling()
Expand All @@ -86,8 +86,7 @@ func (o *PreviewOptions) Run() error {
Overrides: o.Overrides,
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
NoStyle: o.NoStyle,
NoPrompt: o.Output == jsonOutput,
NoStyle: o.NoStyle || o.Output == jsonOutput,
}, project, stack)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions pkg/cmd/preview/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ func TestPreviewOptions_Run(t *testing.T) {
err := o.Run()
assert.Nil(t, err)
})

t.Run("no style is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockNewKubernetesRuntime()
mockOperationPreview()
mockPromptDetail("")

o := NewPreviewOptions()
o.NoStyle = true
err := o.Run()
assert.Nil(t, err)
})
}

type fooRuntime struct{}
Expand Down
18 changes: 12 additions & 6 deletions pkg/cmd/spec/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package spec
import (
"fmt"

"github.com/acarl005/stripansi"
"github.com/pterm/pterm"
"sigs.k8s.io/kustomize/kyaml/errors"
liu-hm19 marked this conversation as resolved.
Show resolved Hide resolved

"kusionstack.io/kusion/pkg/engine/models"
"kusionstack.io/kusion/pkg/generator"
Expand All @@ -14,10 +16,9 @@ import (

func GenerateSpecWithSpinner(o *generator.Options, project *projectstack.Project, stack *projectstack.Stack) (*models.Spec, error) {
var sp *pterm.SpinnerPrinter
if !o.NoPrompt && o.NoStyle {
if o.NoStyle {
fmt.Printf("Generating Spec in the Stack %s...\n", stack.Name)
}
if !o.NoPrompt && !o.NoStyle {
} else {
sp = &pretty.SpinnerT
sp, _ = sp.Start(fmt.Sprintf("Generating Spec in the Stack %s...", stack.Name))
}
Expand All @@ -42,16 +43,21 @@ func GenerateSpecWithSpinner(o *generator.Options, project *projectstack.Project

spec, err := g.GenerateSpec(o, stack)
if err != nil {
if !o.NoPrompt && sp != nil {
if !o.NoStyle && sp != nil {
sp.Fail()
}

if o.NoStyle {
return nil, errors.Wrap(stripansi.Strip(err.Error()))
liu-hm19 marked this conversation as resolved.
Show resolved Hide resolved
}

return nil, err
}

if !o.NoPrompt && sp != nil {
if !o.NoStyle && sp != nil {
sp.Success()
}
if !o.NoPrompt {
if !o.NoStyle {
fmt.Println()
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,4 @@ type Options struct {

// NoStyle represents whether to turn on the spinner output style
NoStyle bool

// NoPrompt represents whether to print prompt or not
NoPrompt bool
}
Loading