Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make 'kpm pull' diagnostics feel better. #129

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions kpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (

func main() {
reporter.InitReporter()
setting, err := settings.GetSettings()
if err != nil {
reporter.Fatal(err)
setting := settings.GetSettings()
if setting.ErrorEvent != nil {
reporter.Fatal(setting.ErrorEvent)
}
app := cli.NewApp()
app.Name = "kpm"
Expand All @@ -37,7 +37,7 @@ func main() {
cmd.NewPushCmd(setting),
cmd.NewPullCmd(),
}
err = app.Run(os.Args)
err := app.Run(os.Args)
if err != nil {
reporter.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func NewAddCmd() *cli.Command {

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

// 2. acquire the lock of the package cache.
err = settings.AcquirePackageCacheLock()
err := settings.AcquirePackageCacheLock()
if err != nil {
return err
}
Expand Down Expand Up @@ -164,9 +164,9 @@ func parseOciRegistryOptions(c *cli.Context) (*opt.RegistryOptions, error) {
return nil, err
}

settings, err := settings.GetSettings()
if err != nil {
return nil, err
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
return nil, settings.ErrorEvent
}

return &opt.RegistryOptions{
Expand Down
177 changes: 106 additions & 71 deletions pkg/cmd/cmd_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"fmt"
"net/url"
"os"
"path/filepath"
Expand All @@ -29,77 +30,111 @@ func NewPullCmd() *cli.Command {
},
},
Action: func(c *cli.Context) error {
tag := c.String(FLAG_TAG)
ociUrlOrPkgName := c.Args().Get(0)
localPath := c.Args().Get(1)

if len(ociUrlOrPkgName) == 0 {
reporter.Report("kpm: oci url or package name must be specified.")
reporter.ExitWithReport("kpm: run 'kpm pull help' for more information.")
}

if len(tag) == 0 {
reporter.Report("kpm: pulling '", ociUrlOrPkgName, "'.")
} else {
reporter.Report("kpm: pulling '", ociUrlOrPkgName, "' with tag '", tag, "'.")
}

ociOpt, err := opt.ParseOciOptionFromOciUrl(ociUrlOrPkgName, tag)

if err == errors.IsOciRef {
settings, err := settings.GetSettings()
if err != nil {
return err
}

urlpath, err := url.JoinPath(settings.DefaultOciRepo(), ociUrlOrPkgName)
if err != nil {
return err
}

ociOpt, err = opt.ParseOciRef(urlpath)
if err != nil {
return err
}
} else if err != nil {
return err
}

absPullPath, err := filepath.Abs(localPath)
if err != nil {
return err
}

tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return errors.InternalBug
}
// clean the temp dir.
defer os.RemoveAll(tmpDir)

localPath = ociOpt.AddStoragePathSuffix(tmpDir)

// 2. Pull the tar.
err = oci.Pull(localPath, ociOpt.Reg, ociOpt.Repo, ociOpt.Tag)

if err != nil {
return err
}

// 3. Get the (*.tar) file path.
matches, err := filepath.Glob(filepath.Join(localPath, KCL_PKG_TAR))
if err != nil || len(matches) != 1 {
return errors.FailedPullFromOci
}

// 4. Untar the tar file.
err = utils.UnTarDir(matches[0], ociOpt.AddStoragePathSuffix(absPullPath))
if err != nil {
return err
}

reporter.Report("kpm: the kcl package is pulled successfully.")
return nil
return KpmPull(c)
},
}
}

func KpmPull(c *cli.Context) error {
tag := c.String(FLAG_TAG)
ociUrlOrPkgName := c.Args().Get(0)
localPath := c.Args().Get(1)

if len(ociUrlOrPkgName) == 0 {
return reporter.NewErrorEvent(
reporter.UnKnownPullWhat,
errors.FailedPull,
"oci url or package name must be specified.",
)
}

if len(tag) == 0 {
reporter.ReportEventToStdout(
reporter.NewEvent(
reporter.PullingStarted,
fmt.Sprintf("start to pull '%s'.", ociUrlOrPkgName),
),
)
} else {
reporter.ReportEventToStdout(
reporter.NewEvent(
reporter.PullingStarted,
fmt.Sprintf("start to pull '%s' with tag '%s'.", ociUrlOrPkgName, tag),
),
)
}

ociOpt, event := opt.ParseOciOptionFromOciUrl(ociUrlOrPkgName, tag)

if event.Type() == reporter.IsNotUrl || event.Type() == reporter.UrlSchemeNotOci {
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
return settings.ErrorEvent
}

urlpath, err := url.JoinPath(settings.DefaultOciRepo(), ociUrlOrPkgName)
if err != nil {
return reporter.NewErrorEvent(reporter.Bug, err)
}

ociOpt, err = opt.ParseOciRef(urlpath)
if err != nil {
return err
}
} else if event != nil {
return event
}

absPullPath, err := filepath.Abs(localPath)
if err != nil {
return reporter.NewErrorEvent(reporter.Bug, err)
}

tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return reporter.NewErrorEvent(reporter.Bug, err, fmt.Sprintf("failed to create temp dir '%s'.", tmpDir))
}

// clean the temp dir.
defer os.RemoveAll(tmpDir)

localPath = ociOpt.AddStoragePathSuffix(tmpDir)

// 2. Pull the tar.
err = oci.Pull(localPath, ociOpt.Reg, ociOpt.Repo, ociOpt.Tag)

if err != nil {
return err
}

// 3. Get the (*.tar) file path.
tarPath := filepath.Join(localPath, KCL_PKG_TAR)
matches, err := filepath.Glob(tarPath)
if err != nil || len(matches) != 1 {
if err == nil {
err = errors.InvalidPkg
}

return reporter.NewErrorEvent(
reporter.InvalidKclPkg,
err,
fmt.Sprintf("failed to find the kcl package tar from '%s'.", tarPath),
)
}

// 4. Untar the tar file.
storagePath := ociOpt.AddStoragePathSuffix(absPullPath)
err = utils.UnTarDir(matches[0], storagePath)
if err != nil {
return reporter.NewErrorEvent(
reporter.FailedUntarKclPkg,
err,
fmt.Sprintf("failed to untar the kcl package tar from '%s' into '%s'.", matches[0], storagePath),
)
}

reporter.ReportEventToStdout(
reporter.NewEvent(reporter.PullingFinished, fmt.Sprintf("pulled '%s' in '%s' successfully.", ociUrlOrPkgName, storagePath)),
)
return nil
}
12 changes: 8 additions & 4 deletions pkg/cmd/cmd_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func NewPushCmd(settings *settings.Settings) *cli.Command {

// genDefaultOciUrlForKclPkg will generate the default oci url from the current package.
func genDefaultOciUrlForKclPkg(pkg *pkg.KclPkg) (string, error) {
settings, err := settings.GetSettings()
if err != nil {
return "", err
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
return "", settings.ErrorEvent
}

urlPath, err := url.JoinPath(settings.DefaultOciRepo(), pkg.GetPkgName())
Expand Down Expand Up @@ -154,7 +154,11 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, settings *settings.Settings)
// 3. Generate the OCI options from oci url and the version of current kcl package.
ociOpts, err := opt.ParseOciOptionFromOciUrl(ociUrl, kclPkg.GetPkgTag())
if err != nil {
return err
return reporter.NewErrorEvent(
reporter.UnsupportOciUrlScheme,
errors.InvalidOciUrl,
"only support url scheme 'oci://'.",
)
}

reporter.Report("kpm: package '" + kclPkg.GetPkgName() + "' will be pushed.")
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 @@ -135,7 +135,7 @@ func runOci(ociRef, version string, entryFiles []string, vendorMode bool, kclArg
// 3.Get the (*.tar) file path.
matches, err := filepath.Glob(filepath.Join(localPath, KCL_PKG_TAR))
if err != nil || len(matches) != 1 {
return "", errors.FailedPullFromOci
return "", errors.FailedPull
}

return runTar(matches[0], entryFiles, vendorMode, kclArgs)
Expand Down
11 changes: 8 additions & 3 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package errors

import "errors"
import (
"errors"
)

var FailedDownloadError = errors.New("kpm: failed to download dependency")
var CheckSumMismatchError = errors.New("kpm: checksum mismatch")
Expand Down Expand Up @@ -37,11 +39,14 @@ var InvalidKpmHomeInCurrentPkg = errors.New("kpm: environment variable KCL_PKG_P
// 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 FailedPullFromOci = errors.New("kpm: failed to pull kcl package tar from oci.")
var FailedPull = errors.New("failed to pull kcl package.")
var FailedPushToOci = errors.New("kpm: failed to push kcl package tar to oci.")
var InvalidOciRef = errors.New("kpm: invalid oci reference.")
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.")

// Invalid Version
var InvalidVersionFormat = errors.New("kpm: failed to parse version.")

var InvalidPkg = errors.New("invalid kcl package.")
var InvalidOciUrl = errors.New("invalid oci url.")
8 changes: 4 additions & 4 deletions pkg/mod/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ type Dependency struct {
// FillDepInfo will fill registry information for a dependency.
func (dep *Dependency) FillDepInfo() error {
if dep.Source.Oci != nil {
settings, err := settings.GetSettings()
if err != nil {
return err
settings := settings.GetSettings()
if settings.ErrorEvent != nil {
return settings.ErrorEvent
}
dep.Source.Oci.Reg = settings.DefaultOciRegistry()
urlpath, err := url.JoinPath(settings.DefaultOciRepo(), dep.Name)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (dep *Oci) Download(localPath string) (string, error) {

matches, err := filepath.Glob(filepath.Join(localPath, "*.tar"))
if err != nil || len(matches) != 1 {
return "", errors.FailedPullFromOci
return "", errors.FailedPull
}

tarPath := matches[0]
Expand Down
Loading