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: mod init format #460

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ func (c *KpmClient) InitEmptyPkg(kclPkg *pkg.KclPkg) error {
return err
}

err = c.createIfNotExist(filepath.Join(kclPkg.ModFile.HomePath, constants.DEFAULT_KCL_FILE_NAME), kclPkg.CreateDefauleMain)
err = c.createIfNotExist(filepath.Join(kclPkg.ModFile.HomePath, constants.DEFAULT_KCL_FILE_NAME), kclPkg.CreateDefaultMain)
if err != nil {
return err
}
Expand Down
46 changes: 23 additions & 23 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,23 +936,23 @@ func TestParseOciOptionFromString(t *testing.T) {
oci_ref_with_tag := "test_oci_repo:test_oci_tag"
ociOption, err := kpmcli.ParseOciOptionFromString(oci_ref_with_tag, "test_tag")
assert.Equal(t, err, nil)
assert.Equal(t, ociOption.PkgName, "")
assert.Equal(t, ociOption.Ref, "")
assert.Equal(t, ociOption.Reg, "ghcr.io")
assert.Equal(t, ociOption.Repo, "kcl-lang/test_oci_repo")
assert.Equal(t, ociOption.Tag, "test_oci_tag")

oci_ref_without_tag := "test_oci_repo:test_oci_tag"
ociOption, err = kpmcli.ParseOciOptionFromString(oci_ref_without_tag, "test_tag")
assert.Equal(t, err, nil)
assert.Equal(t, ociOption.PkgName, "")
assert.Equal(t, ociOption.Ref, "")
assert.Equal(t, ociOption.Reg, "ghcr.io")
assert.Equal(t, ociOption.Repo, "kcl-lang/test_oci_repo")
assert.Equal(t, ociOption.Tag, "test_oci_tag")

oci_url_with_tag := "oci://test_reg/test_oci_repo"
ociOption, err = kpmcli.ParseOciOptionFromString(oci_url_with_tag, "test_tag")
assert.Equal(t, err, nil)
assert.Equal(t, ociOption.PkgName, "")
assert.Equal(t, ociOption.Ref, "")
assert.Equal(t, ociOption.Reg, "test_reg")
assert.Equal(t, ociOption.Repo, "/test_oci_repo")
assert.Equal(t, ociOption.Tag, "test_tag")
Expand Down Expand Up @@ -1108,10 +1108,10 @@ func TestAddWithNoSumCheck(t *testing.T) {
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: "0.1.0",
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Ref: "helloworld",
Tag: "0.1.0",
},
},
NoSumCheck: true,
Expand Down Expand Up @@ -1250,10 +1250,10 @@ func TestAddWithDiffVersionNoSumCheck(t *testing.T) {
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: "0.1.2",
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Ref: "helloworld",
Tag: "0.1.2",
},
},
NoSumCheck: true,
Expand Down Expand Up @@ -1314,10 +1314,10 @@ func TestAddWithDiffVersionWithSumCheck(t *testing.T) {
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: "0.1.2",
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Ref: "helloworld",
Tag: "0.1.2",
},
},
}
Expand Down Expand Up @@ -1494,10 +1494,10 @@ func TestAddWithLocalPath(t *testing.T) {
LocalPath: tmpPkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: "0.1.1",
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Ref: "helloworld",
Tag: "0.1.1",
},
},
}
Expand Down Expand Up @@ -1740,10 +1740,10 @@ func testAddDefaultRegistryDep(t *testing.T) {
LocalPath: tc.pkgPath,
RegistryOpts: opt.RegistryOptions{
Registry: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
PkgName: "helloworld",
Tag: tc.tag,
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Ref: "helloworld",
Tag: tc.tag,
},
},
}
Expand Down
1 change: 0 additions & 1 deletion pkg/client/test_data/test_data_add_deps/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ version = "0.0.1"
[dependencies]
oci_name = { oci = "oci://test_reg/test_repo", tag = "test_tag" }
name = { git = "test_url", tag = "test_tag" }

2 changes: 0 additions & 2 deletions pkg/client/test_data/test_init_empty_mod/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
name = "test_name"
edition = "v0.10.0"
version = "0.0.1"


18 changes: 0 additions & 18 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,3 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
},
}, nil
}

// parseOciRegistryOptions will parse the oci registry information from user cli inputs.
func parseRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) {
ociPkgRef := c.Args().First()
name, version, err := opt.ParseOciPkgNameAndVersion(ociPkgRef)
if err != nil {
return nil, err
}

return &opt.RegistryOptions{
Registry: &opt.OciOptions{
Reg: kpmcli.GetSettings().DefaultOciRegistry(),
Repo: kpmcli.GetSettings().DefaultOciRepo(),
PkgName: name,
Tag: version,
},
}, nil
}
4 changes: 2 additions & 2 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (local *Local) IsDir() bool {
return false
}

return local != nil && utils.DirExists(local.Path) && fileInfo.IsDir()
return utils.DirExists(local.Path) && fileInfo.IsDir()
}

