Skip to content

Commit

Permalink
do not show generating hint in the command preview (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan committed Jul 19, 2023
1 parent 3c2fb72 commit c219564
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
20 changes: 11 additions & 9 deletions pkg/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,34 @@ func (o *PreviewOptions) Validate() error {

func (o *PreviewOptions) Run() error {
// Set no style
if o.NoStyle {
if o.NoStyle || o.Output == jsonOutput {
pterm.DisableStyling()
pterm.DisableColor()
}
if o.Output == jsonOutput {
pterm.DisableStyling()
pterm.DisableColor()
}

// Parse project and stack of work directory
project, stack, err := projectstack.DetectProjectAndStack(o.WorkDir)
if err != nil {
return err
}

// Get compile result
sp, err := spec.GenerateSpecWithSpinner(&generator.Options{
options := &generator.Options{
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Settings: o.Settings,
Arguments: o.Arguments,
Overrides: o.Overrides,
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
NoStyle: o.NoStyle || o.Output == jsonOutput,
}, project, stack)
NoStyle: o.NoStyle,
}

var sp *models.Spec
if o.Output == jsonOutput {
sp, err = spec.GenerateSpec(options, project, stack)
} else {
sp, err = spec.GenerateSpecWithSpinner(options, project, stack)
}
if err != nil {
return err
}
Expand Down
18 changes: 14 additions & 4 deletions pkg/cmd/preview/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestPreviewOptions_Run(t *testing.T) {
t.Run("no changes", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockGenerateSpecWithSpinner()
mockNewKubernetesRuntime()

o := NewPreviewOptions()
Expand All @@ -93,7 +93,7 @@ func TestPreviewOptions_Run(t *testing.T) {
t.Run("detail is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockGenerateSpecWithSpinner()
mockNewKubernetesRuntime()
mockOperationPreview()
mockPromptDetail("")
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestPreviewOptions_Run(t *testing.T) {
t.Run("no style is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockGenerateSpecWithSpinner()
mockNewKubernetesRuntime()
mockOperationPreview()
mockPromptDetail("")
Expand Down Expand Up @@ -219,7 +219,7 @@ func mockDetectProjectAndStack() {
})
}

func mockGenerateSpec() {
func mockGenerateSpecWithSpinner() {
monkey.Patch(spec.GenerateSpecWithSpinner, func(
o *generator.Options,
project *projectstack.Project,
Expand All @@ -229,6 +229,16 @@ func mockGenerateSpec() {
})
}

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

func mockNewKubernetesRuntime() {
monkey.Patch(kubernetes.NewKubernetesRuntime, func() (runtime.Runtime, error) {
return &fooRuntime{}, nil
Expand Down
45 changes: 26 additions & 19 deletions pkg/cmd/spec/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ func GenerateSpecWithSpinner(o *generator.Options, project *projectstack.Project
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 := !o.NoStyle && sp != nil

spec, err := GenerateSpec(o, project, stack)
// failed
if err != nil {
if style {
sp.Fail()
return nil, err
} else {
// TODO: we will replace this implementation with KCL no-style flag when it is supported
return nil, errors.New(stripansi.Strip(err.Error()))
}
}

// success
if style {
sp.Success()
} else {
fmt.Println()
}
return spec, nil
}

func GenerateSpec(o *generator.Options, project *projectstack.Project, stack *projectstack.Stack) (*models.Spec, error) {
// Choose the generator
var g generator.Generator
pg := project.Generator
Expand All @@ -43,25 +68,7 @@ func GenerateSpecWithSpinner(o *generator.Options, project *projectstack.Project

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

// TODO: we will replace this implementation with KCL no-style flag
// when it is supported
if o.NoStyle {
return nil, errors.New(stripansi.Strip(err.Error()))
}

return nil, err
return nil, errors.New(stripansi.Strip(err.Error()))
}

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

return spec, nil
}

0 comments on commit c219564

Please sign in to comment.