mirror of
https://github.com/chenasraf/sofmani.git
synced 2026-05-17 17:28:04 +00:00
refactor: add cmd utils
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
33
utils/command.go
Normal file
33
utils/command.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user