Skip to content

Commit

Permalink
feat: make 'kpm add' feel better. (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe authored Jul 28, 2023
1 parent 81663d0 commit e104bef
Show file tree
Hide file tree
Showing 56 changed files with 253 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ kpm
# e2e test cases dir
!kpm/
scripts/pkg_in_reg/*
scripts/registry_auth/*
130 changes: 71 additions & 59 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,74 +34,75 @@ func NewAddCmd() *cli.Command {
},

Action: func(c *cli.Context) error {
// 1. get settings from the global config file.
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
return settings.ErrorEvent
}
return KpmAdd(c)
},
}
}

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

defer func() {
// 3. release the lock of the package cache after the function returns.
releaseErr := settings.ReleasePackageCacheLock()
if releaseErr != nil && err == nil {
err = releaseErr
}
}()
// 2. acquire the lock of the package cache.
err := settings.AcquirePackageCacheLock()
if err != nil {
return err
}

pwd, err := os.Getwd()
defer func() {
// 3. release the lock of the package cache after the function returns.
releaseErr := settings.ReleasePackageCacheLock()
if releaseErr != nil && err == nil {
err = releaseErr
}
}()

if err != nil {
reporter.Fatal("kpm: internal bugs, please contact us to fix it")
}
pwd, err := os.Getwd()

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
return err
}
if err != nil {
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}

kclPkg, err := pkg.LoadKclPkg(pwd)
if err != nil {
reporter.Fatal("kpm: could not load `kcl.mod` in `", pwd, "`")
}
globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
return err
}

err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != nil {
return err
}
kclPkg, err := pkg.LoadKclPkg(pwd)
if err != nil {
return err
}

addOpts, err := parseAddOptions(c, globalPkgPath)
if err != nil {
return err
}
err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != (*reporter.KpmEvent)(nil) {
return err
}

err = addOpts.Validate()
if err != nil {
return err
}
addOpts, err := parseAddOptions(c, globalPkgPath)
if err != nil {
return err
}

err = kclPkg.AddDeps(addOpts)
if err != nil {
return err
}
reporter.Report("kpm: add dependency successfully.")
return nil
},
err = addOpts.Validate()
if err != nil {
return err
}

err = kclPkg.AddDeps(addOpts)
if err != nil {
return err
}
return nil
}

// onlyOnceOption is used to check that the value of some parameters can only appear once.
func onlyOnceOption(c *cli.Context, name string) (*string, error) {
func onlyOnceOption(c *cli.Context, name string) (*string, *reporter.KpmEvent) {
inputOpt := c.StringSlice(name)
if len(inputOpt) > 1 {
reporter.ExitWithReport("kpm: the argument '", name, "' cannot be used multiple times")
reporter.ExitWithReport("kpm: run 'kpm add help' for more information.")
return nil, fmt.Errorf("kpm: Invalid command")
return nil, reporter.NewErrorEvent(reporter.InvalidCmd, fmt.Errorf("the argument '%s' cannot be used multiple times", name))
} else if len(inputOpt) == 1 {
return &inputOpt[0], nil
} else {
Expand All @@ -114,7 +115,10 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
// parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'.
if c.NArg() == 0 {
gitOpts, err := parseGitRegistryOptions(c)
if err != nil {
if err != (*reporter.KpmEvent)(nil) {
if err.Type() == reporter.InvalidGitUrl {
return nil, reporter.NewErrorEvent(reporter.InvalidCmd, errors.InvalidAddOptions)
}
return nil, err
}
return &opt.AddOptions{
Expand All @@ -135,19 +139,27 @@ func parseAddOptions(c *cli.Context, localPath string) (*opt.AddOptions, error)
}

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

if err != nil {
return nil, nil
if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

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

if err != nil {
if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

if gitUrl == nil {
return nil, reporter.NewErrorEvent(reporter.InvalidGitUrl, fmt.Errorf("the argument 'git' is required"))
}

if gitTag == nil {
return nil, reporter.NewErrorEvent(reporter.WithoutGitTag, fmt.Errorf("the argument 'tag' is required"))
}

return &opt.RegistryOptions{
Git: &opt.GitOptions{
Url: *gitUrl,
Expand Down Expand Up @@ -188,11 +200,11 @@ func parseOciPkgNameAndVersion(s string) (string, string, error) {
}

if len(parts) > 2 {
return "", "", errors.InvalidAddOptionsInvalidOciRef
return "", "", reporter.NewErrorEvent(reporter.InvalidPkgRef, fmt.Errorf("invalid oci package reference '%s'", s))
}

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

return parts[0], parts[1], nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewInitCmd() *cli.Command {

err = kclPkg.ValidateKpmHome(globalPkgPath)

if err != nil {
if err != (*reporter.KpmEvent)(nil) {
return err
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/cmd_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/errors"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
)

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

err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != nil {
if err != (*reporter.KpmEvent)(nil) {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func runPkgInPath(pkgPath string, entryFilePaths []string, vendorMode bool, kclA
}

err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != nil {
if err != (*reporter.KpmEvent)(nil) {
return "", err
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/test_data/test_failed_update_conf/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "test_failed_update_conf"
edition = "0.0.1"
version = "0.0.1"

Empty file.
1 change: 1 addition & 0 deletions pkg/cmd/test_data/test_failed_update_conf/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
6 changes: 3 additions & 3 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"path/filepath"

"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/utils"
)

Expand All @@ -30,14 +30,14 @@ func GetAbsPkgPath() (string, error) {
if kpmHome == "" {
defaultHome, err := utils.CreateSubdirInUserHome(GetKpmSubDir())
if err != nil {
return "", errors.InternalBug
return "", reporter.NewErrorEvent(reporter.FailedAccessPkgPath, err, "could not access $KCL_PKG_PATH.")
}
kpmHome = defaultHome
}

kpmHome, err := filepath.Abs(kpmHome)
if err != nil {
return "", errors.InternalBug
return "", reporter.NewErrorEvent(reporter.FailedAccessPkgPath, err, "could not access $KCL_PKG_PATH.")
}

return kpmHome, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

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

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

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

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

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

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

0 comments on commit e104bef

Please sign in to comment.