Skip to content

Commit cf34f96

Browse files
authored
Merge pull request #460 from Peefy/refactor-mod-init-format
refactor: mod init format
2 parents ce9dfee + 661539a commit cf34f96

File tree

15 files changed

+67
-91
lines changed

15 files changed

+67
-91
lines changed

pkg/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ func (c *KpmClient) InitEmptyPkg(kclPkg *pkg.KclPkg) error {
738738
return err
739739
}
740740

741-
err = c.createIfNotExist(filepath.Join(kclPkg.ModFile.HomePath, constants.DEFAULT_KCL_FILE_NAME), kclPkg.CreateDefauleMain)
741+
err = c.createIfNotExist(filepath.Join(kclPkg.ModFile.HomePath, constants.DEFAULT_KCL_FILE_NAME), kclPkg.CreateDefaultMain)
742742
if err != nil {
743743
return err
744744
}

pkg/client/client_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -964,23 +964,23 @@ func TestParseOciOptionFromString(t *testing.T) {
964964
oci_ref_with_tag := "test_oci_repo:test_oci_tag"
965965
ociOption, err := kpmcli.ParseOciOptionFromString(oci_ref_with_tag, "test_tag")
966966
assert.Equal(t, err, nil)
967-
assert.Equal(t, ociOption.PkgName, "")
967+
assert.Equal(t, ociOption.Ref, "")
968968
assert.Equal(t, ociOption.Reg, "ghcr.io")
969969
assert.Equal(t, ociOption.Repo, "kcl-lang/test_oci_repo")
970970
assert.Equal(t, ociOption.Tag, "test_oci_tag")
971971

972972
oci_ref_without_tag := "test_oci_repo:test_oci_tag"
973973
ociOption, err = kpmcli.ParseOciOptionFromString(oci_ref_without_tag, "test_tag")
974974
assert.Equal(t, err, nil)
975-
assert.Equal(t, ociOption.PkgName, "")
975+
assert.Equal(t, ociOption.Ref, "")
976976
assert.Equal(t, ociOption.Reg, "ghcr.io")
977977
assert.Equal(t, ociOption.Repo, "kcl-lang/test_oci_repo")
978978
assert.Equal(t, ociOption.Tag, "test_oci_tag")
979979

980980
oci_url_with_tag := "oci://test_reg/test_oci_repo"
981981
ociOption, err = kpmcli.ParseOciOptionFromString(oci_url_with_tag, "test_tag")
982982
assert.Equal(t, err, nil)
983-
assert.Equal(t, ociOption.PkgName, "")
983+
assert.Equal(t, ociOption.Ref, "")
984984
assert.Equal(t, ociOption.Reg, "test_reg")
985985
assert.Equal(t, ociOption.Repo, "/test_oci_repo")
986986
assert.Equal(t, ociOption.Tag, "test_tag")
@@ -1136,10 +1136,10 @@ func TestAddWithNoSumCheck(t *testing.T) {
11361136
LocalPath: pkgPath,
11371137
RegistryOpts: opt.RegistryOptions{
11381138
Oci: &opt.OciOptions{
1139-
Reg: "ghcr.io",
1140-
Repo: "kcl-lang/helloworld",
1141-
PkgName: "helloworld",
1142-
Tag: "0.1.0",
1139+
Reg: "ghcr.io",
1140+
Repo: "kcl-lang/helloworld",
1141+
Ref: "helloworld",
1142+
Tag: "0.1.0",
11431143
},
11441144
},
11451145
NoSumCheck: true,
@@ -1278,10 +1278,10 @@ func TestAddWithDiffVersionNoSumCheck(t *testing.T) {
12781278
LocalPath: pkgPath,
12791279
RegistryOpts: opt.RegistryOptions{
12801280
Oci: &opt.OciOptions{
1281-
Reg: "ghcr.io",
1282-
Repo: "kcl-lang/helloworld",
1283-
PkgName: "helloworld",
1284-
Tag: "0.1.2",
1281+
Reg: "ghcr.io",
1282+
Repo: "kcl-lang/helloworld",
1283+
Ref: "helloworld",
1284+
Tag: "0.1.2",
12851285
},
12861286
},
12871287
NoSumCheck: true,
@@ -1342,10 +1342,10 @@ func TestAddWithDiffVersionWithSumCheck(t *testing.T) {
13421342
LocalPath: pkgPath,
13431343
RegistryOpts: opt.RegistryOptions{
13441344
Oci: &opt.OciOptions{
1345-
Reg: "ghcr.io",
1346-
Repo: "kcl-lang/helloworld",
1347-
PkgName: "helloworld",
1348-
Tag: "0.1.2",
1345+
Reg: "ghcr.io",
1346+
Repo: "kcl-lang/helloworld",
1347+
Ref: "helloworld",
1348+
Tag: "0.1.2",
13491349
},
13501350
},
13511351
}
@@ -1522,10 +1522,10 @@ func TestAddWithLocalPath(t *testing.T) {
15221522
LocalPath: tmpPkgPath,
15231523
RegistryOpts: opt.RegistryOptions{
15241524
Oci: &opt.OciOptions{
1525-
Reg: "ghcr.io",
1526-
Repo: "kcl-lang/helloworld",
1527-
PkgName: "helloworld",
1528-
Tag: "0.1.1",
1525+
Reg: "ghcr.io",
1526+
Repo: "kcl-lang/helloworld",
1527+
Ref: "helloworld",
1528+
Tag: "0.1.1",
15291529
},
15301530
},
15311531
}
@@ -1768,10 +1768,10 @@ func testAddDefaultRegistryDep(t *testing.T) {
17681768
LocalPath: tc.pkgPath,
17691769
RegistryOpts: opt.RegistryOptions{
17701770
Registry: &opt.OciOptions{
1771-
Reg: "ghcr.io",
1772-
Repo: "kcl-lang/helloworld",
1773-
PkgName: "helloworld",
1774-
Tag: tc.tag,
1771+
Reg: "ghcr.io",
1772+
Repo: "kcl-lang/helloworld",
1773+
Ref: "helloworld",
1774+
Tag: tc.tag,
17751775
},
17761776
},
17771777
}

pkg/client/test_data/test_data_add_deps/kcl.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ version = "0.0.1"
66
[dependencies]
77
oci_name = { oci = "oci://test_reg/test_repo", tag = "test_tag" }
88
name = { git = "test_url", tag = "test_tag" }
9-

pkg/client/test_data/test_init_empty_mod/kcl.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
name = "test_name"
33
edition = "v0.10.0"
44
version = "0.0.1"
5-
6-

pkg/cmd/cmd_add.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,3 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
225225
},
226226
}, nil
227227
}
228-
229-
// parseOciRegistryOptions will parse the oci registry information from user cli inputs.
230-
func parseRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) {
231-
ociPkgRef := c.Args().First()
232-
name, version, err := opt.ParseOciPkgNameAndVersion(ociPkgRef)
233-
if err != nil {
234-
return nil, err
235-
}
236-
237-
return &opt.RegistryOptions{
238-
Registry: &opt.OciOptions{
239-
Reg: kpmcli.GetSettings().DefaultOciRegistry(),
240-
Repo: kpmcli.GetSettings().DefaultOciRepo(),
241-
PkgName: name,
242-
Tag: version,
243-
},
244-
}, nil
245-
}

