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: unify the kcl run cli #105

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions cmd/kcl/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ func appendLangFlags(o *options.RunOptions, flags *pflag.FlagSet) {
"Specify the path selectors")
flags.StringVarP(&o.Output, "output", "o", "",
"Specify the YAML/JSON output file path")
flags.StringVarP(&o.Git, "git", "", "",
"Specify the KCL module git url")
flags.StringVarP(&o.Oci, "oci", "", "",
"Specify the KCL module oci url")
flags.StringVarP(&o.Tag, "tag", "t", "",
"Specify the tag for the OCI or Git artifact")
flags.StringVarP(&o.Commit, "commit", "c", "",
"Specify the commit for the Git artifact")
flags.StringVarP(&o.Branch, "branch", "b", "",
"Specify the branch for the Git artifact")
flags.StringVar(&o.Format, "format", "yaml",
"Specify the output format")
flags.BoolVarP(&o.DisableNone, "disable_none", "n", false,
Expand Down
2 changes: 1 addition & 1 deletion cmd/kcl/commands/mod_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
# Pull the module from the OCI Registry by oci url
kcl mod pull oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0
# Pull the module from the GitHub by flag
# Pull the module from the Git by flag
kcl mod pull --git https://github.com/kcl-lang/konfig --tag v0.4.0
# Pull the module from the OCI Registry by flag
Expand Down
16 changes: 11 additions & 5 deletions cmd/kcl/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const (
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
runExample = ` # Run the current package
kcl run
# Run a single file and output YAML
kcl run path/to/kcl.k
# Run a single file and output JSON
Expand All @@ -25,13 +28,16 @@ For example, 'kcl run path/to/kcl.k' will run the file named path/to/kcl.k
kcl run path/to/kcl1.k path/to/kcl2.k
# Run OCI packages
kcl run oci://ghcr.io/kcl-lang/helloworld
kcl run oci://ghcr.io/kcl-lang/helloworld --tag 0.1.0
# Run remote Git repo
kcl run https://github.com/kcl-lang/flask-demo-kcl-manifests
kcl run git://github.com/kcl-lang/flask-demo-kcl-manifests --commit ade147b
# Run OCI packages by flag
kcl run --oci https://ghcr.io/kcl-lang/helloworld --tag 0.1.0
# Run the current package
kcl run`
# Run remote module from Git with branch repo by flag
kcl run --git https://github.com/kcl-lang/flask-demo-kcl-manifests --branch main`
)

// NewRunCmd returns the run command.
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
kcl-lang.io/kcl-go v0.9.1
kcl-lang.io/kcl-openapi v0.6.3
kcl-lang.io/kcl-playground v0.5.1
kcl-lang.io/kpm v0.9.0
kcl-lang.io/kpm v0.9.1-0.20240710094621-7c6b74f8b24c
)

