diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 57e2a0ec..acf744ca 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" + "github.com/hashicorp/go-version" "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kpm/pkg/errors" "kcl-lang.io/kpm/pkg/reporter" @@ -144,6 +145,7 @@ func (opts *CompileOptions) LogWriter() io.Writer { type InitOptions struct { Name string InitPath string + Version string } func (opts *InitOptions) Validate() error { @@ -152,6 +154,12 @@ func (opts *InitOptions) Validate() error { } else if len(opts.InitPath) == 0 { return errors.InternalBug } + if opts.Version != "" { + if _, err := version.NewSemver(opts.Version); err != nil { + return errors.InvalidVersionFormat + } + } + return nil } diff --git a/pkg/opt/opt_test.go b/pkg/opt/opt_test.go index a04ea6a0..52b9e455 100644 --- a/pkg/opt/opt_test.go +++ b/pkg/opt/opt_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/assert" "kcl-lang.io/kcl-go/pkg/kcl" + "kcl-lang.io/kpm/pkg/errors" ) func TestWorkDirAsPkgPath(t *testing.T) { @@ -20,3 +21,12 @@ func TestWorkDirAsPkgPath(t *testing.T) { opts.SetEntries([]string{"override.k"}) assert.Equal(t, opts.Entries(), []string{"override.k"}) } + +func TestInitOptions(t *testing.T) { + o1 := InitOptions{Name: "foo", InitPath: "bar", Version: "v0.0.1"} + o2 := InitOptions{Name: "foo", InitPath: "bar", Version: "v0.0.2"} + o3 := InitOptions{Name: "foo", InitPath: "bar", Version: "abc.0.3"} + assert.Equal(t, o1.Validate(), nil) + assert.Equal(t, o2.Validate(), nil) + assert.Equal(t, o3.Validate(), errors.InvalidVersionFormat) +} diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index eda42258..1593b8f8 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -316,14 +316,18 @@ func (mfile *ModFile) GetModLockFilePath() string { } const defaultVerion = "0.0.1" + var defaultEdition = runner.GetKclVersion() func NewModFile(opts *opt.InitOptions) *ModFile { + if opts.Version == "" { + opts.Version = defaultVerion + } return &ModFile{ HomePath: opts.InitPath, Pkg: Package{ Name: opts.Name, - Version: defaultVerion, + Version: opts.Version, Edition: defaultEdition, }, Dependencies: Dependencies{