From adf930f3086fd0410ab874be021dcafc2d7cda0e Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Mon, 23 Dec 2024 22:49:47 +0200 Subject: [PATCH] refactor: add cmd utils --- installer/brew_installer.go | 44 ++++++++++++------------------------ installer/group_installer.go | 25 +++++++++----------- installer/shell_installer.go | 27 ++++------------------ sofmani.yml | 6 ++--- utils/command.go | 33 +++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 70 deletions(-) create mode 100644 utils/command.go diff --git a/installer/brew_installer.go b/installer/brew_installer.go index 9eb74d8..8de2a34 100644 --- a/installer/brew_installer.go +++ b/installer/brew_installer.go @@ -2,12 +2,10 @@ package installer import ( "encoding/json" - "io" - "os" - "os/exec" "slices" "github.com/chenasraf/sofmani/appconfig" + "github.com/chenasraf/sofmani/utils" ) type BrewInstaller struct { @@ -16,38 +14,25 @@ type BrewInstaller struct { } type BrewOpts struct { - Tap *string - BinName *string + Tap *string + BinName *string + PreCommand *string + PostCommand *string } // Install implements IInstaller. func (i *BrewInstaller) Install() error { - cmd := exec.Command("brew", "install", i.Info.Name) - stdout, _ := cmd.StdoutPipe() - stderr, _ := cmd.StderrPipe() - cmd.Start() - go io.Copy(os.Stdout, stdout) - go io.Copy(os.Stderr, stderr) - cmd.Wait() - return nil + return utils.RunCmdPassThrough("brew", "install", i.Info.Name) } // Update implements IInstaller. func (i *BrewInstaller) Update() error { - cmd := exec.Command("brew", "upgrade", i.Info.Name) - stdout, _ := cmd.StdoutPipe() - stderr, _ := cmd.StderrPipe() - cmd.Start() - go io.Copy(os.Stdout, stdout) - go io.Copy(os.Stderr, stderr) - cmd.Wait() - return nil + return utils.RunCmdPassThrough("brew", "upgrade", i.Info.Name) } // CheckNeedsUpdate implements IInstaller. func (i *BrewInstaller) CheckNeedsUpdate() (error, bool) { - cmd := exec.Command("brew", "outdated", "--json", i.Info.Name) - out, err := cmd.Output() + out, err := utils.RunCmdGetOutput("brew", "outdated", "--json", i.Info.Name) if err != nil { return err, false } @@ -69,12 +54,7 @@ func (i *BrewInstaller) CheckNeedsUpdate() (error, bool) { // CheckIsInstalled implements IInstaller. func (i *BrewInstaller) CheckIsInstalled() (error, bool) { - cmd := exec.Command("which", i.GetBinName()) - err := cmd.Run() - if err != nil { - return nil, false - } - return nil, true + return utils.RunCmdGetSuccess("which", i.GetBinName()) } // GetInfo implements IInstaller. @@ -92,6 +72,12 @@ func (i *BrewInstaller) GetOpts() *BrewOpts { if binName, ok := (*info.Opts)["bin_name"].(string); ok { opts.BinName = &binName } + if command, ok := (*info.Opts)["pre_command"].(string); ok { + opts.PreCommand = &command + } + if command, ok := (*info.Opts)["post_command"].(string); ok { + opts.PostCommand = &command + } } return opts } diff --git a/installer/group_installer.go b/installer/group_installer.go index 90f76ad..3e68550 100644 --- a/installer/group_installer.go +++ b/installer/group_installer.go @@ -1,10 +1,9 @@ package installer import ( - "os/exec" - "github.com/chenasraf/sofmani/appconfig" "github.com/chenasraf/sofmani/logger" + "github.com/chenasraf/sofmani/utils" ) type GroupInstaller struct { @@ -15,6 +14,8 @@ type GroupInstaller struct { type GroupOpts struct { BinName *string CheckHasUpdate *string + PreCommand *string + PostCommand *string } // Install implements IInstaller. @@ -42,24 +43,14 @@ func (i *GroupInstaller) Update() error { // CheckNeedsUpdate implements IInstaller. func (i *GroupInstaller) CheckNeedsUpdate() (error, bool) { if i.GetOpts().CheckHasUpdate != nil { - cmd := exec.Command("sh", "-c", *i.GetOpts().CheckHasUpdate) - err := cmd.Run() - if err != nil { - return err, true - } - return nil, false + return utils.RunCmdGetSuccess("sh", "-c", *i.GetOpts().CheckHasUpdate) } return nil, false } // CheckIsInstalled implements IInstaller. func (i *GroupInstaller) CheckIsInstalled() (error, bool) { - cmd := exec.Command("which", i.GetBinName()) - err := cmd.Run() - if err != nil { - return nil, false - } - return nil, true + return utils.RunCmdGetSuccess("which", i.GetBinName()) } // GetInfo implements IInstaller. @@ -77,6 +68,12 @@ func (i *GroupInstaller) GetOpts() *GroupOpts { if command, ok := (*info.Opts)["check_has_update"].(string); ok { opts.CheckHasUpdate = &command } + if command, ok := (*info.Opts)["pre_command"].(string); ok { + opts.PreCommand = &command + } + if command, ok := (*info.Opts)["post_command"].(string); ok { + opts.PostCommand = &command + } } return opts } diff --git a/installer/shell_installer.go b/installer/shell_installer.go index cd2b613..f8014db 100644 --- a/installer/shell_installer.go +++ b/installer/shell_installer.go @@ -2,11 +2,10 @@ package installer import ( "fmt" - "io" "os" - "os/exec" "github.com/chenasraf/sofmani/appconfig" + "github.com/chenasraf/sofmani/utils" ) type ShellInstaller struct { @@ -30,14 +29,7 @@ func (i *ShellInstaller) Install() error { return err } - cmd := exec.Command("sh", "-c", tmpfile) - stdout, _ := cmd.StdoutPipe() - stderr, _ := cmd.StderrPipe() - cmd.Start() - go io.Copy(os.Stdout, stdout) - go io.Copy(os.Stderr, stderr) - cmd.Wait() - return nil + return utils.RunCmdPassThrough("sh", "-c", tmpfile) } // Update implements IInstaller. @@ -48,25 +40,14 @@ func (i *ShellInstaller) Update() error { // CheckNeedsUpdate implements IInstaller. func (i *ShellInstaller) CheckNeedsUpdate() (error, bool) { if i.GetOpts().CheckHasUpdate != nil { - cmd := exec.Command("sh", "-c", *i.GetOpts().CheckHasUpdate) - err := cmd.Run() - if err != nil { - return err, true - } - return nil, false + return utils.RunCmdGetSuccess("sh", "-c", *i.GetOpts().CheckHasUpdate) } return nil, false } // CheckIsInstalled implements IInstaller. func (i *ShellInstaller) CheckIsInstalled() (error, bool) { - // cmd := exec.Command("brew", "list", i.Info.Name) - cmd := exec.Command("which", i.GetBinName()) - err := cmd.Run() - if err != nil { - return nil, false - } - return nil, true + return utils.RunCmdGetSuccess("which", i.GetBinName()) } // GetInfo implements IInstaller. diff --git a/sofmani.yml b/sofmani.yml index f8be08e..b35c514 100644 --- a/sofmani.yml +++ b/sofmani.yml @@ -48,14 +48,13 @@ install: command: 'curl https://pyenv.run | bash' - name: pipx type: group + opts: + post_command: sudo pipx ensurepath --global steps: - name: pipx type: brew platforms: only: ['macos'] - opts: - post_command: | - sudo pipx ensurepath --global - name: pipx type: apt platforms: @@ -63,7 +62,6 @@ install: opts: post_command: | sudo pipx ensurepath --global - - name: jq type: brew platforms: diff --git a/utils/command.go b/utils/command.go new file mode 100644 index 0000000..3c6f555 --- /dev/null +++ b/utils/command.go @@ -0,0 +1,33 @@ +package utils + +import ( + "io" + "os" + "os/exec" +) + +func RunCmdPassThrough(bin string, args ...string) error { + cmd := exec.Command(bin, args...) + stdout, _ := cmd.StdoutPipe() + stderr, _ := cmd.StderrPipe() + cmd.Start() + go io.Copy(os.Stdout, stdout) + go io.Copy(os.Stderr, stderr) + cmd.Wait() + return nil +} + +func RunCmdGetSuccess(bin string, args ...string) (error, bool) { + cmd := exec.Command(bin, args...) + err := cmd.Run() + if err != nil { + return nil, false + } + return nil, true +} + +func RunCmdGetOutput(bin string, args ...string) ([]byte, error) { + cmd := exec.Command(bin, args...) + out, err := cmd.Output() + return out, err +}