Skip to content

Commit

Permalink
Merge pull request #106 from kcmvp/plugin-installation
Browse files Browse the repository at this point in the history
#105: add progress bar for plugin installation
  • Loading branch information
kcmvp authored May 18, 2024
2 parents b5688b3 + 70b5ccc commit 18a1ea4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
15 changes: 10 additions & 5 deletions cmd/gbc/artifact/multiple_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"github.com/creack/pty"
)

func StreamCmdOutput(cmd *exec.Cmd, task string) error {
type consoleFormatter func(msg string) string

func StreamCmdOutput(cmd *exec.Cmd, task string, formatter consoleFormatter) error {
// Start the command with a pty
var scanner *bufio.Scanner
if ptmx, err := pty.Start(cmd); err == nil {
Expand All @@ -27,9 +29,9 @@ func StreamCmdOutput(cmd *exec.Cmd, task string) error {
} else {
return err
}
color.HiCyan("start %s ......\n", task)
color.Green("start %s ......\n", task)
// Create a file to save the output
log, err := os.Create(filepath.Join(CurProject().Target(), fmt.Sprintf("%s.log", task)))
log, err := os.Create(filepath.Join(CurProject().Target(), fmt.Sprintf("%s.log", strings.ReplaceAll(task, " ", "_"))))
if err != nil {
return fmt.Errorf(color.RedString("Error creating file:", err))
}
Expand All @@ -43,6 +45,9 @@ func StreamCmdOutput(cmd *exec.Cmd, task string) error {
go func() {
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if formatter != nil {
line = strings.TrimSpace(formatter(line))
}
if len(line) > 1 {
ch <- line
}
Expand All @@ -63,7 +68,7 @@ func StreamCmdOutput(cmd *exec.Cmd, task string) error {
_, err = log.WriteString(lineWithoutColor + "\n")
line = lo.IfF(overwrite, func() string {
overwrite = false
return fmt.Sprintf("\r%-20s", line)
return fmt.Sprintf("\r%-15s", line)
}).Else(line)
fmt.Println(line)
if err != nil {
Expand All @@ -78,7 +83,7 @@ func StreamCmdOutput(cmd *exec.Cmd, task string) error {
}
}
_ = progress.Finish()
color.HiCyan("\rfinish %s ......\n", task)
color.Green("\rfinished %s ......\n", task)
ticker.Stop()
return cmd.Wait()
}
16 changes: 9 additions & 7 deletions cmd/gbc/artifact/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package artifact
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/samber/lo"
"io/fs"
"os"
Expand Down Expand Up @@ -123,16 +121,21 @@ func (plugin Plugin) install() (string, error) {
}
tempGoPath := temporaryGoPath()
defer os.RemoveAll(tempGoPath)
fmt.Printf("Installing %s@%s to %s ...... \n", plugin.Url, plugin.Version(), gopath)
cmd := exec.Command("go", "install", fmt.Sprintf("%s@%s", plugin.Url, plugin.Version())) //nolint:gosec
cmd.Env = lo.Map(os.Environ(), func(pair string, _ int) string {
if strings.HasPrefix(pair, "GOPATH=") {
return fmt.Sprintf("%s=%s", "GOPATH", tempGoPath)
}
return pair
})
if data, err := cmd.CombinedOutput(); err != nil {
return tempGoPath, errors.New(color.RedString(string(data)))
task := fmt.Sprintf("%s installation", plugin.Name())
if err := StreamCmdOutput(cmd, task, func(msg string) string {
return ""
}); err != nil {
return tempGoPath, err
}
if cmd.ProcessState.ExitCode() != 0 {
return tempGoPath, fmt.Errorf("faild %d", cmd.ProcessState.ExitCode())
}
return tempGoPath, filepath.WalkDir(tempGoPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -143,7 +146,6 @@ func (plugin Plugin) install() (string, error) {
if err != nil {
return err
}
fmt.Printf("%s is installed successfully \n", plugin.Url)
} else {
// change the permission for deletion
os.Chmod(path, 0o766) //nolint
Expand All @@ -158,7 +160,7 @@ func (plugin Plugin) Execute() error {
}
// always use absolute path
pCmd := exec.Command(filepath.Join(GoPath(), plugin.Binary()), strings.Split(plugin.Args, " ")...) //nolint #gosec
if err := StreamCmdOutput(pCmd, plugin.taskName()); err != nil {
if err := StreamCmdOutput(pCmd, plugin.taskName(), nil); err != nil {
return err
}
if pCmd.ProcessState.ExitCode() != 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gbc/artifact/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func NewProgress() *progressbar.ProgressBar {
spinner := []string{"←", "↑", "→", "↓"}
return progressbar.NewOptions(-1,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetWidth(10),
progressbar.OptionSetWidth(5),
progressbar.OptionSpinnerCustom(lo.Map(spinner, func(item string, idx int) string {
return fmt.Sprintf("[yellow]%s", item)
})),
Expand Down
5 changes: 2 additions & 3 deletions cmd/gbc/command/build_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func cleanAction(_ *cobra.Command, _ ...string) error {
func testAction(_ *cobra.Command, _ ...string) error {
coverProfile := fmt.Sprintf("-coverprofile=%s/cover.out", artifact.CurProject().Target())
testCmd := exec.Command("go", []string{"test", "-v", coverProfile, "./..."}...) //nolint
return artifact.StreamCmdOutput(testCmd, "test")
return artifact.StreamCmdOutput(testCmd, "test", nil)
}

func coverReport(_ *cobra.Command, _ ...string) error {
Expand All @@ -143,7 +143,6 @@ func coverReport(_ *cobra.Command, _ ...string) error {
}

func nolintReport(_ *cobra.Command, _ ...string) error {
color.HiCyan("Analysis nolint report ...")
_ = os.Chdir(artifact.CurProject().Root())
reg := regexp.MustCompile(`//\s*nolint`)
data, _ := exec.Command("go", "list", "-f", `{{.Dir}}:{{join .GoFiles " "}}`, `./...`).CombinedOutput()
Expand Down Expand Up @@ -180,7 +179,7 @@ func nolintReport(_ *cobra.Command, _ ...string) error {
}
maxLength += 5
if fOverAll+lOverAll > 0 {
color.Yellow("file level lint ignores: %d, line level lint ignores: %d", fOverAll, lOverAll)
color.Yellow("[Lint Report]:file level ignores: %d, line level ignores: %d", fOverAll, lOverAll)
slices.SortFunc(ignoreList, func(a, b lo.Tuple3[string, int, int]) int {
return b.C - a.C
})
Expand Down

0 comments on commit 18a1ea4

Please sign in to comment.