pkg/downloader/source.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (local *Local) IsDir() bool {
118118
return false
119119
}
120120

121-
return local != nil && utils.DirExists(local.Path) && fileInfo.IsDir()
121+
return utils.DirExists(local.Path) && fileInfo.IsDir()
122122
}
123123

124124
func (local *Local) FindRootPath() (string, error) {
@@ -213,7 +213,7 @@ func (git *Git) ToFilePath() (string, error) {
213213
}
214214

215215
func (git *Git) GetPackage() string {
216-
if(git == nil) {
216+
if git == nil {
217217
return ""
218218
}
219219
return git.Package

pkg/opt/opt.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ func NewOciOptionsFromUrl(parsedUrl *url.URL) *OciOptions {
297297
parsedUrl.Scheme = constants.HttpsScheme
298298
}
299299
return &OciOptions{
300-
Reg: parsedUrl.Host,
301-
Repo: parsedUrl.Path,
302-
Tag: parsedUrl.Query().Get(constants.Tag),
303-
PkgName: filepath.Base(parsedUrl.Path),
300+
Reg: parsedUrl.Host,
301+
Repo: parsedUrl.Path,
302+
Tag: parsedUrl.Query().Get(constants.Tag),
303+
Ref: filepath.Base(parsedUrl.Path),
304304
}
305305
}
306306

@@ -330,10 +330,10 @@ func NewOciOptionsFromRef(refStr string, settings *settings.Settings) (*OciOptio
330330
}
331331

332332
return &OciOptions{
333-
Reg: reg,
334-
Repo: repo,
335-
Tag: tag,
336-
PkgName: filepath.Base(repo),
333+
Reg: reg,
334+
Repo: repo,
335+
Tag: tag,
336+
Ref: filepath.Base(repo),
337337
}, nil
338338
}
339339

@@ -393,12 +393,28 @@ func (opts *GitOptions) Validate() error {
393393
}
394394

395395
// OciOptions for download oci packages.
396-
// kpm will download packages from oci registry by '{Reg}/{Repo}/{PkgName}:{Tag}'.
396+
// kpm will download packages from oci registry by '{Reg}/{Repo}/{Ref}:{Tag}'.
397397
type OciOptions struct {
398-
Reg string
399-
Repo string
400-
Tag string
401-
PkgName string
398+
// OCI registry in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
399+
// +required
400+
Reg string
401+
// OCI repo in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
402+
// +required
403+
Repo string
404+
// OCI tag in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
405+
// +optional
406+
Tag string
407+
// OCI package name in the format '{Reg}/{Repo}/{Ref}:{Tag}'.
408+
// +required
409+
Ref string
410+
// Package denotes which sub package will be added as the dependency.
411+
// The root path of OCI Package will be used as a dependency, and if this parameter
412+
// is set, packages with matching names will be searched in the OCI package
413+
// for as dependencies.
414+
// +optional
415+
Package string
416+
// Annotations denotes the additional annotation map for the OCI manifest.
417+
// +optional
402418
Annotations map[string]string
403419
}
404420

@@ -412,7 +428,8 @@ func (opts *OciOptions) Validate() error {
412428
// LocalOptions for local packages.
413429
// kpm will find packages from local path.
414430
type LocalOptions struct {
415-
Path string
431+
Path string
432+
Package string
416433
}
417434

418435
func (opts *LocalOptions) Validate() error {

pkg/package/modfile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
516516
}
517517

518518
return &Dependency{
519-
Name: opt.Oci.PkgName,
520-
FullName: opt.Oci.PkgName + "_" + opt.Oci.Tag,
519+
Name: opt.Oci.Ref,
520+
FullName: opt.Oci.Ref + "_" + opt.Oci.Tag,
521521
Source: downloader.Source{
522522
Oci: &ociSource,
523523
},
@@ -550,8 +550,8 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
550550
}
551551

552552
return &Dependency{
553-
Name: opt.Registry.PkgName,
554-
FullName: opt.Registry.PkgName + "_" + opt.Registry.Tag,
553+
Name: opt.Registry.Ref,
554+
FullName: opt.Registry.Ref + "_" + opt.Registry.Tag,
555555
Source: downloader.Source{
556556
Registry: &downloader.Registry{
557557
Oci: &ociSource,

pkg/package/package.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ func (kclPkg *KclPkg) LockDepsVersion() error {
228228
return utils.StoreToFile(fullPath, lockToml)
229229
}
230230

231-
// CreateDefauleMain will create a default main.k file in the current kcl package.
232-
func (kclPkg *KclPkg) CreateDefauleMain() error {
231+
// CreateDefaultMain will create a default main.k file in the current kcl package.
232+
func (kclPkg *KclPkg) CreateDefaultMain() error {
233233
mainKPath := filepath.Join(kclPkg.HomePath, constants.DEFAULT_KCL_FILE_NAME)
234234
return utils.StoreToFile(mainKPath, constants.DEFAULT_KCL_FILE_CONTENT)
235235
}

pkg/package/test_data/store_mod_file/kcl.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
name = "test_name"
33
edition = "0.0.1"
44
version = "0.0.1"
5-
6-

pkg/package/test_data/test_data_modfile/kcl.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
name = "test_kcl_pkg"
33
edition = "v0.10.0"
44
version = "0.0.1"
5-
6-

pkg/package/test_data/test_init_empty_mod/kcl.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@
22
name = "test_name"
33
edition = "v0.10.0"
44
version = "0.0.1"
5-
6-

pkg/package/toml.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ func (pkg *Package) MarshalTOML() string {
5959
sb.WriteString(NEWLINE)
6060
var buf bytes.Buffer
6161
if err := toml.NewEncoder(&buf).Encode(pkg); err != nil {
62-
fmt.Println(err)
6362
return ""
6463
}
6564
sb.WriteString(buf.String())
@@ -80,6 +79,7 @@ func (dep *Dependencies) MarshalTOML() string {
8079
sb.WriteString(NEWLINE)
8180
sb.WriteString(dep.MarshalTOML())
8281
}
82+
sb.WriteString(NEWLINE)
8383
}
8484
return sb.String()
8585
}
@@ -104,7 +104,6 @@ func (p *Profile) MarshalTOML() string {
104104
sb.WriteString(NEWLINE)
105105
var buf bytes.Buffer
106106
if err := toml.NewEncoder(&buf).Encode(p); err != nil {
107-
fmt.Println(err)
108107
return ""
109108
}
110109
sb.WriteString(buf.String())

pkg/utils/utils.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,9 @@ func HashDir(dir string) (string, error) {
7272

7373
// StoreToFile will store 'data' into toml file under 'filePath'.
7474
func StoreToFile(filePath string, dataStr string) error {
75-
file, err := os.Create(filePath)
75+
err := os.WriteFile(filePath, []byte(dataStr), 0644)
7676
if err != nil {
77-
reporter.ExitWithReport("failed to create file: ", filePath, err)
78-
return err
79-
}
80-
defer file.Close()
81-
82-
file, err = os.Create(filePath)
83-
84-
if err != nil {
85-
return err
86-
}
87-
defer file.Close()
88-
89-
if _, err := io.WriteString(file, dataStr); err != nil {
77+
reporter.ExitWithReport("failed to write file: ", filePath, err)
9078
return err
9179
}
9280
return nil

pkg/utils/utils_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ func TestIsModRelativePath(t *testing.T) {
303303
assert.Equal(t, IsModRelativePath("xxx/xxx/xxx"), false)
304304
}
305305

306-
307306
func TestFindPackage(t *testing.T) {
308307
testDir := getTestDir("test_find_package")
309308
correctAddress := filepath.Join(testDir, "test_2")
@@ -314,4 +313,4 @@ func TestFindPackage(t *testing.T) {
314313
func TestMatchesPackageName(t *testing.T) {
315314
address := filepath.Join(getTestDir("test_find_package"), "test_2", "kcl.mod")
316315
assert.Equal(t, matchesPackageName(address, "test_find_package"), true)
317-
}
316+
}

0 commit comments

Comments
 (0)