diff --git a/.idea/copyright/apache.xml b/.idea/copyright/apache.xml index 15fff88a..441c115a 100644 --- a/.idea/copyright/apache.xml +++ b/.idea/copyright/apache.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/cmd/commands/list.go b/cmd/commands/list.go index a804e571..4d079aef 100644 --- a/cmd/commands/list.go +++ b/cmd/commands/list.go @@ -41,8 +41,7 @@ func listCmd(ctx *cli.Context) error { return err } if len(allSdk) == 0 { - pterm.Println("You don't have any sdk installed yet.") - return nil + return fmt.Errorf("you don't have any sdk installed yet") } tree := pterm.LeveledList{} for name, s := range allSdk { @@ -65,8 +64,7 @@ func listCmd(ctx *cli.Context) error { curVersion := source.Current() list := source.List() if len(list) == 0 { - pterm.Println("No available version.") - return nil + return fmt.Errorf("no available version") } for _, version := range list { if version == curVersion { diff --git a/cmd/commands/search.go b/cmd/commands/search.go index 513188b0..7ad5e765 100644 --- a/cmd/commands/search.go +++ b/cmd/commands/search.go @@ -18,7 +18,6 @@ package commands import ( "fmt" - "github.com/pterm/pterm" "github.com/urfave/cli/v2" "github.com/version-fox/vfox/internal/printer" "github.com/version-fox/vfox/internal/sdk" @@ -44,12 +43,10 @@ func searchCmd(ctx *cli.Context) error { } result, err := source.Available() if err != nil { - pterm.Printf("Plugin [Available] error: %s\n", err) - return nil + return fmt.Errorf("plugin [Available] method error: %w", err) } if len(result) == 0 { - pterm.Println("No Available version.") - return nil + return fmt.Errorf("no available version") } kvSelect := printer.PageKVSelect{ TopText: "Please select a version of " + sdkName, @@ -91,8 +88,7 @@ func searchCmd(ctx *cli.Context) error { } version, err := kvSelect.Show() if err != nil { - pterm.Printf("Select version error, err: %s\n", err) - return err + return fmt.Errorf("select version error: %w", err) } return source.Install(sdk.Version(version.Key)) } diff --git a/cmd/commands/update.go b/cmd/commands/update.go index 3260b6b5..20a9ee6f 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -35,6 +35,5 @@ func updateCmd(ctx *cli.Context) error { } manager := sdk.NewSdkManager() defer manager.Close() - _ = manager.Update(args.First()) - return nil + return manager.Update(args.First()) } diff --git a/internal/sdk/manager.go b/internal/sdk/manager.go index fa4ec8a6..8ce876d5 100644 --- a/internal/sdk/manager.go +++ b/internal/sdk/manager.go @@ -163,20 +163,17 @@ func (m *Manager) Update(pluginName string) error { pterm.Printf("Checking %s plugin...\n", updateUrl) content, err := m.loadLuaFromFileOrUrl(updateUrl) if err != nil { - pterm.Printf("Failed to load %s plugin, err: %s\n", updateUrl, err) - return fmt.Errorf("fetch plugin failed") + return fmt.Errorf("fetch plugin failed, err: %w", err) } source, err := NewLuaPlugin(content, updateUrl, m.osType, m.archType) if err != nil { - pterm.Printf("Check %s plugin failed, err: %s\n", updateUrl, err) - return err + return fmt.Errorf("check %s plugin failed, err: %w", updateUrl, err) } success := false backupPath := sdk.Plugin.SourcePath + ".bak" err = util.CopyFile(sdk.Plugin.SourcePath, backupPath) if err != nil { - pterm.Printf("Backup %s plugin failed, err: %s\n", updateUrl, err) - return fmt.Errorf("backup failed") + return fmt.Errorf("backup %s plugin failed, err: %w", updateUrl, err) } defer func() { if success { @@ -187,13 +184,11 @@ func (m *Manager) Update(pluginName string) error { }() pterm.Println("Checking plugin version...") if util.CompareVersion(source.Version, sdk.Plugin.Version) <= 0 { - pterm.Println("The plugin is already the latest version.") - return fmt.Errorf("already the latest version") + return fmt.Errorf("the plugin is already the latest version") } err = os.WriteFile(sdk.Plugin.SourcePath, []byte(content), 0644) if err != nil { - pterm.Printf("Update %s plugin failed, err: %s\n", updateUrl, err) - return fmt.Errorf("write file error") + return fmt.Errorf("update %s plugin failed: %w", updateUrl, err) } success = true pterm.Printf("Update %s plugin successfully! version: %s \n", pterm.LightGreen(pluginName), pterm.LightBlue(source.Version)) @@ -206,8 +201,7 @@ func (m *Manager) Add(pluginName, url, alias string) error { if len(url) == 0 { args := strings.Split(pluginName, "/") if len(args) < 2 { - pterm.Println("Invalid plugin name. Format: /") - return fmt.Errorf("invalid plugin name") + return fmt.Errorf("invalid plugin name, format: /") } category := args[0] name := args[1] @@ -234,27 +228,23 @@ func (m *Manager) Add(pluginName, url, alias string) error { destPath := filepath.Join(m.PathMeta.PluginPath, pname+".lua") if util.FileExists(destPath) { - pterm.Printf("Plugin %s already exists, please use %s to remove it first.\n", pterm.LightGreen(pname), pterm.LightBlue("vfox remove "+pname)) - return fmt.Errorf("plugin already exists") + return fmt.Errorf("plugin %s already exists", pname) } pterm.Printf("Adding plugin from %s...\n", url) content, err := m.loadLuaFromFileOrUrl(url) if err != nil { - pterm.Printf("Failed to load %s plugin, err: %s\n", url, err) - return fmt.Errorf("install failed") + return fmt.Errorf("failed to load plugin: %w", err) } pterm.Println("Checking plugin...") source, err := NewLuaPlugin(content, url, m.osType, m.archType) if err != nil { - pterm.Printf("Check %s plugin failed, err: %s\n", url, err) - return err + return fmt.Errorf("check plugin error: %w", err) } defer source.Close() err = os.WriteFile(destPath, []byte(content), 0644) if err != nil { - pterm.Printf("Add %s plugin failed, err: %s\n", url, err) - return fmt.Errorf("write file error") + return fmt.Errorf("add plugin error: %w", err) } pterm.Println("Plugin info:") pterm.Println("Name ", "->", pterm.LightBlue(source.Name)) @@ -312,22 +302,19 @@ func (m *Manager) Available() ([]*Category, error) { // TODO proxy resp, err := http.Get(pluginIndexUrl) if err != nil { - pterm.Printf("Get plugin index error, err: %s\n", err) + return nil, fmt.Errorf("get plugin index error: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - pterm.Printf("Get plugin index error, status code: %d\n", resp.StatusCode) - return nil, fmt.Errorf("get plugin index error") + return nil, fmt.Errorf("get plugin index error, status code: %d", resp.StatusCode) } if str, err := io.ReadAll(resp.Body); err != nil { - pterm.Printf("Read plugin index error, err: %s\n", err) - return nil, fmt.Errorf("read plugin index error") + return nil, fmt.Errorf("read plugin index error: %w", err) } else { var categories []*Category err = json.Unmarshal(str, &categories) if err != nil { - pterm.Printf("Parse plugin index error, err: %s\n", err) - return nil, fmt.Errorf("parse plugin index error") + return nil, fmt.Errorf("parse plugin index error: %w", err) } return categories, nil } diff --git a/internal/sdk/sdk.go b/internal/sdk/sdk.go index 2db1f6e7..b08faa74 100644 --- a/internal/sdk/sdk.go +++ b/internal/sdk/sdk.go @@ -48,12 +48,10 @@ type Sdk struct { func (b *Sdk) Install(version Version) error { installInfo, err := b.Plugin.PreInstall(version) if err != nil { - pterm.Printf("Plugin [PreInstall] error: %s\n", err.Error()) - return err + return fmt.Errorf("plugin [PreInstall] method error: %w", err) } if installInfo == nil { - pterm.Println("No information about the current version") - return fmt.Errorf("no version") + return fmt.Errorf("no information about the current version") } mainSdk := installInfo.Main success := false @@ -67,8 +65,7 @@ func (b *Sdk) Install(version Version) error { }() label := b.label(mainSdk.Version) if b.checkExists(mainSdk.Version) { - pterm.Printf("%s is already installed.\n", pterm.LightGreen(label)) - return fmt.Errorf("%s has been installed\n", label) + return fmt.Errorf("%s is already installed", label) } var installedSdkInfos []*Info path, err := b.installSdk(mainSdk, newDirPath) @@ -95,7 +92,10 @@ func (b *Sdk) Install(version Version) error { } } success = true - _ = b.Plugin.PostInstall(newDirPath, installedSdkInfos) + err = b.Plugin.PostInstall(newDirPath, installedSdkInfos) + if err != nil { + return fmt.Errorf("plugin [PostInstall] method error: %w", err) + } pterm.Printf("Please use %s to use it.\n", pterm.LightBlue(fmt.Sprintf("vfox use %s", label))) return nil } @@ -184,7 +184,6 @@ func (b *Sdk) Use(version Version, scope UseScope) error { } label := b.label(version) if !b.checkExists(version) { - pterm.Printf("No %s installed, please install it first.", pterm.Yellow(label)) return fmt.Errorf("%s is not installed", label) } // TODO Need to optimize envManager @@ -196,8 +195,7 @@ func (b *Sdk) Use(version Version, scope UseScope) error { } keys, err := b.Plugin.EnvKeys(sdkPackage) if err != nil { - pterm.Printf("Plugin [EnvKeys] error: err:%s\n", err.Error()) - return err + return fmt.Errorf("plugin [EnvKeys] method error: %w", err) } b.clearCurrentEnvConfig()