Skip to content

Commit

Permalink
optimize: error log output
Browse files Browse the repository at this point in the history
close #21
  • Loading branch information
aooohan committed Jan 17, 2024
1 parent c0a47c4 commit 0e426fd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .idea/copyright/apache.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions cmd/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions cmd/commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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))
}
3 changes: 1 addition & 2 deletions cmd/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
41 changes: 14 additions & 27 deletions internal/sdk/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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: <category>/<plugin-name>")
return fmt.Errorf("invalid plugin name")
return fmt.Errorf("invalid plugin name, format: <category>/<plugin-name>")
}
category := args[0]
name := args[1]
Expand All @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 8 additions & 10 deletions internal/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 0e426fd

Please sign in to comment.