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: introduce the new command kusion build and delete redundant commands #608

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ vendor/

# build
_build*
build/
build/bundles/
build/yamls/
build/charts/
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewCmdApply() *cobra.Command {
},
}

o.AddCompileFlags(cmd)
o.AddBuildFlags(cmd)
o.AddPreviewFlags(cmd)
o.AddBackendFlags(cmd)

Expand Down
14 changes: 5 additions & 9 deletions pkg/cmd/apply/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ func (o *Options) Run() error {
}

options := &generator.Options{
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Settings: o.Settings,
Arguments: o.Arguments,
Overrides: o.Overrides,
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
NoStyle: o.NoStyle,
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Arguments: o.Arguments,
NoStyle: o.NoStyle,
}

// Generate Intent
Expand Down
66 changes: 66 additions & 0 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package build

import (
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"

"kusionstack.io/kusion/pkg/cmd/util"
"kusionstack.io/kusion/pkg/util/i18n"
)

func NewCmdBuild() *cobra.Command {
var (
short = i18n.T(`Build Kusion models in a Stack to the Intent`)

long = i18n.T(`
Build Kusion models in a Stack to the Intent

The command must be executed in a Stack or by specifying a Stack directory with the -w flag.
You can provide a list of arguments to replace the placeholders defined in KCL,
and use the --output flag to output the built results to a file`)

example = i18n.T(`

# Build main.k with arguments
kusion build -D name=test -D age=18

# Build main.k with work directory
kusion build -w appops/demo/dev

# Build main.k and write result into output.yaml
kusion build -o output.yaml

# Build without output style and color
kusion build --no-style=true`)
)

o := NewBuildOptions()
cmd := &cobra.Command{
Use: "build",
Short: short,
Long: templates.LongDesc(long),
Example: templates.Examples(example),
RunE: func(_ *cobra.Command, args []string) (err error) {
defer util.RecoverErr(&err)
util.CheckErr(o.Complete(args))
util.CheckErr(o.Validate())
util.CheckErr(o.Run())
return
},
}

o.AddBuildFlags(cmd)
cmd.Flags().StringVarP(&o.Output, "output", "o", "",
i18n.T("Specify the output file"))
cmd.Flags().BoolVarP(&o.NoStyle, "no-style", "", false,
i18n.T("Disable the output style and color"))

return cmd
}

func (o *Options) AddBuildFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.WorkDir, "workdir", "w", "",
i18n.T("Specify the work directory"))
cmd.Flags().StringToStringVarP(&o.Arguments, "argument", "D", map[string]string{},
i18n.T("Specify the top-level argument"))
}
26 changes: 26 additions & 0 deletions pkg/cmd/build/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package build

import (
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
)

func TestNewCmdCompile(t *testing.T) {
m1 := mockey.Mock((*Options).Complete).To(func(o *Options, args []string) error {
o.Output = "stdout"
return nil
}).Build()
m2 := mockey.Mock((*Options).Run).To(func(*Options) error {
return nil
}).Build()
defer m1.UnPatch()
defer m2.UnPatch()

t.Run("compile success", func(t *testing.T) {
cmd := NewCmdBuild()
err := cmd.Execute()
assert.Nil(t, err)
})
}
77 changes: 18 additions & 59 deletions pkg/cmd/compile/options.go → pkg/cmd/build/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package compile
package build

