Skip to content

Commit

Permalink
fix: remove all the 'kpm:' prefix in kpm
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 21, 2023
1 parent 3b107b3 commit ec3033d
Show file tree
Hide file tree
Showing 85 changed files with 238 additions and 275 deletions.
4 changes: 2 additions & 2 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestRunPkgInPathInvalidPath(t *testing.T) {
opts.SetPkgPath(filepath.Join(pkgPath, "test_kcl"))
result, err := RunPkgInPath(opts)
assert.NotEqual(t, err, nil)
assert.Equal(t, err.Error(), fmt.Sprintf("kpm: failed to compile the kcl package\nkpm: Cannot find the kcl file, please check the file path %s\n", filepath.Join(pkgPath, "test_kcl", "not_exist.k")))
assert.Equal(t, err.Error(), fmt.Sprintf("error: failed to compile the kcl package\nerror: Cannot find the kcl file, please check the file path %s\n", filepath.Join(pkgPath, "test_kcl", "not_exist.k")))
assert.Equal(t, result, "")
}

Expand All @@ -71,7 +71,7 @@ func TestRunPkgInPathInvalidPkg(t *testing.T) {
opts.Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "invalid_pkg", "not_exist.k")))
result, err := RunPkgInPath(opts)
assert.NotEqual(t, err, nil)
assert.Equal(t, true, strings.Contains(err.Error(), fmt.Sprintf("kpm: could not load 'kcl.mod' in '%s'\n", pkgPath)))
assert.Equal(t, true, strings.Contains(err.Error(), fmt.Sprintf("could not load 'kcl.mod' in '%s'\n", pkgPath)))
assert.Equal(t, result, "")
}

