feat: add custom installed check, add pre/post command

This commit is contained in:
2024-12-23 23:37:15 +02:00
parent adf930f308
commit 3b5f720441
6 changed files with 174 additions and 6 deletions

View File

@@ -22,7 +22,16 @@ type BrewOpts struct {
// Install implements IInstaller.
func (i *BrewInstaller) Install() error {
return utils.RunCmdPassThrough("brew", "install", i.Info.Name)
chain := [][]string{
{"brew", "install", i.Info.Name},
}
if i.GetOpts().PreCommand != nil {
chain = append([][]string{{"sh", "-c", *i.GetOpts().PreCommand}}, chain...)
}
if i.GetOpts().PostCommand != nil {
chain = append(chain, []string{"sh", "-c", *i.GetOpts().PostCommand})
}
return utils.RunCmdPassThroughChained(chain)
}
// Update implements IInstaller.

View File

@@ -21,6 +21,13 @@ type GroupOpts struct {
// Install implements IInstaller.
func (i *GroupInstaller) Install() error {
logger.Debug("Installing group %s", i.Info.Name)
if i.GetOpts().PreCommand != nil {
logger.Debug("Running pre-command for group %s", i.Info.Name)
err := utils.RunCmdPassThrough("sh", "-c", *i.GetOpts().PreCommand)
if err != nil {
return err
}
}
for _, step := range *i.Info.Steps {
err, installer := GetInstaller(i.Config, &step)
if err != nil {
@@ -32,6 +39,13 @@ func (i *GroupInstaller) Install() error {
RunInstaller(i.Config, installer)
}
}
if i.GetOpts().PostCommand != nil {
logger.Debug("Running post-command for group %s", i.Info.Name)
err := utils.RunCmdPassThrough("sh", "-c", *i.GetOpts().PostCommand)
if err != nil {
return err
}
}
return nil
}

View File

@@ -17,6 +17,7 @@ type ShellOpts struct {
Command *string
BinName *string
CheckHasUpdate *string
CheckInstalled *string
}
// Install implements IInstaller.
@@ -47,6 +48,9 @@ func (i *ShellInstaller) CheckNeedsUpdate() (error, bool) {
// CheckIsInstalled implements IInstaller.
func (i *ShellInstaller) CheckIsInstalled() (error, bool) {
if i.GetOpts().CheckInstalled != nil {
return utils.RunCmdGetSuccess("sh", "-c", *i.GetOpts().CheckInstalled)
}
return utils.RunCmdGetSuccess("which", i.GetBinName())
}
@@ -68,6 +72,9 @@ func (i *ShellInstaller) GetOpts() *ShellOpts {
if command, ok := (*info.Opts)["check_has_update"].(string); ok {
opts.CheckHasUpdate = &command
}
if command, ok := (*info.Opts)["check_installed"].(string); ok {
opts.CheckInstalled = &command
}
}
return opts
}

View File

@@ -17,7 +17,7 @@ func main() {
logFile := "sofmani.log"
logger.InitLogger(logFile, cfg)
logger.Info("Installing...")
logger.Info("Checking all installers...")
for _, i := range cfg.Install {
err, installerInstance := installer.GetInstaller(cfg, &i)
if err != nil {
@@ -34,4 +34,5 @@ func main() {
}
}
}
logger.Info("Complete")
}

View File

@@ -1,4 +1,4 @@
debug: true
# debug: true
install:
- name: treelike
type: brew
@@ -70,5 +70,126 @@ install:
type: shell
opts:
command: pipx install yq
- name: direnv
type: group
steps:
- name: direnv
type: brew
platforms:
only: ['macos']
- name: direnv
type: shell
platforms:
only: ['linux']
opts:
command: export bin_path=/usr/local/bin curl -sfL https://direnv.net/install.sh | bash
- name: dotenvx
type: group
steps:
- name: dotenvx
type: brew
platforms:
only: ['macos']
opts:
tap: dotenvx/brew
- name: dotenvx
type: shell
platforms:
only: ['linux']
opts:
command: curl -sfS https://dotenvx.sh | sh
- name: ollama
type: brew
platforms:
only: ['macos']
opts:
post_command: brew services start ollama
- name: gi_gen
type: group
steps:
- name: gi_gen
type: brew
platforms:
only: ['macos']
opts:
tap: chenasraf/tap
- name: gi_gen
type: shell
platforms:
only: ['linux']
opts:
command: |
gi_ver=$(get-gh-latest-tag "chenasraf/gi_gen")
mkdir -p "$DOTBIN_META"
arch=$(archmatch -mA "macos-arm" -mI "macos-intel" -l "linux-amd")
curl -L https://github.com/chenasraf/gi_gen/releases/download/$gi_ver/gi_gen-$gi_ver-$arch -o $DOTBIN/gi_gen
- name: treelike
type: group
steps:
- name: treelike
type: brew
platforms:
only: ['macos']
opts:
tap: chenasraf/tap
- name: treelike
type: shell
platforms:
only: ['linux']
opts:
command: |
gi_ver=$(get-gh-latest-tag "chenasraf/treelike")
mkdir -p "$DOTBIN_META"
arch=$(archmatch -mA "macos-arm" -mI "macos-intel" -l "linux-amd")
curl -L https://github.com/chenasraf/treelike/releases/download/$treelike_ver/treelike-$arch.tar.gz -o $DOTBIN/treelike.tar.gz
- name: pnpm-pkgs
type: group
steps:
- name: pnpm
type: shell
opts:
command: npm i -g pnpm
- name: tsc
type: shell
opts:
command: pnpm i -g typescript
- name: tldr
type: shell
opts:
command: pnpm i -g tldr
- name: simple-scaffold
type: shell
opts:
command: pnpm i -g simple-scaffold
- name: firebase
type: shell
opts:
command: pnpm i -g firebase-tools
- name: prettier
type: shell
opts:
command: pnpm i -g prettier
- name: http-server
type: shell
opts:
command: pnpm i -g http-server
- name: licenseg
type: shell
opts:
command: pnpm i -g licenseg
- name: vscode-json-language-server
type: shell
opts:
command: pnpm i -g vscode-langservers-extracted
- name: home
type: shell
opts:
bin_name: tx
command: |
cd $DOTFILES/utils
pnpm i && pnpm build && pnpm ginst
- name: zplug
type: shell
opts:
check_installed: test -d ~/.zplug
command: curl -sL --proto-redir -all,https https://raw.githubusercontent.com/zplug/installer/master/installer.zsh | zsh

View File

@@ -4,16 +4,32 @@ import (
"io"
"os"
"os/exec"
"github.com/chenasraf/sofmani/logger"
)
func RunCmdPassThrough(bin string, args ...string) error {
logger.Debug("Running command: %s %v", bin, args)
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()
err := cmd.Wait()
if err != nil {
return err
}
return nil
}
func RunCmdPassThroughChained(commands [][]string) error {
for _, c := range commands {
err := RunCmdPassThrough(c[0], c[1:]...)
if err != nil {
return err
}
}
return nil
}