Skip to content

Commit

Permalink
Merge pull request #170 from zong-zhe/replace-kcl-args
Browse files Browse the repository at this point in the history
feat: remove the '--kcl-args'.
  • Loading branch information
Peefy authored Aug 24, 2023
2 parents 370028b + acdaade commit 85a014e
Show file tree
Hide file tree
Showing 32 changed files with 310 additions and 272 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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/alexflint/go-scalar v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/jsonv v1.1.3 // indirect
Expand Down Expand Up @@ -85,7 +84,6 @@ require (
)

require (
github.com/alexflint/go-arg v1.4.3
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-git/go-git/v5 v5.6.1
github.com/gofrs/flock v0.8.1
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo=
github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA=
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
Expand Down
50 changes: 29 additions & 21 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"strings"

"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/oci"
Expand All @@ -16,7 +17,7 @@ import (
)

// RunTar will compile the kcl package from a kcl package tar.
func RunTar(tarPath string, entryFiles []string, vendorMode bool, kclArgs string) (string, error) {
func RunTar(tarPath string, opts *opt.CompileOptions) (string, error) {
absTarPath, err := absTarPath(tarPath)
if err != nil {
return "", err
Expand All @@ -30,12 +31,13 @@ func RunTar(tarPath string, entryFiles []string, vendorMode bool, kclArgs string
return "", err
}

opts.SetPkgPath(destDir)
// The directory after extracting the tar package is taken as the root directory of the package,
// and kclvm is called to compile the kcl program under the 'destDir'.
// e.g.
// if the tar path is 'xxx/xxx/xxx/test.tar',
// the 'xxx/xxx/xxx/test' will be taken as the root path of the kcl package to compile.
compileResult, compileErr := RunPkgInPath(destDir, entryFiles, vendorMode, kclArgs)
compileResult, compileErr := RunPkgInPath(opts)

if compileErr != nil {
return "", compileErr
Expand All @@ -46,7 +48,7 @@ func RunTar(tarPath string, entryFiles []string, vendorMode bool, kclArgs string
const KCL_PKG_TAR = "*.tar"

// RunOci will compile the kcl package from an OCI reference.
func RunOci(ociRef, version string, entryFiles []string, vendorMode bool, kclArgs string) (string, error) {
func RunOci(ociRef, version string, opts *opt.CompileOptions) (string, error) {
ociOpts, err := opt.ParseOciOptionFromString(ociRef, version)

if err != nil {
Expand Down Expand Up @@ -76,21 +78,22 @@ func RunOci(ociRef, version string, entryFiles []string, vendorMode bool, kclArg
return "", errors.FailedPull
}

return RunTar(matches[0], entryFiles, vendorMode, kclArgs)
return RunTar(matches[0], opts)
}

// RunPkg will compile current kcl package.
func RunPkg(entryFiles []string, vendorMode bool, kclArgs string) (string, error) {
func RunPkg(opts *opt.CompileOptions) (string, error) {

// If no tar packages specified by "--tar" to run
// kpm will take the current directory ($PWD) as the root of the kcl package and compile.
pwd, err := os.Getwd()
opts.SetPkgPath(pwd)

if err != nil {
reporter.ExitWithReport("kpm: internal bug: failed to load working directory")
}

compileResult, err := RunPkgInPath(pwd, entryFiles, vendorMode, kclArgs)
compileResult, err := RunPkgInPath(opts)
if err != nil {
return "", err
}
Expand All @@ -100,9 +103,9 @@ func RunPkg(entryFiles []string, vendorMode bool, kclArgs string) (string, error

// RunPkgInPath will load the 'KclPkg' from path 'pkgPath'.
// And run the kcl package with entry file in 'entryFilePath' in 'vendorMode'.
func RunPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclArgs string) (string, error) {
func RunPkgInPath(opts *opt.CompileOptions) (string, error) {

pkgPath, err := filepath.Abs(pkgPath)
pkgPath, err := filepath.Abs(opts.PkgPath())
if err != nil {
return "", errors.InternalBug
}
Expand All @@ -112,7 +115,7 @@ func RunPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclA
return "", errors.FailedToLoadPackage
}

kclPkg.SetVendorMode(vendorMode)
kclPkg.SetVendorMode(opts.IsVendor())

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
Expand All @@ -124,20 +127,25 @@ func RunPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclA
return "", err
}

// Calculate the absolute path of entry file described by '--input'.
compiler := runner.DefaultCompiler()
compiler.SetKclCliArgs(kclArgs)
for _, entryFilePath := range entryFilePaths {
entryFilePath, err = getAbsInputPath(pkgPath, entryFilePath)
if err != nil {
return "", err
if len(opts.Entries()) > 0 {
// add entry from '--input'
for _, entry := range opts.Entries() {
if filepath.IsAbs(entry) {
opts.Merge(kcl.WithKFilenames(entry))
} else {
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
compiler.AddKFile(entryFilePath)
// add entry from 'kcl.mod'
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
opts.Merge(*kclPkg.GetKclOpts())
} else if !opts.HasSettingsYaml() {
// no entry
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}

if len(entryFilePaths) == 0 && len(kclPkg.GetEntryKclFilesFromModFile()) == 0 {
compiler.AddKFile(kclPkg.HomePath)
}
// Calculate the absolute path of entry file described by '--input'.
compiler := runner.NewCompilerWithOpts(opts)

// Call the kcl compiler.
compileResult, err := kclPkg.Compile(
Expand All @@ -146,7 +154,7 @@ func RunPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclA
)

if err != nil {
return "", err
return "", reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package")
}

return compileResult.GetRawYamlResult(), nil
Expand Down
23 changes: 18 additions & 5 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package api

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/opt"
"kcl-lang.io/kpm/pkg/utils"
)

Expand Down Expand Up @@ -58,23 +61,31 @@ func TestAbsTarPath(t *testing.T) {

func TestRunPkgInPath(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
result, err := RunPkgInPath(filepath.Join(pkgPath, "test_kcl"), []string{"main.k"}, false, "")
opts := opt.DefaultCompileOptions()
opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "main.k"))
opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
result, err := RunPkgInPath(opts)
assert.Equal(t, err, nil)
expected, _ := os.ReadFile(filepath.Join(pkgPath, "expected"))
assert.Equal(t, utils.RmNewline(string(result)), utils.RmNewline(string(expected)))
}

func TestRunPkgInPathInvalidPath(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
result, err := RunPkgInPath(filepath.Join(pkgPath, "test_kcl"), []string{"not_exist.k"}, false, "")
opts := opt.DefaultCompileOptions()
opts.AddEntry(filepath.Join(pkgPath, "test_kcl", "not_exist.k"))
opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
result, err := RunPkgInPath(opts)
assert.NotEqual(t, err, nil)
assert.Equal(t, err, errors.EntryFileNotFound)
assert.Equal(t, err.Error(), fmt.Sprintf("kpm: failed to compile the kcl package\nkpm: Cannot find the kcl file, please check the file path %s\n", filepath.Join(pkgPath, "test_kcl", "not_exist.k")))
assert.Equal(t, result, "")
}

func TestRunPkgInPathInvalidPkg(t *testing.T) {
pkgPath := getTestDir("test_run_pkg_in_path")
result, err := RunPkgInPath(filepath.Join(pkgPath, "invalid_pkg"), []string{"not_exist.k"}, false, "")
opts := opt.DefaultCompileOptions()
opts.Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "invalid_pkg", "not_exist.k")))
result, err := RunPkgInPath(opts)
assert.NotEqual(t, err, nil)
assert.Equal(t, err, errors.FailedToLoadPackage)
assert.Equal(t, result, "")
Expand All @@ -91,7 +102,9 @@ func TestRunTar(t *testing.T) {
}

expectedResult, _ := os.ReadFile(expectPath)
gotResult, err := RunTar(tarPath, []string{""}, true, "")
opts := opt.DefaultCompileOptions()
opts.SetVendor(true)
gotResult, err := RunTar(tarPath, opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.RmNewline(string(expectedResult)), utils.RmNewline(gotResult))
assert.Equal(t, utils.DirExists(untarPath), true)
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/cmd_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ package cmd
const FLAG_INPUT = "input"
const FLAG_VENDOR = "vendor"
const FLAG_UPDATE = "update"
const FLAG_KCL = "kcl_args"
const FLAG_TAG = "tag"
const FLAG_TAR_PATH = "tar_path"

const FLAG_SETTING = "setting"
const FLAG_DISABLE_NONE = "disable_none"
const FLAG_ARGUMENT = "argument"
const FLAG_OVERRIDES = "overrides"
const FLAG_SORT_KEYS = "sort_keys"
138 changes: 109 additions & 29 deletions pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"

"github.com/urfave/cli/v2"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/opt"
)

// NewRunCmd new a Command for `kpm run`.
Expand All @@ -34,38 +36,116 @@ func NewRunCmd() *cli.Command {
Usage: "run in vendor mode",
},

// '--kcl' will pass the arguments to kcl.
&cli.StringFlag{
Name: FLAG_KCL,
Value: "",
Usage: "Arguments for kcl",
// KCL arg: --setting, -Y
&cli.StringSliceFlag{
Name: FLAG_SETTING,
Aliases: []string{"Y"},
Usage: "specify the input setting file",
},

// KCL arg: --argument, -D
&cli.StringSliceFlag{
Name: FLAG_ARGUMENT,
Aliases: []string{"D"},
Usage: "specify the top-level argument",
},

// KCL arg: --overrides, -O
&cli.StringSliceFlag{
Name: FLAG_OVERRIDES,
Aliases: []string{"O"},
Usage: "specify the configuration override path and value",
},

// KCL arg: --disable_none, -n
&cli.BoolFlag{
Name: FLAG_DISABLE_NONE,
Aliases: []string{"n"},
Usage: "disable dumping None values",
},

// KCL arg: --sort_keys -k
&cli.BoolFlag{
Name: FLAG_SORT_KEYS,
Aliases: []string{"k"},
Usage: "sort result keys",
},
},
Action: func(c *cli.Context) error {
pkgWillBeCompiled := c.Args().First()
// 'kpm run' compile the current package undor '$pwd'.
if len(pkgWillBeCompiled) == 0 {
compileResult, err := api.RunPkg(c.StringSlice(FLAG_INPUT), c.Bool(FLAG_VENDOR), c.String(FLAG_KCL))
if err != nil {
return err
}
fmt.Println(compileResult)
} else {
// 'kpm run <package source>' compile the kcl package from the <package source>.
compileResult, err := api.RunPkgInPath(pkgWillBeCompiled, c.StringSlice(FLAG_INPUT), c.Bool(FLAG_VENDOR), c.String(FLAG_KCL))
if err == errors.FailedToLoadPackage {
compileResult, err = api.RunTar(pkgWillBeCompiled, c.StringSlice(FLAG_INPUT), c.Bool(FLAG_VENDOR), c.String(FLAG_KCL))
if err == errors.InvalidKclPacakgeTar {
compileResult, err = api.RunOci(pkgWillBeCompiled, c.String(FLAG_TAG), c.StringSlice(FLAG_INPUT), c.Bool(FLAG_VENDOR), c.String(FLAG_KCL))
}
}

if err != nil {
return err
}
fmt.Println(compileResult)
}
return nil
return KpmRun(c)
},
}
}

func KpmRun(c *cli.Context) error {
kclOpts := CompileOptionFromCli(c)
pkgWillBeCompiled := c.Args().First()
// 'kpm run' compile the current package undor '$pwd'.
if len(pkgWillBeCompiled) == 0 {
compileResult, err := api.RunPkg(kclOpts)
if err != nil {
return err
}
fmt.Println(compileResult)
} else {
// 'kpm run <package source>' compile the kcl package from the <package source>.
kclOpts.SetPkgPath(pkgWillBeCompiled)
compileResult, err := api.RunPkgInPath(kclOpts)
if err == errors.FailedToLoadPackage {
compileResult, err = api.RunTar(pkgWillBeCompiled, kclOpts)
if err == errors.InvalidKclPacakgeTar {
compileResult, err = api.RunOci(pkgWillBeCompiled, c.String(FLAG_TAG), kclOpts)
}
}

if err != nil {
return err
}
fmt.Println(compileResult)
}
return nil
}

// CompileOptionFromCli will parse the kcl options from the cli context.
func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions {
opts := opt.DefaultCompileOptions()

// --input
opts.ExtendEntries(c.StringSlice(FLAG_INPUT))

// --vendor
opts.SetVendor(c.Bool(FLAG_VENDOR))

// --setting, -Y
settingsOpt := c.StringSlice(FLAG_SETTING)
if len(settingsOpt) != 0 {
for _, sPath := range settingsOpt {
opts.Merge(kcl.WithSettings(sPath))
}
opts.SetHasSettingsYaml(true)
}

// --argument, -D
argumentsOpt := c.StringSlice(FLAG_ARGUMENT)
if len(argumentsOpt) != 0 {
for _, arg := range argumentsOpt {
opts.Merge(kcl.WithOptions(arg))
}
}

// --overrides, -O
overridesOpt := c.StringSlice(FLAG_OVERRIDES)
if len(overridesOpt) != 0 {
for _, override := range overridesOpt {
opts.Merge(kcl.WithOverrides(override))
}
}

// --disable_none, -n
opts.Merge(kcl.WithDisableNone(c.Bool(FLAG_DISABLE_NONE)))

// --sort_keys, -k
opts.Merge(kcl.WithSortKeys(c.Bool(FLAG_SORT_KEYS)))

return opts
}
Loading

0 comments on commit 85a014e

Please sign in to comment.