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: run remote repo in kpm run #251

Merged
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
25 changes: 25 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,31 @@ func (c *KpmClient) CompileTarPkg(tarPath string, opts *opt.CompileOptions) (*kc
return c.CompileWithOpts(opts)
}

// CompileGitPkg will compile the kcl package from the git url.
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
func (c *KpmClient) CompileGitPkg(gitUrl, version string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
// 1. Create the temporary directory to pull the tar.
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
// clean the temp dir.
defer os.RemoveAll(tmpDir)

// 2. clone the git repo
_, err = git.CloneWithOpts(
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
git.WithTag(version),
git.WithRepoURL(gitUrl),
git.WithLocalPath(tmpDir),
)
if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedGetPkg, err, "failed to get the git repository")
}

opts.SetPkgPath(tmpDir)

return c.CompileWithOpts(opts)
}

// CompileOciPkg will compile the kcl package from the OCI reference or url.
func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
ociOpts, err := c.ParseOciOptionFromString(ociSource, version)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ func KpmRun(c *cli.Context, kpmcli *client.KpmClient) error {
compileResult, err = kpmcli.CompileWithOpts(kclOpts)
}
} else if runEntry.IsTar() {
// 'kpm run' compile the package from the kcl pakcage tar.
// 'kpm run' compile the package from the kcl package tar.
compileResult, err = kpmcli.CompileTarPkg(runEntry.PackageSource(), kclOpts)
} else if runEntry.IsGit() {
// 'kpm run' compile the package from the git url
compileResult, err = kpmcli.CompileGitPkg(runEntry.PackageSource(), c.String(FLAG_TAG), kclOpts)
} else {
// 'kpm run' compile the package from the OCI reference or url.
compileResult, err = kpmcli.CompileOciPkg(runEntry.PackageSource(), c.String(FLAG_TAG), kclOpts)
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
UrlEntry = "url"
RefEntry = "ref"
TarEntry = "tar"
GitEntry = "git"
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
KCL_MOD = "kcl.mod"
OCI_SEPARATOR = ":"
KCL_PKG_TAR = "*.tar"
Expand Down
6 changes: 6 additions & 0 deletions pkg/runner/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (e *Entry) IsTar() bool {
return e.kind == constants.TarEntry
}

func (e *Entry) IsGit() bool {
return e.kind == constants.GitEntry
}

// IsEmpty will return true if the entry is empty.
func (e *Entry) IsEmpty() bool {
return len(e.packageSource) == 0
Expand Down Expand Up @@ -166,6 +170,8 @@ func GetSourceKindFrom(source string) EntryKind {
return constants.FileEntry
} else if utils.IsTar(source) {
return constants.TarEntry
} else if utils.IsGitRepoUrl(source) {
return constants.GitEntry
} else if utils.IsURL(source) {
return constants.UrlEntry
} else if utils.IsRef(source) {
Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

goerrors "errors"
Expand Down Expand Up @@ -378,6 +379,12 @@ func IsURL(str string) bool {
return err == nil && u.Scheme != "" && u.Host != ""
}

// IsGitRepoUrl will check whether the string 'str' is a git repo url
func IsGitRepoUrl(str string) bool {
r := regexp.MustCompile(`((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)?(/)?`)
return r.MatchString(str)
}

// IsRef will check whether the string 'str' is a reference.
func IsRef(str string) bool {
_, err := reference.ParseNormalizedNamed(str)
Expand Down
23 changes: 23 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ func TestIsUrl(t *testing.T) {
assert.Equal(t, IsURL("https://"), false)
}

func TestIsGitRepoUrl(t *testing.T) {
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, IsGitRepoUrl("invalid url"), false)
assert.Equal(t, IsGitRepoUrl("ftp://github.com/user/project.git"), false)
assert.Equal(t, IsGitRepoUrl("https://github.com/user/project"), true)
assert.Equal(t, IsGitRepoUrl("[email protected]:user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("https://github.com/user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("https://github.com/user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("[email protected]:user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("https://192.168.101.127/user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("http://192.168.101.127/user/project.git"), true)
assert.Equal(t, IsGitRepoUrl("ssh://[email protected]:port/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://[email protected]/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://host.xz:port/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://host.xz/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://[email protected]/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://[email protected]/~user/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://host.xz/~user/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("ssh://[email protected]/~/path/to/repo.git"), true)
assert.Equal(t, IsGitRepoUrl("git://host.xz/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("http://host.xz/path/to/repo.git/"), true)
assert.Equal(t, IsGitRepoUrl("https://host.xz/path/to/repo.git/"), true)
}

func TestIsRef(t *testing.T) {
assert.Equal(t, IsRef("invalid ref"), false)
assert.Equal(t, IsRef("ghcr.io/xxx/xxx"), true)
Expand Down
Loading