-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce the new command
kusion build
and delete redundant c…
…ommands (#608)
- Loading branch information
Showing
22 changed files
with
190 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ vendor/ | |
|
||
# build | ||
_build* | ||
build/ | ||
build/bundles/ | ||
build/yamls/ | ||
build/charts/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.