-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Peefy/feat-run-command
feat: impl the run command and add test cases.
- Loading branch information
Showing
11 changed files
with
434 additions
and
77 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
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 |
---|---|---|
@@ -1,38 +1,83 @@ | ||
// Copyright The KCL Authors. All rights reserved. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/kubectl/pkg/util/i18n" | ||
"kcl-lang.io/cli/pkg/options" | ||
) | ||
|
||
const ( | ||
runDesc = ` | ||
This command runs the kcl code and displays the output. 'kcl run' takes multiple input for arguments. | ||
For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k | ||
` | ||
runExample = ` # Run a single file and output YAML | ||
kcl run path/to/kcl.k | ||
# Run a single file and output JSON | ||
kcl run path/to/kcl.k --format json | ||
# Run multiple files | ||
kcl run path/to/kcl1.k path/to/kcl2.k | ||
# Run OCI packages | ||
kcl run oci://ghcr.io/kcl-lang/hello-world | ||
# Run the current package | ||
kcl run | ||
` | ||
) | ||
|
||
// NewRunCmd returns the run command. | ||
func NewRunCmd() *cobra.Command { | ||
o := options.NewRunOptions() | ||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Run KCL codes.", | ||
Use: "run", | ||
Short: "Run KCL codes.", | ||
Long: runDesc, | ||
Example: runExample, | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
o.Entries = args | ||
err := o.Run() | ||
if err != nil { | ||
if err := o.Complete(args); err != nil { | ||
return err | ||
} | ||
if err := o.Validate(); err != nil { | ||
return err | ||
} | ||
return nil | ||
return o.Run() | ||
}, | ||
SilenceUsage: true, | ||
} | ||
|
||
cmd.Flags().StringSliceVarP(&o.Arguments, "argument", "D", []string{}, | ||
i18n.T("Specify the top-level argument")) | ||
"Specify the top-level argument") | ||
cmd.Flags().StringSliceVarP(&o.Settings, "setting", "Y", []string{}, | ||
i18n.T("Specify the command line setting files")) | ||
cmd.Flags().StringVarP(&o.Output, "output", "o", "", | ||
i18n.T("Specify the output file")) | ||
cmd.Flags().BoolVarP(&o.DisableNone, "disable-none", "n", false, | ||
i18n.T("Disable dumping None values")) | ||
"Specify the command line setting files") | ||
cmd.Flags().StringSliceVarP(&o.Overrides, "overrides", "O", []string{}, | ||
i18n.T("Specify the configuration override path and value")) | ||
"Specify the configuration override path and value") | ||
cmd.Flags().StringSliceVarP(&o.PathSelectors, "path_selectors", "S", []string{}, | ||
"Specify the path selectors") | ||
cmd.Flags().StringSliceVarP(&o.ExternalPackages, "external", "E", []string{}, | ||
" Mapping of package name and path where the package is located") | ||
cmd.Flags().StringVarP(&o.Output, "output", "o", "", | ||
"Specify the YAML/JSON output file path") | ||
cmd.Flags().StringVarP(&o.Tag, "tag", "t", "", | ||
"Specify the tag for the OCI or Git artifact") | ||
cmd.Flags().StringVar(&o.Format, "format", "yaml", | ||
"Specify the output format") | ||
cmd.Flags().BoolVarP(&o.DisableNone, "disable_none", "n", false, | ||
"Disable dumping None values") | ||
cmd.Flags().BoolVarP(&o.StrictRangeCheck, "strict_range_check", "r", false, | ||
"Do perform strict numeric range checks") | ||
cmd.Flags().BoolVarP(&o.Debug, "debug", "d", false, | ||
"Run in debug mode") | ||
cmd.Flags().BoolVarP(&o.SortKeys, "sort_keys", "k", false, | ||
"Sort output result keys") | ||
cmd.Flags().BoolVarP(&o.Vendor, "vendor", "V", false, | ||
"Sort output result keys") | ||
cmd.Flags().BoolVar(&o.NoStyle, "no_style", false, | ||
"Sort output result keys") | ||
|
||
return cmd | ||
} |
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,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNewRunCmd(t *testing.T) { | ||
cmd := NewRunCmd() | ||
|
||
if cmd.Use != "run" { | ||
t.Errorf("unexpected command use: %s", cmd.Use) | ||
} | ||
|
||
if cmd.Short != "Run KCL codes." { | ||
t.Errorf("unexpected command short description: %s", cmd.Short) | ||
} | ||
|
||
if cmd.Long != runDesc { | ||
t.Errorf("unexpected command long description: %s", cmd.Long) | ||
} | ||
|
||
if cmd.Example != runExample { | ||
t.Errorf("unexpected command example: %s", cmd.Example) | ||
} | ||
|
||
if cmd.SilenceUsage != true { | ||
t.Errorf("unexpected SilenceUsage value: %v", cmd.SilenceUsage) | ||
} | ||
|
||
runE := cmd.RunE | ||
if runE == nil { | ||
t.Fatal("RunE function is nil") | ||
} | ||
|
||
args := []string{"../examples/kubernetes.k"} | ||
err := runE(cmd, args) | ||
if err != nil { | ||
t.Errorf("RunE function returned an error: %v", err) | ||
} | ||
|
||
args = []string{"error.k"} | ||
err = runE(cmd, args) | ||
if !strings.Contains(err.Error(), "Cannot find the kcl file") { | ||
t.Errorf("RunE function returned an error: %v", err) | ||
} | ||
|
||
args = []string{"error.k"} | ||
err = runE(cmd, args) | ||
if !strings.Contains(err.Error(), "Cannot find the kcl file") { | ||
t.Errorf("RunE function returned an error: %v", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Copyright The KCL Authors. All rights reserved. | ||
|
||
package cmd | ||
|
||
import ( | ||
|
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.