Skip to content

Commit

Permalink
refactor: delete io.writer from func Preview() and add to Summary()
Browse files Browse the repository at this point in the history
  • Loading branch information
howieyuen committed Sep 5, 2022
1 parent 66cc2fa commit d8c0b3e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
4 changes: 3 additions & 1 deletion pkg/engine/operation/models/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"bytes"
"fmt"
"io"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -158,7 +159,7 @@ func (p *Changes) AllUnChange() bool {
return true
}

func (p *Changes) Summary() {
func (p *Changes) Summary(writer io.Writer) {
// Create a fork of the default table, fill it with data and print it.
// Data can also be generated and inserted later.
tableHeader := []string{fmt.Sprintf("Stack: %s", p.stack.Name), "ID", "Action"}
Expand All @@ -179,6 +180,7 @@ func (p *Changes) Summary() {
WithLeftAlignment(true).
WithSeparator(" ").
WithData(tableData).
WithWriter(writer).
Render()
pterm.Println() // Blank line
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/engine/operation/models/change_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

import (
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -432,7 +433,7 @@ func TestChanges_Preview(t *testing.T) {
project: tt.fields.project,
stack: tt.fields.stack,
}
p.Summary()
p.Summary(os.Stdout)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kusionctl/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (o *ApplyOptions) Run() error {
return err
}

changes, err := previewcmd.Preview(&o.PreviewOptions, runtime, stateStorage, planResources, project, stack, os.Stdout)
changes, err := previewcmd.Preview(&o.PreviewOptions, runtime, stateStorage, planResources, project, stack)
if err != nil {
return err
}
Expand All @@ -100,7 +100,7 @@ func (o *ApplyOptions) Run() error {
}

// Summary preview table
changes.Summary()
changes.Summary(os.Stdout)

// Detail detection
if o.Detail && !o.Yes {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kusionctl/cmd/destroy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package destroy

import (
"fmt"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -88,7 +89,7 @@ func (o *DestroyOptions) Run() error {
}

// Preview
changes.Summary()
changes.Summary(os.Stdout)

// Detail detection
if o.Detail {
Expand Down
10 changes: 3 additions & 7 deletions pkg/kusionctl/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package preview

import (
"fmt"
"io"
"os"

"github.com/pterm/pterm"
Expand Down Expand Up @@ -77,7 +76,7 @@ func (o *PreviewOptions) Run() error {
return err
}

changes, err := Preview(o, r, stateStorage, planResources, project, stack, os.Stdout)
changes, err := Preview(o, r, stateStorage, planResources, project, stack)
if err != nil {
return err
}
Expand All @@ -88,7 +87,7 @@ func (o *PreviewOptions) Run() error {
}

// Summary preview table
changes.Summary()
changes.Summary(os.Stdout)

// Detail detection
if o.Detail {
Expand Down Expand Up @@ -116,7 +115,7 @@ func (o *PreviewOptions) Run() error {
//
// Example:
//
// o := NewApplyOptions()
// o := NewPreviewOptions()
// stateStorage := &states.FileSystemState{
// Path: filepath.Join(o.WorkDir, states.KusionState)
// }
Expand All @@ -130,16 +129,13 @@ func (o *PreviewOptions) Run() error {
// if err != nil {
// return err
// }
//
// todo @elliotxx io.Writer is not used now
func Preview(
o *PreviewOptions,
runtime runtime.Runtime,
storage states.StateStorage,
planResources *models.Spec,
project *projectstack.Project,
stack *projectstack.Stack,
out io.Writer,
) (*opsmodels.Changes, error) {
log.Info("Start compute preview changes ...")

Expand Down
3 changes: 1 addition & 2 deletions pkg/kusionctl/cmd/preview/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package preview

import (
"context"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -52,7 +51,7 @@ func Test_preview(t *testing.T) {
mockOperationPreview()

o := NewPreviewOptions()
_, err := Preview(o, &fooRuntime{}, stateStorage, &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, project, stack, os.Stdout)
_, err := Preview(o, &fooRuntime{}, stateStorage, &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, project, stack)
assert.Nil(t, err)
})
}
Expand Down

0 comments on commit d8c0b3e

Please sign in to comment.