import (
"fmt"
Expand All @@ -11,37 +11,29 @@ import (

"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/generator"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/projectstack"
)

type Options struct {
IsKclPkg bool
IsCheck bool
Filenames []string
Flags
}

type Flags struct {
Output string
WorkDir string
Settings []string
Arguments map[string]string
Overrides []string
DisableNone bool
OverrideAST bool
NoStyle bool
Output string
WorkDir string
Arguments map[string]string
NoStyle bool
}

const Stdout = "stdout"

func NewCompileOptions() *Options {
func NewBuildOptions() *Options {
return &Options{
Filenames: []string{},
Flags: Flags{
Settings: []string{},
Arguments: map[string]string{},
Overrides: []string{},
},
}
}
Expand Down Expand Up @@ -77,25 +69,19 @@ func (o *Options) Run() error {
return err
}

sp, err := spec.GenerateSpecWithSpinner(&generator.Options{
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Settings: o.Settings,
Arguments: o.Arguments,
Overrides: o.Overrides,
DisableNone: o.DisableNone,
OverrideAST: o.OverrideAST,
NoStyle: o.NoStyle,
}, project, stack)
sp, err := spec.GenerateSpecWithSpinner(
&generator.Options{
IsKclPkg: o.IsKclPkg,
WorkDir: o.WorkDir,
Filenames: o.Filenames,
Arguments: o.Arguments,
NoStyle: o.NoStyle,
},
project,
stack,
)
if err != nil {
// only print err in the check command
if o.IsCheck {
fmt.Println(err)
return nil
} else {
return err
}
return err
}

yaml, err := yamlv2.Marshal(sp)
Expand Down Expand Up @@ -133,32 +119,5 @@ func (o *Options) PreSet(preCheck func(cur string) bool) error {
o.IsKclPkg = true
return nil
}

if len(o.Settings) == 0 {
o.Settings = []string{projectstack.KclFile}
info, err := os.Stat(filepath.Join(curDir, projectstack.CiTestDir, projectstack.SettingsFile))
switch {
case err != nil && os.IsNotExist(err):
log.Warnf("%s is not exist", projectstack.SettingsFile)
case err != nil && !os.IsNotExist(err):
return err
case err == nil && info.Mode().IsRegular():
o.Settings = append(o.Settings, filepath.Join(projectstack.CiTestDir, projectstack.SettingsFile))
case err == nil && !info.Mode().IsRegular():
log.Warnf("%s is not a regular file", projectstack.SettingsFile)
}
}

if o.Output == "" {
absCiTestDir := filepath.Join(curDir, projectstack.CiTestDir)
_, err := os.Stat(absCiTestDir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
_ = os.Mkdir(absCiTestDir, 0o750)
}
o.Output = filepath.Join(projectstack.CiTestDir, projectstack.StdoutGoldenFile)
}
return nil
}
38 changes: 10 additions & 28 deletions pkg/cmd/compile/options_test.go → pkg/cmd/build/options_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package compile
package build

import (
"errors"
Expand Down Expand Up @@ -42,55 +42,37 @@ var (

func TestCompileOptions_preSet(t *testing.T) {
type fields struct {
Settings []string
Output string
Output string
}
type want struct {
Settings []string
Output string
Output string
}

tests := []struct {
name string
fields fields
want want
}{
{
name: "preset-nothing",
fields: fields{
Settings: []string{"ci-test/settings.yaml", "kcl.yaml"},
Output: "ci-test/stdout.golden.yaml",
},
want: want{
Settings: []string{"ci-test/settings.yaml", "kcl.yaml"},
Output: "ci-test/stdout.golden.yaml",
},
},
{
name: "preset-everything",
fields: fields{
Settings: []string{},
Output: "",
Output: "",
},
want: want{
Settings: []string{"kcl.yaml"},
Output: "ci-test/stdout.golden.yaml",
Output: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := NewCompileOptions()
o := NewBuildOptions()

o.Settings = tt.fields.Settings
o.Output = tt.fields.Output

o.PreSet(func(cur string) bool {
return true
})

wantOpt := NewCompileOptions()
wantOpt.Settings = tt.want.Settings
wantOpt := NewBuildOptions()
wantOpt.Output = tt.want.Output

assert.Equal(t, wantOpt, o)
Expand All @@ -111,7 +93,7 @@ func TestCompileOptions_Run(t *testing.T) {
defer m2.UnPatch()
defer m3.UnPatch()

o := NewCompileOptions()
o := NewBuildOptions()
o.NoStyle = true
err := o.Run()
assert.Nil(t, err)
Expand All @@ -121,7 +103,7 @@ func TestCompileOptions_Run(t *testing.T) {
m1 := mockDetectProjectAndStackFail()
defer m1.UnPatch()

o := NewCompileOptions()
o := NewBuildOptions()
o.NoStyle = true
err := o.Run()
assert.Equal(t, errTest, err)
Expand All @@ -132,7 +114,7 @@ func TestCompileOptions_Run(t *testing.T) {
m2 := mockGenerateSpecFail()
defer m1.UnPatch()
defer m2.UnPatch()
o := NewCompileOptions()
o := NewBuildOptions()
o.NoStyle = true
err := o.Run()
assert.Equal(t, errTest, err)
Expand Down
Loading
Loading