Skip to content

Commit

Permalink
feat: make 'run' command supports ModSpec (#510)
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Oct 30, 2024
1 parent ab68fb5 commit e9b0854
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
37 changes: 37 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,35 @@ func TestRunLocalWithArgs(t *testing.T) {
}
}

func TestRunRemoteWithArgsInvalid(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

logbuf := new(bytes.Buffer)
kpmcli.SetLogWriter(logbuf)

type testCase struct {
sourceURL string
expectedLog string
expectedErrMsg string
}

testCases := []testCase{
{
sourceURL: "git://github.com/kcl-lang/flask-demo-kcl-manifests?commit=8308200&mod=cc:0.0.2",
expectedLog: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests' with commit '8308200'\n",
expectedErrMsg: "version mismatch: 0.0.1 != 0.0.2, version 0.0.2 not found",
},
}

for _, tc := range testCases {
_, err := kpmcli.Run(WithRunSourceUrl(tc.sourceURL))
assert.Equal(t, err.Error(), tc.expectedErrMsg)
assert.Equal(t, logbuf.String(), tc.expectedLog)
logbuf.Reset()
}
}

func TestRunRemoteWithArgs(t *testing.T) {
pkgPath := getTestDir("test_run_options")
kpmcli, err := NewKpmClient()
Expand Down Expand Up @@ -2017,6 +2046,14 @@ func TestRunRemoteWithArgs(t *testing.T) {
return string(expected), err
},
},
{
sourceURL: "git://github.com/kcl-lang/flask-demo-kcl-manifests?commit=8308200&mod=cc:0.0.1",
expectedLog: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests' with commit '8308200'\n",
expectedYamlFn: func(pkgPath string) (string, error) {
expected, err := os.ReadFile(filepath.Join(pkgPath, "remote", "expect_3.yaml"))
return string(expected), err
},
},
}

for i, tc := range testCases {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_options/remote/expect_3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program: Hello World!
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
GitCommit = "commit"

Tag = "tag"
Mod = "mod"

KCL_MOD = "kcl.mod"
KCL_MOD_LOCK = "kcl.mod.lock"
Expand Down
51 changes: 44 additions & 7 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,40 @@ func (local *Local) ToFilePath() (string, error) {
}

func (source *Source) ToString() (string, error) {
var sourceStr string
var err error
if source == nil {
return "", fmt.Errorf("source is nil")
}
if source.Git != nil {
return source.Git.ToString()
}
if source.Oci != nil {
return source.Oci.ToString()
sourceStr, err = source.Git.ToString()
if err != nil {
return "", err
}
} else if source.Oci != nil {
sourceStr, err = source.Oci.ToString()
if err != nil {
return "", err
}
} else if source.Local != nil {
sourceStr, err = source.Local.ToString()
if err != nil {
return "", err
}
}
if source.Local != nil {
return source.Local.ToString()
if source.ModSpec != nil {
url, err := url.Parse(sourceStr)
if err != nil {
return "", err
}

q := url.Query()
q.Set(constants.Mod, source.ModSpec.ToString())
url.RawQuery = q.Encode()
sourceStr = url.String()
}
return "", fmt.Errorf("source is nil")

return sourceStr, nil
}

func (git *Git) ToString() (string, error) {
Expand Down Expand Up @@ -336,6 +357,13 @@ func (local *Local) ToString() (string, error) {
return pathUrl.String(), nil
}

func (mod *ModSpec) ToString() string {
if mod == nil {
return ""
}
return fmt.Sprintf("%s:%s", mod.Name, mod.Version)
}

func (source *Source) FromString(sourceStr string) error {
if source == nil {
return fmt.Errorf("source is nil")
Expand All @@ -346,6 +374,15 @@ func (source *Source) FromString(sourceStr string) error {
return err
}

modSpec := sourceUrl.Query().Get(constants.Mod)
if len(modSpec) != 0 {
source.ModSpec = &ModSpec{}
err := source.ModSpec.FromString(modSpec)
if err != nil {
return err
}
}

if sourceUrl.Scheme == constants.GitScheme || sourceUrl.Scheme == constants.SshScheme {
source.Git = &Git{}
source.Git.FromString(sourceStr)
Expand Down
13 changes: 11 additions & 2 deletions pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error {
return err
}
pkgPath := tmpDir
if s.Git != nil && len(s.Git.Package) > 0 {
pkgPath, err = utils.FindPackage(tmpDir, s.Git.Package)
if !s.ModSpec.IsNil() {
pkgPath, err = utils.FindPackage(tmpDir, s.ModSpec.Name)
if err != nil {
return err
}
Expand All @@ -155,6 +155,15 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error {
return err
}

if !s.ModSpec.IsNil() {
if kclPkg.ModFile.Pkg.Version != s.ModSpec.Version {
return fmt.Errorf(
"version mismatch: %s != %s, version %s not found",
kclPkg.ModFile.Pkg.Version, s.ModSpec.Version, s.ModSpec.Version,
)
}
}

return v(kclPkg)
}

Expand Down

0 comments on commit e9b0854

Please sign in to comment.