Skip to content

Commit

Permalink
Merge pull request #3 from Peefy/feat-run-command
Browse files Browse the repository at this point in the history
feat: impl the run command and add test cases.
  • Loading branch information
Peefy authored Oct 24, 2023
2 parents d01656a + 8419ba9 commit f5a4902
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 77 deletions.
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright The KCL Authors. All rights reserved.
//
// #### Language & Tooling Commands
//
// ```
Expand Down Expand Up @@ -69,10 +71,10 @@ package cmd

import (
"github.com/spf13/cobra"
"kcl-lang.io/cli/pkg/version"
)

const rootCmdShortUsage = "The KCL Command Line Interface (CLI)."
const rootCmdLongUsage = `The KCL Command Line Interface (CLI).
const rootDesc = `The KCL Command Line Interface (CLI).
KCL is an open-source, constraint-based record and functional language that
enhances the writing of complex configurations, including those for cloud-native
Expand All @@ -83,9 +85,10 @@ scenarios. The KCL website: https://kcl-lang.io
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "kcl",
Short: rootCmdShortUsage,
Long: rootCmdLongUsage,
Short: "The KCL Command Line Interface (CLI).",
Long: rootDesc,
SilenceUsage: true,
Version: version.GetVersionString(),
}
cmd.AddCommand(NewVersionCmd())
cmd.AddCommand(NewRunCmd())
Expand Down
73 changes: 59 additions & 14 deletions cmd/run.go
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
}
53 changes: 53 additions & 0 deletions cmd/run_test.go
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)
}
}
2 changes: 2 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright The KCL Authors. All rights reserved.

package cmd

import (
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module kcl-lang.io/cli
go 1.19

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/spf13/cobra v1.7.0
k8s.io/kubectl v0.28.3
kcl-lang.io/kcl-go v0.6.1-0.20231023065115-16ee99bd07f7
kcl-lang.io/kcl-openapi v0.5.1
kcl-lang.io/kpm v0.3.7
Expand Down Expand Up @@ -53,7 +53,7 @@ require (
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/powerman/rpc-codec v1.2.2 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
Expand Down Expand Up @@ -90,7 +90,6 @@ require (
github.com/Microsoft/go-winio v0.6.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0g
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ=
github.com/acomagu/bufpipe v1.0.4/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -106,8 +108,6 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chai2010/gettext-go v1.0.2 h1:1Lwwip6Q2QGsAdl/ZKPCwTe9fe0CjlUbqj5bFNSjIRk=
github.com/chai2010/gettext-go v1.0.2/go.mod h1:y+wnP2cHYaVj19NZhYKAwEMH2CI1gNHeQQ+5AjwawxA=
github.com/chai2010/jsonv v1.1.3 h1:gBIHXn/5mdEPTuWZfjC54fn/yUSRR8OGobXobcc6now=
github.com/chai2010/jsonv v1.1.3/go.mod h1:mEoT1dQ9qVF4oP9peVTl0UymTmJwXoTDOh+sNA6+XII=
github.com/chai2010/protorpc v1.1.4 h1:CTtFUhzXRoeuR7FtgQ2b2vdT/KgWVpCM+sIus8zJjHs=
Expand Down Expand Up @@ -1142,8 +1142,6 @@ k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42/go.mod h1:Z/45zLw8lUo4wdiUkI+v/ImEGAvu3WatcZl3lPMR4Rk=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/kubectl v0.28.3 h1:H1Peu1O3EbN9zHkJCcvhiJ4NUj6lb88sGPO5wrWIM6k=
k8s.io/kubectl v0.28.3/go.mod h1:RDAudrth/2wQ3Sg46fbKKl4/g+XImzvbsSRZdP2RiyE=
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
Expand Down
Loading

0 comments on commit f5a4902

Please sign in to comment.