Skip to content

Commit

Permalink
refactor: better install details
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Feb 21, 2024
1 parent b608960 commit 90fa865
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
54 changes: 21 additions & 33 deletions cmd/vale/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ func getLocation(browser string) (map[string]string, error) {
}, nil
}

func writeNativeConfig() error {
func writeNativeConfig() (string, error) {
cfgFile, err := getNativeConfig()
if err != nil {
return err
return "", err
}

exe, err := exec.LookPath("vale")
if err != nil {
return err
return "", err
}

cfg := map[string]string{
Expand All @@ -136,10 +136,10 @@ func writeNativeConfig() error {

jsonCfg, err := json.Marshal(cfg)
if err != nil {
return err
return "", err
}

return os.WriteFile(cfgFile, jsonCfg, os.ModePerm)
return cfgFile, os.WriteFile(cfgFile, jsonCfg, os.ModePerm)
}

func installNativeHostUnix(manifestData []byte, manifestFile string) error {
Expand Down Expand Up @@ -229,24 +229,21 @@ func installNativeHost(args []string, _ *core.CLIFlags) error { //nolint:funlen
p, _ := pterm.DefaultProgressbar.WithTotal(len(steps)).WithTitle("Installing host").Start()

p.UpdateTitle(steps[0])
err := writeNativeConfig()
cfgFile, err := writeNativeConfig()
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}
pterm.Success.Println(steps[0])
pterm.Success.Println(fmt.Sprintf("wrote '%s'", cfgFile))
p.Increment()

locations, err := getLocation(browser)
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}

hostURL, err := hostDownloadURL()
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}
exeName := getExecName("vale-native")

Expand All @@ -256,19 +253,17 @@ func installNativeHost(args []string, _ *core.CLIFlags) error { //nolint:funlen
if core.FileExists(fp) {
err = os.Remove(fp)
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}
}
}

p.UpdateTitle(steps[1])
err = fetch(hostURL, locations["appDir"])
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}
pterm.Success.Println(steps[1])
pterm.Success.Println(fmt.Sprintf("fetched '%s'", hostURL))
p.Increment()

manifestData := manifest{
Expand All @@ -282,8 +277,7 @@ func installNativeHost(args []string, _ *core.CLIFlags) error { //nolint:funlen

extension, found := extensionByBrowser[browser]
if !found {
p.Stop()
return core.NewE100("host-install", errMissingExt)
return progressError("host-install", errMissingExt, p)
}
allowed := []string{extension}

Expand All @@ -300,17 +294,15 @@ func installNativeHost(args []string, _ *core.CLIFlags) error { //nolint:funlen

manifestJSON, err := json.MarshalIndent(manifestData, "", " ")
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}

p.UpdateTitle(steps[2])
err = installHost(manifestJSON, manifestFile, browser)
if err != nil {
p.Stop()
return core.NewE100("host-install", err)
return progressError("host-install", err, p)
}
pterm.Success.Println(steps[2])
pterm.Success.Println(fmt.Sprintf("installed '%s'", manifestFile))
p.Increment()

return nil
Expand All @@ -331,8 +323,7 @@ func uninstallNativeHost(args []string, _ *core.CLIFlags) error {

locations, err := getLocation(browser)
if err != nil {
p.Stop()
return core.NewE100("host-uninstall", err)
return progressError("host-uninstall", err, p)
}
p.UpdateTitle(steps[0])

Expand All @@ -342,8 +333,7 @@ func uninstallNativeHost(args []string, _ *core.CLIFlags) error {
if core.FileExists(fp) {
err = os.Remove(filepath.Join(locations["appDir"], file))
if err != nil {
p.Stop()
return core.NewE100("host-uninstall", err)
progressError("host-uninstall", err, p)

Check failure on line 336 in cmd/vale/native.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
}
}
}
Expand All @@ -356,15 +346,13 @@ func uninstallNativeHost(args []string, _ *core.CLIFlags) error {
if core.FileExists(manifestFile) {
err = os.Remove(manifestFile)
if err != nil {
p.Stop()
return core.NewE100("host-uninstall", err)
return progressError("host-uninstall", err, p)
}
}

err = unsetManifestRegistry(browser)
if err != nil {
p.Stop()
return core.NewE100("host-uninstall", err)
return progressError("host-uninstall", err, p)
}

pterm.Success.Println(steps[1])
Expand Down
9 changes: 9 additions & 0 deletions cmd/vale/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"path/filepath"
"runtime"
"strings"

"github.com/pterm/pterm"

"github.com/errata-ai/vale/v3/internal/core"
)

// Response is returned after an action.
Expand All @@ -17,6 +21,11 @@ type Response struct {
Success bool
}

func progressError(context string, err error, p *pterm.ProgressbarPrinter) error {
_, _ = p.Stop()
return core.NewE100(context, err)
}

func pluralize(s string, n int) string {
if n != 1 {
return s + "s"
Expand Down

0 comments on commit 90fa865

Please sign in to comment.