From 726aac2dd91dc77077559aa0012166b1dd54ac5d Mon Sep 17 00:00:00 2001 From: nexustar Date: Tue, 7 Sep 2021 11:14:59 +0800 Subject: [PATCH] support start playground offline when specify version (#1553) --- cmd/list.go | 14 +++++++---- pkg/repository/v1_repository.go | 41 +++++++++++++++------------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 0fece7bcdd..140d2303ca 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -91,9 +91,11 @@ func (lr *listResult) print() { } func showComponentList(env *environment.Environment, opt listOptions) (*listResult, error) { - err := env.V1Repository().UpdateComponentManifests() - if err != nil { - tui.ColorWarningMsg.Fprint(os.Stderr, "Warn: Update component manifest failed, err_msg=[", err.Error(), "]\n") + if !opt.installedOnly { + err := env.V1Repository().UpdateComponentManifests() + if err != nil { + tui.ColorWarningMsg.Fprint(os.Stderr, "Warn: Update component manifest failed, err_msg=[", err.Error(), "]\n") + } } installed, err := env.Profile().InstalledComponents() @@ -183,7 +185,11 @@ func showComponentList(env *environment.Environment, opt listOptions) (*listResu func showComponentVersions(env *environment.Environment, component string, opt listOptions) (*listResult, error) { var comp *v1manifest.Component var err error - comp, err = env.V1Repository().FetchComponentManifest(component, false) + if opt.installedOnly { + comp, err = env.V1Repository().LocalComponentManifest(component, false) + } else { + comp, err = env.V1Repository().FetchComponentManifest(component, false) + } if err != nil { return nil, errors.Annotate(err, "failed to fetch component") } diff --git a/pkg/repository/v1_repository.go b/pkg/repository/v1_repository.go index 656493b4de..827dc7bbb1 100644 --- a/pkg/repository/v1_repository.go +++ b/pkg/repository/v1_repository.go @@ -729,6 +729,23 @@ func (r *V1Repository) FetchComponentManifest(id string, withYanked bool) (com * return r.updateComponentManifest(id, withYanked) } +// LocalComponentManifest load the component manifest from local. +func (r *V1Repository) LocalComponentManifest(id string, withYanked bool) (com *v1manifest.Component, err error) { + index := v1manifest.Index{} + _, exists, err := r.Local().LoadManifest(&index) + if err != nil { + return nil, err + } + if !exists { + return r.FetchComponentManifest(id, withYanked) + } + + components := index.ComponentList() + comp := components[id] + filename := v1manifest.ComponentManifestFilename(id) + return r.Local().LoadComponentManifest(&comp, filename) +} + // ComponentVersion returns version item of a component func (r *V1Repository) ComponentVersion(id, ver string, includeYanked bool) (*v1manifest.VersionItem, error) { manifest, err := r.FetchComponentManifest(id, includeYanked) @@ -758,27 +775,7 @@ func (r *V1Repository) ComponentVersion(id, ver string, includeYanked bool) (*v1 // LocalComponentVersion returns version item of a component from local manifest file func (r *V1Repository) LocalComponentVersion(id, ver string, includeYanked bool) (*v1manifest.VersionItem, error) { - index := v1manifest.Index{} - _, exists, err := r.Local().LoadManifest(&index) - if err != nil { - return nil, err - } - if !exists { - err = r.ensureManifests() - if err != nil { - return nil, err - } - _, _, err := r.Local().LoadManifest(&index) - - if err != nil { - return nil, err - } - } - - components := index.ComponentList() - comp := components[id] - filename := v1manifest.ComponentManifestFilename(id) - manifest, err := r.Local().LoadComponentManifest(&comp, filename) + manifest, err := r.LocalComponentManifest(id, includeYanked) if err != nil { return nil, err } @@ -806,7 +803,7 @@ func (r *V1Repository) LocalComponentVersion(id, ver string, includeYanked bool) // ResolveComponentVersionWithPlatform resolves the latest version of a component that satisfies the constraint func (r *V1Repository) ResolveComponentVersionWithPlatform(id, constraint, platform string) (utils.Version, error) { - manifest, err := r.FetchComponentManifest(id, false) + manifest, err := r.LocalComponentManifest(id, false) if err != nil { return "", err }