From 661539ae7bbec232225cdcecccd9f1740076af8c Mon Sep 17 00:00:00 2001 From: peefy Date: Thu, 22 Aug 2024 20:03:52 +0800 Subject: [PATCH] refactor: mod init format Signed-off-by: peefy --- pkg/client/client.go | 2 +- pkg/client/client_test.go | 46 +++++++++---------- .../test_data/test_data_add_deps/kcl.mod | 1 - .../test_data/test_init_empty_mod/kcl.mod | 2 - pkg/cmd/cmd_add.go | 18 -------- pkg/downloader/source.go | 4 +- pkg/opt/opt.go | 45 ++++++++++++------ pkg/package/modfile.go | 8 ++-- pkg/package/package.go | 4 +- pkg/package/test_data/store_mod_file/kcl.mod | 2 - .../test_data/test_data_modfile/kcl.mod | 2 - .../test_data/test_init_empty_mod/kcl.mod | 2 - pkg/package/toml.go | 3 +- pkg/utils/utils.go | 16 +------ pkg/utils/utils_test.go | 3 +- 15 files changed, 67 insertions(+), 91 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 26c50088..66f77acb 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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 } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index c2b1d189..67f588f8 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -936,7 +936,7 @@ 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") @@ -944,7 +944,7 @@ func TestParseOciOptionFromString(t *testing.T) { 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") @@ -952,7 +952,7 @@ func TestParseOciOptionFromString(t *testing.T) { 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") @@ -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, @@ -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, @@ -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", }, }, } @@ -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", }, }, } @@ -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, }, }, } diff --git a/pkg/client/test_data/test_data_add_deps/kcl.mod b/pkg/client/test_data/test_data_add_deps/kcl.mod index 909a628f..c35d627c 100644 --- a/pkg/client/test_data/test_data_add_deps/kcl.mod +++ b/pkg/client/test_data/test_data_add_deps/kcl.mod @@ -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" } - diff --git a/pkg/client/test_data/test_init_empty_mod/kcl.mod b/pkg/client/test_data/test_init_empty_mod/kcl.mod index ca1c5d84..5c2f4052 100644 --- a/pkg/client/test_data/test_init_empty_mod/kcl.mod +++ b/pkg/client/test_data/test_init_empty_mod/kcl.mod @@ -2,5 +2,3 @@ name = "test_name" edition = "v0.10.0" version = "0.0.1" - - diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index 30761bdf..694de7d0 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -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 -} diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index 2576bbee..e9274340 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -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) { @@ -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 diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 02309a55..32d9f963 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -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), } } @@ -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 } @@ -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 } @@ -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 { diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 0f72cf32..011b1f0d 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -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, }, @@ -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, diff --git a/pkg/package/package.go b/pkg/package/package.go index 57426b62..328412fb 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -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) } diff --git a/pkg/package/test_data/store_mod_file/kcl.mod b/pkg/package/test_data/store_mod_file/kcl.mod index cab62f23..97e4a2ea 100644 --- a/pkg/package/test_data/store_mod_file/kcl.mod +++ b/pkg/package/test_data/store_mod_file/kcl.mod @@ -2,5 +2,3 @@ name = "test_name" edition = "0.0.1" version = "0.0.1" - - diff --git a/pkg/package/test_data/test_data_modfile/kcl.mod b/pkg/package/test_data/test_data_modfile/kcl.mod index d8198722..2a3402bc 100644 --- a/pkg/package/test_data/test_data_modfile/kcl.mod +++ b/pkg/package/test_data/test_data_modfile/kcl.mod @@ -2,5 +2,3 @@ name = "test_kcl_pkg" edition = "v0.10.0" version = "0.0.1" - - diff --git a/pkg/package/test_data/test_init_empty_mod/kcl.mod b/pkg/package/test_data/test_init_empty_mod/kcl.mod index ca1c5d84..5c2f4052 100644 --- a/pkg/package/test_data/test_init_empty_mod/kcl.mod +++ b/pkg/package/test_data/test_init_empty_mod/kcl.mod @@ -2,5 +2,3 @@ name = "test_name" edition = "v0.10.0" version = "0.0.1" - - diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 76569b8d..c37c0e75 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -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()) @@ -80,6 +79,7 @@ func (dep *Dependencies) MarshalTOML() string { sb.WriteString(NEWLINE) sb.WriteString(dep.MarshalTOML()) } + sb.WriteString(NEWLINE) } return sb.String() } @@ -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()) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 6c7cdff2..8d24b7f4 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -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 diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index fc36bde2..265b4dc8 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -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") @@ -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) -} \ No newline at end of file +}