require (
Expand All @@ -27,7 +27,7 @@ require (
github.com/chainguard-dev/git-urls v1.0.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containers/image/v5 v5.31.0 // indirect
github.com/containers/image/v5 v5.31.1 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.10 // indirect
github.com/containers/storage v1.54.0 // indirect
Expand All @@ -44,7 +44,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.4 // indirect
github.com/hashicorp/go-getter v1.7.5 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
Expand Down Expand Up @@ -72,6 +72,7 @@ require (
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
kcl-lang.io/lib v0.9.1 // indirect
)

require (
Expand Down Expand Up @@ -124,7 +125,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-yaml v1.11.3
github.com/gofrs/flock v0.11.0 // indirect
github.com/gofrs/flock v0.12.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -204,7 +205,6 @@ require (
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
kcl-lang.io/lib v0.9.1 // indirect
oras.land/oras-go v1.2.5 // indirect
oras.land/oras-go/v2 v2.5.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ github.com/containerd/errdefs v0.1.0 h1:m0wCRBiu1WJT/Fr+iOoQHMQS/eP5myQ8lCv4Dz5Z
github.com/containerd/errdefs v0.1.0/go.mod h1:YgWiiHtLmSeBrvpw+UfPijzbLaB77mEG1WwJTDETIV0=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containers/image/v5 v5.31.0 h1:eDFVlz5XaYICxe9dXpf23htEKvyosgkl62mJlIATXE4=
github.com/containers/image/v5 v5.31.0/go.mod h1:5QfOqSackPkSbF7Qxc1DnVNnPJKQ+KWLkfEfDpK590Q=
github.com/containers/image/v5 v5.31.1 h1:3x9soI6Biml/GiDLpkSmKrkRSwVGctxu/vONpoUdklA=
github.com/containers/image/v5 v5.31.1/go.mod h1:5QfOqSackPkSbF7Qxc1DnVNnPJKQ+KWLkfEfDpK590Q=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 h1:Qzk5C6cYglewc+UyGf6lc8Mj2UaPTHy/iF2De0/77CA=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01/go.mod h1:9rfv8iPl1ZP7aqh9YA68wnZv2NUDbXdcdPHVz0pFbPY=
github.com/containers/ocicrypt v1.1.10 h1:r7UR6o8+lyhkEywetubUUgcKFjOWOaWz8cEBrCPX0ic=
Expand Down Expand Up @@ -485,8 +485,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.11.0 h1:AGFQxrpWd8ezw60AvLWIPbxMydNfF8564pwH3FCty0g=
github.com/gofrs/flock v0.11.0/go.mod h1:FirDy1Ing0mI2+kB6wk+vyyAH+e6xiE+EYA0jnzV9jc=
github.com/gofrs/flock v0.12.0 h1:xHW8t8GPAiGtqz7KxiSqfOEXwpOaqhpYZrTE2MQBgXY=
github.com/gofrs/flock v0.12.0/go.mod h1:FirDy1Ing0mI2+kB6wk+vyyAH+e6xiE+EYA0jnzV9jc=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
Expand Down Expand Up @@ -632,8 +632,8 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-getter v1.7.4 h1:3yQjWuxICvSpYwqSayAdKRFcvBl1y/vogCxczWSmix0=
github.com/hashicorp/go-getter v1.7.4/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-getter v1.7.5 h1:dT58k9hQ/vbxNMwoI5+xFYAJuv6152UNvdHokfI5wE4=
github.com/hashicorp/go-getter v1.7.5/go.mod h1:W7TalhMmbPmsSMdNjD0ZskARur/9GJ17cfHTRtXV744=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
Expand Down Expand Up @@ -1691,8 +1691,8 @@ kcl-lang.io/kcl-openapi v0.6.3 h1:6Br0IBaNshRvBfmaH22Zv1miVEt6FWlmh2v/wBLb194=
kcl-lang.io/kcl-openapi v0.6.3/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
kcl-lang.io/kcl-playground v0.5.1 h1:MKQQUHgt4+2QyU2NVwa73oksOaBJGDi4keGoggA0MiU=
kcl-lang.io/kcl-playground v0.5.1/go.mod h1:IFmnlw7m011ccX8OidMUfnnN2u/TWdtQGxyABRTbmow=
kcl-lang.io/kpm v0.9.0 h1:ISbsPw1GWhe6Wt4MgfY0WZEjvb5NI43O+5dmH0CUoiM=
kcl-lang.io/kpm v0.9.0/go.mod h1:+8dz5o9l1DEzEYdfRetnAXhrfqKdzTQb3s99DubbHhk=
kcl-lang.io/kpm v0.9.1-0.20240710094621-7c6b74f8b24c h1:qCz15r0JFyB3AEYlSY2Aq1dPbT90EWJga4EMMMylyTc=
kcl-lang.io/kpm v0.9.1-0.20240710094621-7c6b74f8b24c/go.mod h1:6oKrOP+tNqteyfdlEy1VH8xvy7Eg3FL6q1CHxM4tjc0=
kcl-lang.io/lib v0.9.1 h1:wPcTYFZo4XQZYHPrEoQjV2SU++2jz2y/1Khfqj2/8DA=
kcl-lang.io/lib v0.9.1/go.mod h1:tu+tzwGgHLzYZSIxUG/ntipStrxZd6OvutWYPTxS7cs=
oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo=
Expand Down
171 changes: 84 additions & 87 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/acarl005/stripansi"
Expand All @@ -18,9 +18,8 @@ import (
"kcl-lang.io/kcl-go/pkg/3rdparty/toml"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kcl-go/pkg/tools/gen"
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/git"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/runner"
Expand Down Expand Up @@ -62,8 +61,16 @@ type RunOptions struct {
Debug bool
// StrictRangeCheck performs the 32-bit strict numeric range checks on numbers.
StrictRangeCheck bool
// Git Url is the package url of the Git artifact.
Git string
// Oci Url is the package url of the OCI artifact.
Oci string
// Tag is the package tag of the OCI or Git artifact.
Tag string
// Commit is the package commit of the Git artifact.
Commit string
// Branch is the package branch of the Git artifact.
Branch string
// CompileOnly is used to check a local package and all of its dependencies for errors.
CompileOnly bool
// Format is the output type, e.g., Json, Yaml, etc. Default is Yaml.
Expand Down Expand Up @@ -103,91 +110,26 @@ func (o *RunOptions) Run() error {
err = releaseErr
}
}()
opts := CompileOptionFromCli(o)
if o.Quiet {
opts.SetLogWriter(nil)
}
if err != nil {
return err
}
entry, errEvent := runner.FindRunEntryFrom(opts.Entries())
if errEvent != nil {
return errEvent
}
pwd, err := os.Getwd()
if err != nil {
return err
}
if entry.IsEmpty() {
// kcl compiles the current package under '$pwd'.
if _, e := api.GetKclPackage(pwd); e == nil {
opts.SetPkgPath(pwd)
result, err = cli.CompileWithOpts(opts)
} else {
// TODO: refactor the entry search logic.
depsOpt, depErr := LoadDepsFrom(pwd, o.Quiet)
if depErr != nil {
return err
}
opts.Merge(*depsOpt)
result, err = api.RunWithOpt(opts)
}
} else {
// kcl compiles the package from the local file system, tar and OCI package, etc.
if entry.IsLocalFile() {
depsOpt, depErr := LoadDepsFrom(pwd, o.Quiet)
if depErr != nil {
return err
}
opts.Merge(*depsOpt)
result, err = api.RunWithOpt(opts)
} else if entry.IsLocalFileWithKclMod() {
// Else compile the kcl package (kcl.mod)
var transformedEntries []string
for _, entry := range opts.Entries() {
if !filepath.IsAbs(entry) {
entry, err = filepath.Abs(entry)
if err != nil {
return err
}
}
transformedEntries = append(transformedEntries, entry)
}
// Maybe a single KCL module folder, use the kcl.mod entry profile to run.
if pkg, e := api.GetKclPackage(transformedEntries[0]); e == nil && fs.IsDir(transformedEntries[0]) {
entries := pkg.GetPkgProfile().GetEntries()
if len(entries) > 0 {
opts.SetEntries(entries)
opts.SetPkgPath(transformedEntries[0])
} else {
// Multiple entries with the kcl.mod file and deps.
opts.SetEntries(transformedEntries)
opts.SetPkgPath(entry.PackageSource())
}
} else {
// Multiple entries with the kcl.mod file and deps.
opts.SetEntries(transformedEntries)
opts.SetPkgPath(entry.PackageSource())
}
result, err = cli.CompileWithOpts(opts)
} else if entry.IsTar() {
// compiles the package from the kcl package tar.
opts.SetEntries([]string{})
result, err = cli.CompileTarPkg(entry.PackageSource(), opts)
} else if entry.IsGit() {
opts.SetEntries([]string{})
gitOpts := git.NewCloneOptions(entry.PackageSource(), "", o.Tag, "", "", nil)
// compiles the package from the git url
result, err = cli.CompileGitPkg(gitOpts, opts)
} else if entry.IsUrl() {
// compiles the package from the OCI reference or url.
opts.SetEntries([]string{})
result, err = cli.CompileOciPkg(entry.PackageSource(), o.Tag, opts)
} else {
// If there is only kcl file without kcl package (kcl.mod)
result, err = api.RunWithOpt(opts)
}
cli.SetLogWriter(nil)
}

result, err = cli.Run(
client.WithRunSourceUrls(o.Entries),
client.WithSettingFiles(o.Settings),
client.WithArguments(o.Arguments),
client.WithOverrides(o.Overrides, o.Debug),
client.WithPathSelectors(o.PathSelectors),
client.WithExternalPkgs(o.ExternalPackages),
client.WithVendor(o.Vendor),
client.WithSortKeys(o.SortKeys),
client.WithShowHidden(o.ShowHidden),
client.WithDisableNone(o.DisableNone),
client.WithDebug(o.Debug),
client.WithStrictRange(o.StrictRangeCheck),
client.WithCompileOnly(o.CompileOnly),
)

if err != nil {
if o.NoStyle {
err = errors.New(stripansi.Strip(err.Error()))
Expand All @@ -199,7 +141,62 @@ func (o *RunOptions) Run() error {

// Complete completes the options based on the provided arguments.
func (o *RunOptions) Complete(args []string) error {
o.Entries = args
if len(o.Git) != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add some validation for entries and git, oci source.

gitUrl, err := url.Parse(o.Git)
if err != nil {
return err
}
if gitUrl.Scheme == constants.HttpsScheme || gitUrl.Scheme == constants.HttpScheme {
gitUrl.Scheme = constants.GitScheme
}
query := gitUrl.Query()
if o.Tag != "" {
query.Set("tag", o.Tag)
}
if o.Commit != "" {
query.Set("commit", o.Commit)
}
if o.Branch != "" {
query.Set("branch", o.Branch)
}
gitUrl.RawQuery = query.Encode()
o.Entries = append(o.Entries, gitUrl.String())
}

if len(o.Oci) != 0 {
ociUrl, err := url.Parse(o.Oci)
if err != nil {
return err
}
if ociUrl.Scheme == constants.HttpsScheme || ociUrl.Scheme == constants.HttpScheme {
ociUrl.Scheme = constants.OciScheme
}
query := ociUrl.Query()
if o.Tag != "" {
query.Set("tag", o.Tag)
}
ociUrl.RawQuery = query.Encode()
o.Entries = append(o.Entries, ociUrl.String())
}

for _, arg := range args {
argUrl, err := url.Parse(arg)
if err != nil {
return err
}
query := argUrl.Query()
if o.Tag != "" {
query.Set("tag", o.Tag)
}
if o.Commit != "" {
query.Set("commit", o.Commit)
}
if o.Branch != "" {
query.Set("branch", o.Branch)
}
argUrl.RawQuery = query.Encode()
o.Entries = append(o.Entries, argUrl.String())
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_suites/test_kcl_mod_pull_3/stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
start to pull https://github.com/kcl-lang/flask-demo-kcl-manifests?branch=main
cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests'
cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests' with branch 'main'
pulled flask_manifests 0.0.1 successfully
1 change: 1 addition & 0 deletions test/e2e/test_suites/test_kcl_run_10/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kcl run --git https://github.com/kcl-lang/flask-demo-kcl-manifests --commit ade147b
Empty file.
Loading
Loading