Expand Down
80 changes: 28 additions & 52 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,8 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
// clean the dependencies in kcl.mod.lock which not in kcl.mod
for name := range kclPkg.Dependencies.Deps {
if _, ok := kclPkg.ModFile.Dependencies.Deps[name]; !ok {
reporter.ReportEventTo(
reporter.NewEvent(
reporter.RemoveDep,
fmt.Sprintf("removing '%s'", name),
),
reporter.ReportMsgTo(
fmt.Sprintf("removing '%s'", name),
c.logWriter,
)
delete(kclPkg.Dependencies.Deps, name)
Expand All @@ -177,11 +174,8 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
// add the dependencies in kcl.mod which not in kcl.mod.lock
for name, d := range kclPkg.ModFile.Dependencies.Deps {
if _, ok := kclPkg.Dependencies.Deps[name]; !ok {
reporter.ReportEventTo(
reporter.NewEvent(
reporter.AddDep,
fmt.Sprintf("adding '%s'", name),
),
reporter.ReportMsgTo(
fmt.Sprintf("adding '%s'", name),
c.logWriter,
)
kclPkg.Dependencies.Deps[name] = d
Expand Down Expand Up @@ -446,7 +440,7 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp

// createIfNotExist will create a file if it does not exist.
func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) error {
reporter.ReportMsgTo(fmt.Sprintf("kpm: creating new :%s", filepath), c.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("creating new :%s", filepath), c.GetLogWriter())
err := utils.CreateFileIfNotExist(
filepath,
storeFunc,
Expand All @@ -456,7 +450,7 @@ func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) er
if errEvent.Type() != reporter.FileExists {
return err
} else {
reporter.ReportMsgTo(fmt.Sprintf("kpm: '%s' already exists", filepath), c.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("'%s' already exists", filepath), c.GetLogWriter())
}
} else {
return err
Expand Down Expand Up @@ -494,8 +488,8 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
return nil, err
}

reporter.ReportEventTo(
reporter.NewEvent(reporter.Adding, fmt.Sprintf("adding dependency '%s'.", d.Name)),
reporter.ReportMsgTo(
fmt.Sprintf("adding dependency '%s'", d.Name),
c.logWriter,
)
// 2. download the dependency to the local path.
Expand All @@ -515,11 +509,8 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
succeedMsgInfo = fmt.Sprintf("%s:%s", d.Name, d.Version)
}

reporter.ReportEventTo(
reporter.NewEvent(
reporter.Adding,
fmt.Sprintf("add dependency '%s' successfully.", succeedMsgInfo),
),
reporter.ReportMsgTo(
fmt.Sprintf("add dependency '%s' successfully", succeedMsgInfo),
c.logWriter,
)
return kclPkg, nil
Expand Down Expand Up @@ -564,7 +555,7 @@ func (c *KpmClient) PackagePkg(kclPkg *pkg.KclPkg, vendorMode bool) (string, err
err = c.Package(kclPkg, kclPkg.DefaultTarPath(), vendorMode)

if err != nil {
reporter.ExitWithReport("kpm: failed to package pkg " + kclPkg.GetPkgName() + ".")
reporter.ExitWithReport("failed to package pkg " + kclPkg.GetPkgName() + ".")
return "", err
}
return kclPkg.DefaultTarPath(), nil
Expand Down Expand Up @@ -751,11 +742,8 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend

// DownloadFromGit will download the dependency from the git repository.
func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, error) {
reporter.ReportEventTo(
reporter.NewEvent(
reporter.DownloadingFromGit,
fmt.Sprintf("downloading '%s' with tag '%s'.", dep.Url, dep.Tag),
),
reporter.ReportMsgTo(
fmt.Sprintf("downloading '%s' with tag '%s'", dep.Url, dep.Tag),
c.logWriter,
)

Expand Down Expand Up @@ -787,8 +775,8 @@ func (c *KpmClient) DownloadFromOci(dep *pkg.Oci, localPath string) (string, err
return "", err
}

reporter.ReportEventTo(
reporter.NewEvent(reporter.SelectLatestVersion, "the lastest version '", tagSelected, "' will be added."),
reporter.ReportMsgTo(
fmt.Sprintf("the lastest version '%s' will be added", tagSelected),
c.logWriter,
)

Expand All @@ -798,11 +786,8 @@ func (c *KpmClient) DownloadFromOci(dep *pkg.Oci, localPath string) (string, err
tagSelected = dep.Tag
}

reporter.ReportEventTo(
reporter.NewEvent(
reporter.DownloadingFromOCI,
fmt.Sprintf("downloading '%s:%s' from '%s/%s:%s'.", dep.Repo, tagSelected, dep.Reg, dep.Repo, tagSelected),
),
reporter.ReportMsgTo(
fmt.Sprintf("downloading '%s:%s' from '%s/%s:%s'", dep.Repo, tagSelected, dep.Reg, dep.Repo, tagSelected),
c.logWriter,
)

Expand Down Expand Up @@ -864,24 +849,18 @@ func (c *KpmClient) PullFromOci(localPath, source, tag string) error {
return reporter.NewErrorEvent(
reporter.UnKnownPullWhat,
errors.FailedPull,
"oci url or package name must be specified.",
"oci url or package name must be specified",
)
}

if len(tag) == 0 {
reporter.ReportEventTo(
reporter.NewEvent(
reporter.PullingStarted,
fmt.Sprintf("start to pull '%s'.", source),
),
reporter.ReportMsgTo(
fmt.Sprintf("start to pull '%s'", source),
c.logWriter,
)
} else {
reporter.ReportEventTo(
reporter.NewEvent(
reporter.PullingStarted,
fmt.Sprintf("start to pull '%s' with tag '%s'.", source, tag),
),
reporter.ReportMsgTo(
fmt.Sprintf("start to pull '%s' with tag '%s'", source, tag),
c.logWriter,
)
}
Expand Down Expand Up @@ -930,8 +909,8 @@ func (c *KpmClient) PullFromOci(localPath, source, tag string) error {
)
}

reporter.ReportEventTo(
reporter.NewEvent(reporter.PullingFinished, fmt.Sprintf("pulled '%s' in '%s' successfully.", source, storagePath)),
reporter.ReportMsgTo(
fmt.Sprintf("pulled '%s' in '%s' successfully", source, storagePath),
c.logWriter,
)
return nil
Expand Down Expand Up @@ -1130,20 +1109,17 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
if err != nil {
return err
}
reporter.ReportEventTo(
reporter.NewEvent(reporter.SelectLatestVersion, "the lastest version '", tagSelected, "' will be pulled."),
reporter.ReportMsgTo(
fmt.Sprintf("the lastest version '%s' will be pulled", tagSelected),
c.logWriter,
)
} else {
tagSelected = ociOpts.Tag
}

full_repo := utils.JoinPath(ociOpts.Reg, ociOpts.Repo)
reporter.ReportEventTo(
reporter.NewEvent(
reporter.Pulling,
fmt.Sprintf("pulling '%s:%s' from '%s'.", ociOpts.Repo, tagSelected, full_repo),
),
reporter.ReportMsgTo(
fmt.Sprintf("pulling '%s:%s' from '%s'", ociOpts.Repo, tagSelected, full_repo),
c.logWriter,
)

Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/cmd_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewImportCmd(kpmcli *client.KpmClient) *cli.Command {
},
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("kpm: invalid arguments")
return fmt.Errorf("invalid arguments")
}
inputFile := c.Args().First()

Expand All @@ -68,17 +68,17 @@ func NewImportCmd(kpmcli *client.KpmClient) *cli.Command {
case "auto":
opt.Mode = gen.ModeAuto
default:
return fmt.Errorf("kpm: invalid mode: %s", c.String("mode"))
return fmt.Errorf("invalid mode: %s", c.String("mode"))
}

outputFile := c.String("output")
if outputFile == "" {
outputFile = "generated.k"
reporter.ReportMsgTo("kpm: output file not specified, use default: generated.k", kpmcli.GetLogWriter())
reporter.ReportMsgTo("output file not specified, use default: generated.k", kpmcli.GetLogWriter())
}

if _, err := os.Stat(outputFile); err == nil && !c.Bool("force") {
return fmt.Errorf("kpm: output file already exist, use --force to overwrite: %s", outputFile)
return fmt.Errorf("output file already exist, use --force to overwrite: %s", outputFile)
}

outputWriter, err := os.Create(outputFile)
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 @@ -73,7 +73,7 @@ func NewInitCmd(kpmcli *client.KpmClient) *cli.Command {
return err
}

reporter.ReportMsgTo(fmt.Sprintf("kpm: package '%s' init finished", pkgName), kpmcli.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("package '%s' init finished", pkgName), kpmcli.GetLogWriter())
return nil
},
}
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/cmd_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func NewLoginCmd(kpmcli *client.KpmClient) *cli.Command {
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("registry must be specified"),
"run 'kpm login help' for more information",
)
}
registry := c.Args().First()
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ func NewLogoutCmd(kpmcli *client.KpmClient) *cli.Command {
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("registry must be specified"),
"run 'kpm logout help' for more information",
)
}
err := kpmcli.LogoutOci(c.Args().First())
if err != nil {
return err
}
reporter.ReportMsgTo("kpm: Logout Succeeded", kpmcli.GetLogWriter())
reporter.ReportMsgTo("Logout Succeeded", kpmcli.GetLogWriter())
return nil
},
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command {
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("the directory where the tar is generated is required"),
"run 'kpm pkg help' for more information",
)
}

Expand All @@ -53,7 +52,7 @@ func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command {
kclPkg, err := pkg.LoadKclPkg(pwd)

if err != nil {
reporter.ExitWithReport("kpm: failed to load package in " + pwd + ".")
reporter.ExitWithReport("failed to load package in " + pwd + ".")
return err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli
return reporter.NewErrorEvent(
reporter.UnsupportOciUrlScheme,
errors.InvalidOciUrl,
"only support url scheme 'oci://'.",
"only support url scheme 'oci://'",
)
}

Expand All @@ -173,7 +173,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, kpmcli *cli
return err
}

reporter.ReportMsgTo(fmt.Sprintf("kpm: package '%s' will be pushed", kclPkg.GetPkgName()), kpmcli.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("package '%s' will be pushed", kclPkg.GetPkgName()), kpmcli.GetLogWriter())
// 4. Push it.
err = kpmcli.PushToOci(tarPath, ociOpts)
if err != (*reporter.KpmEvent)(nil) {
Expand Down
52 changes: 26 additions & 26 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,54 @@ import (
"errors"
)

var FailedDownloadError = errors.New("kpm: failed to download dependency")
var FailedDownloadError = errors.New("failed to download dependency")
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.")
var InternalBug = errors.New("kpm: internal bug, please contact us and we will fix the problem.")
var FailedToLoadPackage = errors.New("kpm: failed to load package, please check the package path is valid.")
var FailedToVendorDependency = errors.New("failed to vendor dependency")
var FailedToPackage = errors.New("failed to package.")
var InvalidDependency = errors.New("invalid dependency.")
var InternalBug = errors.New("internal bug, please contact us and we will fix the problem.")
var FailedToLoadPackage = errors.New("failed to load package, please check the package path is valid.")

// Invalid Options Format Errors
// Invalid 'kpm init'
var InvalidInitOptions = errors.New("kpm: invalid 'kpm init' argument, you must provide a name for the package to be initialized.")
var InvalidInitOptions = errors.New("invalid 'kpm init' argument, you must provide a name for the package to be initialized.")

// Invalid 'kpm add'
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 InvalidAddOptions = errors.New("invalid 'kpm add' argument, you must provide a package name or url for the package")
var InvalidAddOptionsInvalidGitUrl = errors.New("invalid 'kpm add' argument, you must provide a Git Url for the package.")
var InvalidAddOptionsInvalidOciRef = errors.New("invalid 'kpm add' argument, you must provide a valid Oci Ref 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.")
var EntryFileNotFound = errors.New("kpm: entry file cannot be found, please make sure the '--input' entry file can be found")
var CompileFailed = errors.New("kpm: failed to compile kcl.")
var FailedUnTarKclPackage = errors.New("kpm: failed to untar kcl package, please re-download")
var UnknownTarFormat = errors.New("kpm: unknown tar format.")
var KclPacakgeTarNotFound = errors.New("kpm: the kcl package tar path is not found")
var InvalidKclPacakgeTar = errors.New("kpm: the kcl package tar path is an invalid *.tar file")
var InvalidRunOptionsWithoutEntryFiles = errors.New("invalid 'kpm run' argument, you must provide an entry file.")
var EntryFileNotFound = errors.New("entry file cannot be found, please make sure the '--input' entry file can be found")
var CompileFailed = errors.New("failed to compile kcl.")
var FailedUnTarKclPackage = errors.New("failed to untar kcl package, please re-download")
var UnknownTarFormat = errors.New("unknown tar format.")
var KclPacakgeTarNotFound = errors.New("the kcl package tar path is not found")
var InvalidKclPacakgeTar = errors.New("the kcl package tar path is an invalid *.tar file")

// Invalid KCL_PKG_PATH
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.")
var FailedLogout = errors.New("kpm: failed to logout, the registry not logged in.")
var FailedPull = errors.New("failed to pull kcl package.")
var FailedPushToOci = errors.New("kpm: failed to push kcl package tar to oci.")
var FailedLogin = errors.New("failed to login, please check registry, username and password is valid.")
var FailedLogout = errors.New("failed to logout, the registry not logged in.")
var FailedPull = errors.New("failed to pull kcl package")
var FailedPushToOci = errors.New("failed to push kcl package tar to oci.")
var InvalidOciRef = errors.New("invalid oci reference.")
var NotOciUrl = errors.New("kpm: url is not an oci url.")
var IsOciRef = errors.New("kpm: oci ref is not an url.")
var NotOciUrl = errors.New("url is not an oci url.")
var IsOciRef = errors.New("oci ref is not an url.")

// Invalid Version
var InvalidVersionFormat = errors.New("kpm: failed to parse version.")
var InvalidVersionFormat = errors.New("failed to parse version.")
var PathNotFound = errors.New("path not found.")
var PathIsEmpty = errors.New("path is empty.")
var InvalidPkg = errors.New("invalid kcl package.")
var InvalidOciUrl = errors.New("invalid oci url.")
var UnknownEnv = errors.New("invalid environment variable.")
var InvalidOciUrl = errors.New("invalid oci url")
var UnknownEnv = errors.New("invalid environment variable")

// No kcl files
var NoKclFiles = errors.New("No input KCL files")
Loading

0 comments on commit ec3033d

Please sign in to comment.