func (local *Local) FindRootPath() (string, error) {
Expand Down Expand Up @@ -213,7 +213,7 @@ func (git *Git) ToFilePath() (string, error) {
}

func (git *Git) GetPackage() string {
if(git == nil) {
if git == nil {
return ""
}
return git.Package
Expand Down
45 changes: 31 additions & 14 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ func NewOciOptionsFromUrl(parsedUrl *url.URL) *OciOptions {
parsedUrl.Scheme = constants.HttpsScheme
}
return &OciOptions{
Reg: parsedUrl.Host,
Repo: parsedUrl.Path,
Tag: parsedUrl.Query().Get(constants.Tag),
PkgName: filepath.Base(parsedUrl.Path),
Reg: parsedUrl.Host,
Repo: parsedUrl.Path,
Tag: parsedUrl.Query().Get(constants.Tag),
Ref: filepath.Base(parsedUrl.Path),
}
}

Expand Down Expand Up @@ -330,10 +330,10 @@ func NewOciOptionsFromRef(refStr string, settings *settings.Settings) (*OciOptio
}

return &OciOptions{
Reg: reg,
Repo: repo,
Tag: tag,
PkgName: filepath.Base(repo),
Reg: reg,
Repo: repo,
Tag: tag,
Ref: filepath.Base(repo),
}, nil
}

Expand Down Expand Up @@ -393,12 +393,28 @@ func (opts *GitOptions) Validate() error {
}

// OciOptions for download oci packages.
// kpm will download packages from oci registry by '{Reg}/{Repo}/{PkgName}:{Tag}'.
// kpm will download packages from oci registry by '{Reg}/{Repo}/{Ref}:{Tag}'.
type OciOptions struct {
Reg string
Repo string
Tag string
PkgName string
// OCI registry in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
// +required
Reg string
// OCI repo in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
// +required
Repo string
// OCI tag in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
// +optional
Tag string
// OCI package name in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
// +required
Ref string
// Package denotes which sub package will be added as the dependency.
// The root path of OCI Package will be used as a dependency, and if this parameter
// is set, packages with matching names will be searched in the OCI package
// for as dependencies.
// +optional
Package string
// Annotations denotes the additional annotation map for the OCI manifest.
// +optional
Annotations map[string]string
}

Expand All @@ -412,7 +428,8 @@ func (opts *OciOptions) Validate() error {
// LocalOptions for local packages.
// kpm will find packages from local path.
type LocalOptions struct {
Path string
Path string
Package string
}

func (opts *LocalOptions) Validate() error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
}

return &Dependency{
Name: opt.Oci.PkgName,
FullName: opt.Oci.PkgName + "_" + opt.Oci.Tag,
Name: opt.Oci.Ref,
FullName: opt.Oci.Ref + "_" + opt.Oci.Tag,
Source: downloader.Source{
Oci: &ociSource,
},
Expand Down Expand Up @@ -550,8 +550,8 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
}

return &Dependency{
Name: opt.Registry.PkgName,
FullName: opt.Registry.PkgName + "_" + opt.Registry.Tag,
Name: opt.Registry.Ref,
FullName: opt.Registry.Ref + "_" + opt.Registry.Tag,
Source: downloader.Source{
Registry: &downloader.Registry{
Oci: &ociSource,
Expand Down
4 changes: 2 additions & 2 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ func (kclPkg *KclPkg) LockDepsVersion() error {
return utils.StoreToFile(fullPath, lockToml)
}

// CreateDefauleMain will create a default main.k file in the current kcl package.
func (kclPkg *KclPkg) CreateDefauleMain() error {
// CreateDefaultMain will create a default main.k file in the current kcl package.
func (kclPkg *KclPkg) CreateDefaultMain() error {
mainKPath := filepath.Join(kclPkg.HomePath, constants.DEFAULT_KCL_FILE_NAME)
return utils.StoreToFile(mainKPath, constants.DEFAULT_KCL_FILE_CONTENT)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/package/test_data/store_mod_file/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
name = "test_name"
edition = "0.0.1"
version = "0.0.1"


2 changes: 0 additions & 2 deletions pkg/package/test_data/test_data_modfile/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
name = "test_kcl_pkg"
edition = "v0.10.0"
version = "0.0.1"


2 changes: 0 additions & 2 deletions pkg/package/test_data/test_init_empty_mod/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
name = "test_name"
edition = "v0.10.0"
version = "0.0.1"


3 changes: 1 addition & 2 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (pkg *Package) MarshalTOML() string {
sb.WriteString(NEWLINE)
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(pkg); err != nil {
fmt.Println(err)
return ""
}
sb.WriteString(buf.String())
Expand All @@ -80,6 +79,7 @@ func (dep *Dependencies) MarshalTOML() string {
sb.WriteString(NEWLINE)
sb.WriteString(dep.MarshalTOML())
}
sb.WriteString(NEWLINE)
}
return sb.String()
}
Expand All @@ -104,7 +104,6 @@ func (p *Profile) MarshalTOML() string {
sb.WriteString(NEWLINE)
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).Encode(p); err != nil {
fmt.Println(err)
return ""
}
sb.WriteString(buf.String())
Expand Down
16 changes: 2 additions & 14 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,9 @@ func HashDir(dir string) (string, error) {

// StoreToFile will store 'data' into toml file under 'filePath'.
func StoreToFile(filePath string, dataStr string) error {
file, err := os.Create(filePath)
err := os.WriteFile(filePath, []byte(dataStr), 0644)
if err != nil {
reporter.ExitWithReport("failed to create file: ", filePath, err)
return err
}
defer file.Close()

file, err = os.Create(filePath)

if err != nil {
return err
}
defer file.Close()

if _, err := io.WriteString(file, dataStr); err != nil {
reporter.ExitWithReport("failed to write file: ", filePath, err)
return err
}
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ func TestIsModRelativePath(t *testing.T) {
assert.Equal(t, IsModRelativePath("xxx/xxx/xxx"), false)
}


func TestFindPackage(t *testing.T) {
testDir := getTestDir("test_find_package")
correctAddress := filepath.Join(testDir, "test_2")
Expand All @@ -314,4 +313,4 @@ func TestFindPackage(t *testing.T) {
func TestMatchesPackageName(t *testing.T) {
address := filepath.Join(getTestDir("test_find_package"), "test_2", "kcl.mod")
assert.Equal(t, matchesPackageName(address, "test_find_package"), true)
}
}
Loading