Skip to content

Commit e104bef

Browse files
authored
feat: make 'kpm add' feel better. (#143)
1 parent 81663d0 commit e104bef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+253
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ kpm
2323
# e2e test cases dir
2424
!kpm/
2525
scripts/pkg_in_reg/*
26+
scripts/registry_auth/*

pkg/cmd/cmd_add.go

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,74 +34,75 @@ func NewAddCmd() *cli.Command {
3434
},
3535

3636
Action: func(c *cli.Context) error {
37-
// 1. get settings from the global config file.
38-
settings := settings.GetSettings()
39-
if settings.ErrorEvent != nil {
40-
return settings.ErrorEvent
41-
}
37+
return KpmAdd(c)
38+
},
39+
}
40+
}
4241

43-
// 2. acquire the lock of the package cache.
44-
err := settings.AcquirePackageCacheLock()
45-
if err != nil {
46-
return err
47-
}
42+
func KpmAdd(c *cli.Context) error {
43+
// 1. get settings from the global config file.
44+
settings := settings.GetSettings()
45+
if settings.ErrorEvent != (*reporter.KpmEvent)(nil) {
46+
return settings.ErrorEvent
47+
}
4848

49-
defer func() {
50-
// 3. release the lock of the package cache after the function returns.
51-
releaseErr := settings.ReleasePackageCacheLock()
52-
if releaseErr != nil && err == nil {
53-
err = releaseErr
54-
}
55-
}()
49+
// 2. acquire the lock of the package cache.
50+
err := settings.AcquirePackageCacheLock()
51+
if err != nil {
52+
return err
53+
}
5654

57-
pwd, err := os.Getwd()
55+
defer func() {
56+
// 3. release the lock of the package cache after the function returns.
57+
releaseErr := settings.ReleasePackageCacheLock()
58+
if releaseErr != nil && err == nil {
59+
err = releaseErr
60+
}
61+
}()
5862

59-
if err != nil {
60-
reporter.Fatal("kpm: internal bugs, please contact us to fix it")
61-
}
63+
pwd, err := os.Getwd()
6264

63-
globalPkgPath, err := env.GetAbsPkgPath()
64-
if err != nil {
65-
return err
66-
}
65+
if err != nil {
66+
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
67+
}
6768

68-
kclPkg, err := pkg.LoadKclPkg(pwd)
69-
if err != nil {
70-
reporter.Fatal("kpm: could not load `kcl.mod` in `", pwd, "`")
71-
}
69+
globalPkgPath, err := env.GetAbsPkgPath()
70+
if err != nil {
71+
return err
72+
}
7273

73-
err = kclPkg.ValidateKpmHome(globalPkgPath)
74-
if err != nil {
75-
return err
76-
}
74+
kclPkg, err := pkg.LoadKclPkg(pwd)
75+
if err != nil {
76+
return err
77+
}
7778

78-
addOpts, err := parseAddOptions(c, globalPkgPath)
79-
if err != nil {
80-
return err
81-
}
79+
err = kclPkg.ValidateKpmHome(globalPkgPath)
80+
if err != (*reporter.KpmEvent)(nil) {
81+
return err
82+
}
8283

83-
err = addOpts.Validate()
84-
if err != nil {
85-
return err
86-
}
84+
addOpts, err := parseAddOptions(c, globalPkgPath)
85+
if err != nil {
86+
return err
87+
}
8788

88-
err = kclPkg.AddDeps(addOpts)
89-
if err != nil {
90-
return err
91-
}
92-
reporter.Report("kpm: add dependency successfully.")
93-
return nil
94-
},
89+
err = addOpts.Validate()
90+
if err != nil {
91+
return err
92+
}
93+
94+
err = kclPkg.AddDeps(addOpts)
95+
if err != nil {
96+
return err
9597
}
98+
return nil
9699
}
97100

98101
// onlyOnceOption is used to check that the value of some parameters can only appear once.
99-
func onlyOnceOption(c *cli.Context, name string) (*string, error) {
102+
func onlyOnceOption(c *cli.Context, name string) (*string, *reporter.KpmEvent) {
100103
inputOpt := c.StringSlice(name)
101104
if len(inputOpt) > 1 {
102-
reporter.ExitWithReport("kpm: the argument '", name, "' cannot be used multiple times")
103-
reporter.ExitWithReport("kpm: run 'kpm add help' for more information.")
104-
return nil, fmt.Errorf("kpm: Invalid command")
105+
return nil, reporter.NewErrorEvent(reporter.InvalidCmd, fmt.Errorf("the argument '%s' cannot be used multiple times", name))
105106
} else if len(inputOpt) == 1 {
106107
return &inputOpt[0], nil
107108
} else {
@@ -114,7 +115,10 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
114115
// parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'.
115116
if c.NArg() == 0 {
116117
gitOpts, err := parseGitRegistryOptions(c)
117-
if err != nil {
118+
if err != (*reporter.KpmEvent)(nil) {
119+
if err.Type() == reporter.InvalidGitUrl {
120+
return nil, reporter.NewErrorEvent(reporter.InvalidCmd, errors.InvalidAddOptions)
121+
}
118122
return nil, err
119123
}
120124
return &opt.AddOptions{
@@ -135,19 +139,27 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
135139
}
136140

137141
// parseGitRegistryOptions will parse the git registry information from user cli inputs.
138-
func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, error) {
142+
func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.KpmEvent) {
139143
gitUrl, err := onlyOnceOption(c, "git")
140144

141-
if err != nil {
142-
return nil, nil
145+
if err != (*reporter.KpmEvent)(nil) {
146+
return nil, err
143147
}
144148

145149
gitTag, err := onlyOnceOption(c, "tag")
146150

147-
if err != nil {
151+
if err != (*reporter.KpmEvent)(nil) {
148152
return nil, err
149153
}
150154

155+
if gitUrl == nil {
156+
return nil, reporter.NewErrorEvent(reporter.InvalidGitUrl, fmt.Errorf("the argument 'git' is required"))
157+
}
158+
159+
if gitTag == nil {
160+
return nil, reporter.NewErrorEvent(reporter.WithoutGitTag, fmt.Errorf("the argument 'tag' is required"))
161+
}
162+
151163
return &opt.RegistryOptions{
152164
Git: &opt.GitOptions{
153165
Url: *gitUrl,
@@ -188,11 +200,11 @@ func parseOciPkgNameAndVersion(s string) (string, string, error) {
188200
}
189201

190202
if len(parts) > 2 {
191-
return "", "", errors.InvalidAddOptionsInvalidOciRef
203+
return "", "", reporter.NewErrorEvent(reporter.InvalidPkgRef, fmt.Errorf("invalid oci package reference '%s'", s))
192204
}
193205

194206
if parts[1] == "" {
195-
return "", "", errors.InvalidAddOptionsInvalidOciRef
207+
return "", "", reporter.NewErrorEvent(reporter.InvalidPkgRef, fmt.Errorf("invalid oci package reference '%s'", s))
196208
}
197209

198210
return parts[0], parts[1], nil

pkg/cmd/cmd_init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewInitCmd() *cli.Command {
6262

6363
err = kclPkg.ValidateKpmHome(globalPkgPath)
6464

65-
if err != nil {
65+
if err != (*reporter.KpmEvent)(nil) {
6666
return err
6767
}
6868

pkg/cmd/cmd_metadata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"kcl-lang.io/kpm/pkg/env"
1111
"kcl-lang.io/kpm/pkg/errors"
1212
pkg "kcl-lang.io/kpm/pkg/package"
13+
"kcl-lang.io/kpm/pkg/reporter"
1314
)
1415

1516
// NewMetadataCmd new a Command for `kpm metadata`.
@@ -53,7 +54,7 @@ func NewMetadataCmd() *cli.Command {
5354
kclPkg.SetVendorMode(c.Bool(FLAG_VENDOR))
5455

5556
err = kclPkg.ValidateKpmHome(globalPkgPath)
56-
if err != nil {
57+
if err != (*reporter.KpmEvent)(nil) {
5758
return err
5859
}
5960

pkg/cmd/cmd_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func runPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclA
182182
}
183183

184184
err = kclPkg.ValidateKpmHome(globalPkgPath)
185-
if err != nil {
185+
if err != (*reporter.KpmEvent)(nil) {
186186
return "", err
187187
}
188188

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "test_failed_update_conf"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+

pkg/cmd/test_data/test_failed_update_conf/kcl.mod.lock

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'

pkg/env/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"os"
55
"path/filepath"
66

7-
"kcl-lang.io/kpm/pkg/errors"
7+
"kcl-lang.io/kpm/pkg/reporter"
88
"kcl-lang.io/kpm/pkg/utils"
99
)
1010

@@ -30,14 +30,14 @@ func GetAbsPkgPath() (string, error) {
3030
if kpmHome == "" {
3131
defaultHome, err := utils.CreateSubdirInUserHome(GetKpmSubDir())
3232
if err != nil {
33-
return "", errors.InternalBug
33+
return "", reporter.NewErrorEvent(reporter.FailedAccessPkgPath, err, "could not access $KCL_PKG_PATH.")
3434
}
3535
kpmHome = defaultHome
3636
}
3737

3838
kpmHome, err := filepath.Abs(kpmHome)
3939
if err != nil {
40-
return "", errors.InternalBug
40+
return "", reporter.NewErrorEvent(reporter.FailedAccessPkgPath, err, "could not access $KCL_PKG_PATH.")
4141
}
4242

4343
return kpmHome, nil

pkg/errors/errors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
var FailedDownloadError = errors.New("kpm: failed to download dependency")
8-
var CheckSumMismatchError = errors.New("kpm: checksum mismatch")
8+
var CheckSumMismatchError = errors.New("checksum mismatch")
99
var FailedToVendorDependency = errors.New("kpm: failed to vendor dependency")
1010
var FailedToPackage = errors.New("kpm: failed to package.")
1111
var InvalidDependency = errors.New("kpm: invalid dependency.")
@@ -17,12 +17,12 @@ var FailedToLoadPackage = errors.New("kpm: failed to load package, please check
1717
var InvalidInitOptions = errors.New("kpm: invalid 'kpm init' argument, you must provide a name for the package to be initialized.")
1818

1919
// Invalid 'kpm add'
20-
var InvalidAddOptionsWithoutRegistry = errors.New("kpm: invalid 'kpm add' argument, you must provide a registry url for the package.")
20+
var InvalidAddOptions = errors.New("invalid 'kpm add' argument, you must provide a package name or url for the package.")
2121
var InvalidAddOptionsInvalidGitUrl = errors.New("kpm: invalid 'kpm add' argument, you must provide a Git Url for the package.")
2222
var InvalidAddOptionsInvalidOciRef = errors.New("kpm: invalid 'kpm add' argument, you must provide a valid Oci Ref for the package.")
2323

24-
var InvalidAddOptionsInvalidOciReg = errors.New("kpm: invalid 'kpm add' argument, you must provide a Reg for the package.")
25-
var InvalidAddOptionsInvalidOciRepo = errors.New("kpm: invalid 'kpm add' argument, you must provide a Repo for the package.")
24+
var InvalidAddOptionsInvalidOciReg = errors.New("invalid 'kpm add' argument, you must provide a Reg for the package.")
25+
var InvalidAddOptionsInvalidOciRepo = errors.New("invalid 'kpm add' argument, you must provide a Repo for the package.")
2626

2727
// Invalid 'kpm run'
2828
var InvalidRunOptionsWithoutEntryFiles = errors.New("kpm: invalid 'kpm run' argument, you must provide an entry file.")
@@ -34,7 +34,7 @@ var KclPacakgeTarNotFound = errors.New("kpm: the kcl package tar path is not fou
3434
var InvalidKclPacakgeTar = errors.New("kpm: the kcl package tar path is an invalid *.tar file")
3535

3636
// Invalid KCL_PKG_PATH
37-
var InvalidKpmHomeInCurrentPkg = errors.New("kpm: environment variable KCL_PKG_PATH cannot be set to the same path as the current KCL package.")
37+
var InvalidKpmHomeInCurrentPkg = errors.New("environment variable KCL_PKG_PATH cannot be set to the same path as the current KCL package.")
3838

3939
// Invalid oci
4040
var FailedLogin = errors.New("kpm: failed to login, please check registry, username and password is valid.")

0 commit comments

Comments
 (0)