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

refactor: refactor the function to parse KCL dependency url #358

Merged
merged 3 commits into from
Jun 4, 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
78 changes: 45 additions & 33 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,44 +196,64 @@ type RegistryOptions struct {
}

// NewRegistryOptionsFrom will parse the registry options from oci url, oci ref and git url.
// If you do not know the url of the package is git or url, you can use this function to parse the options.
// By default:
// 'git://', "https://", "http://" will be parsed as git options.
// 'oci://', will be parsed as oci options.
// 'file://' or a file path will be parsed as local options.
//
// If you know the url is git or oci, you can use 'NewGitOptionsFromUrl' or 'NewOciOptionsFromUrl' to parse the options.
// 'oci' and 'http', 'https' are supported for 'NewOciOptionsFromUrl'.
// 'git', 'http', 'https', 'ssh' are supported for 'NewGitOptionsFromUrl'.
func NewRegistryOptionsFrom(rawUrlorOciRef string, settings *settings.Settings) (*RegistryOptions, error) {
parsedUrl, err := url.Parse(rawUrlorOciRef)
if err != nil {
return nil, err
}

// parse the options from the local file path
localOptions, err := NewLocalOptionsFromUrl(parsedUrl)
if localOptions != nil && err == (*reporter.KpmEvent)(nil) {
return &RegistryOptions{
Local: localOptions,
}, nil
if parsedUrl.Scheme == "" || parsedUrl.Scheme == constants.FileEntry {
localOptions, err := NewLocalOptionsFromUrl(parsedUrl)
if localOptions != nil && err == (*reporter.KpmEvent)(nil) {
return &RegistryOptions{
Local: localOptions,
}, nil
}
}

// parse the options from the git url
// https, http, git and ssh are supported
gitOptions := NewGitOptionsFromUrl(parsedUrl)

if gitOptions != nil {
return &RegistryOptions{
Git: gitOptions,
}, nil
if parsedUrl.Scheme == constants.GitScheme ||
parsedUrl.Scheme == constants.HttpScheme ||
parsedUrl.Scheme == constants.HttpsScheme ||
parsedUrl.Scheme == constants.SshScheme {
gitOptions := NewGitOptionsFromUrl(parsedUrl)

if gitOptions != nil {
return &RegistryOptions{
Git: gitOptions,
}, nil
}
}

// parse the options from the oci url
// oci is supported
ociOptions := NewOciOptionsFromUrl(parsedUrl)
if ociOptions == nil {
ociOptions, err = NewOciOptionsFromRef(rawUrlorOciRef, settings)
if err != nil {
return nil, err
if parsedUrl.Scheme == constants.OciScheme ||
parsedUrl.Scheme == constants.HttpScheme ||
parsedUrl.Scheme == constants.HttpsScheme {
ociOptions := NewOciOptionsFromUrl(parsedUrl)
if ociOptions == nil {
ociOptions, err = NewOciOptionsFromRef(rawUrlorOciRef, settings)
if err != nil {
return nil, err
}
}
}

if ociOptions != nil {
return &RegistryOptions{
Oci: ociOptions,
}, nil
if ociOptions != nil {
return &RegistryOptions{
Oci: ociOptions,
}, nil
}
}

return nil, fmt.Errorf("invalid dependencies source: %s", rawUrlorOciRef)
Expand All @@ -242,13 +262,9 @@ func NewRegistryOptionsFrom(rawUrlorOciRef string, settings *settings.Settings)
// NewGitOptionsFromUrl will parse the git options from the git url.
// https, http, git and ssh are supported.
func NewGitOptionsFromUrl(parsedUrl *url.URL) *GitOptions {
if parsedUrl.Scheme != constants.GitScheme &&
parsedUrl.Scheme != constants.HttpScheme &&
parsedUrl.Scheme != constants.HttpsScheme &&
parsedUrl.Scheme != constants.SshScheme {
if parsedUrl.Scheme == "" {
return nil
}

return &GitOptions{
Url: parsedUrl.Host + parsedUrl.Path,
Branch: parsedUrl.Query().Get(constants.GitBranch),
Expand All @@ -258,12 +274,11 @@ func NewGitOptionsFromUrl(parsedUrl *url.URL) *GitOptions {
}

// NewOciOptionsFromUrl will parse the oci options from the oci url.
// oci is supported.
// https, http, oci is supported.
func NewOciOptionsFromUrl(parsedUrl *url.URL) *OciOptions {
if parsedUrl.Scheme != constants.OciScheme {
if parsedUrl.Scheme == "" {
return nil
}

return &OciOptions{
Reg: parsedUrl.Host,
Repo: parsedUrl.Path,
Expand Down Expand Up @@ -308,10 +323,7 @@ func NewOciOptionsFromRef(refStr string, settings *settings.Settings) (*OciOptio
// NewLocalOptionsFromUrl will parse the local options from the local path.
// scheme 'file' and only path is supported.
func NewLocalOptionsFromUrl(parsedUrl *url.URL) (*LocalOptions, error) {
if parsedUrl.Scheme == "" || parsedUrl.Scheme == constants.FileEntry {
return ParseLocalPathOptions(parsedUrl.Path)
}
return nil, nil
return ParseLocalPathOptions(parsedUrl.Path)
}

// parseOciPkgNameAndVersion will parse package name and version
Expand Down
26 changes: 25 additions & 1 deletion pkg/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package opt

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -32,7 +33,7 @@ func TestInitOptions(t *testing.T) {
assert.Equal(t, o3.Validate(), errors.InvalidVersionFormat)
}

func TestNewOciOptionsFromRef(t *testing.T) {
func TestNewRegistryOptionsFromRef(t *testing.T) {
ref := "test:latest"
settings := settings.GetSettings()
opts, err := NewRegistryOptionsFrom(ref, settings)
Expand Down Expand Up @@ -62,3 +63,26 @@ func TestNewOciOptionsFromRef(t *testing.T) {
assert.Equal(t, opts.Git.Branch, "main")
assert.Equal(t, opts.Git.Url, "github.com/kcl-lang/test1")
}

func TestNewOciOptions(t *testing.T) {
parsedUrl, err := url.Parse("oci://docker.io/kcllang/test1?tag=0.0.1")
assert.Equal(t, err, nil)
ociOptions := NewOciOptionsFromUrl(parsedUrl)
assert.Equal(t, ociOptions.Tag, "0.0.1")
assert.Equal(t, ociOptions.Repo, "/kcllang/test1")
assert.Equal(t, ociOptions.Reg, "docker.io")

parsedUrl, err = url.Parse("http://docker.io/kcllang/test1?tag=0.0.1")
assert.Equal(t, err, nil)
ociOptions = NewOciOptionsFromUrl(parsedUrl)
assert.Equal(t, ociOptions.Tag, "0.0.1")
assert.Equal(t, ociOptions.Repo, "/kcllang/test1")
assert.Equal(t, ociOptions.Reg, "docker.io")

parsedUrl, err = url.Parse("https://docker.io/kcllang/test1?tag=0.0.1")
assert.Equal(t, err, nil)
ociOptions = NewOciOptionsFromUrl(parsedUrl)
assert.Equal(t, ociOptions.Tag, "0.0.1")
assert.Equal(t, ociOptions.Repo, "/kcllang/test1")
assert.Equal(t, ociOptions.Reg, "docker.io")
}
Loading