From fcebf7c176d1faa6e75ad080f1ab3933f8b8747a Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Tue, 21 Jan 2025 00:32:24 +0200 Subject: [PATCH] feat: pipx installer --- README.md | 4 ++ appconfig/installer_data.go | 1 + docs/installer-types.md | 3 ++ installer/installer.go | 2 + installer/pipx_installer.go | 74 +++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 installer/pipx_installer.go diff --git a/README.md b/README.md index 4682350..b381f8d 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,10 @@ For a full list with all the supported options, see [the docs](./docs/installer- - Installs packages using apt install. +- **`pipx`** + + - Installs packages using pipx. + --- ## 📂 Example Workflow diff --git a/appconfig/installer_data.go b/appconfig/installer_data.go index 32c27e4..e3ac156 100644 --- a/appconfig/installer_data.go +++ b/appconfig/installer_data.go @@ -40,6 +40,7 @@ const ( InstallerTypeNpm InstallerType = "npm" InstallerTypePnpm InstallerType = "pnpm" InstallerTypeYarn InstallerType = "yarn" + InstallerTypePipx InstallerType = "pipx" InstallerTypeManifest InstallerType = "manifest" ) diff --git a/docs/installer-types.md b/docs/installer-types.md index a481818..09a4df4 100644 --- a/docs/installer-types.md +++ b/docs/installer-types.md @@ -177,6 +177,9 @@ These fields are shared by all installer types. Some fields may vary in behavior - **`apt`** - **Description**: Installs packages using apt install. +- **`pipx`** + - **Description**: Installs packages using pipx. + ## Installer Examples All of these examples should be usable, but don't count on them being maintained. Why not look at diff --git a/installer/installer.go b/installer/installer.go index 57ff60f..bb73614 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -30,6 +30,8 @@ func GetInstaller(config *appconfig.AppConfig, data *appconfig.InstallerData) (e return nil, NewNpmInstaller(config, data) case appconfig.InstallerTypeApt: return nil, NewAptInstaller(config, data) + case appconfig.InstallerTypePipx: + return nil, NewPipxInstaller(config, data) case appconfig.InstallerTypeGit: return nil, NewGitInstaller(config, data) case appconfig.InstallerTypeManifest: diff --git a/installer/pipx_installer.go b/installer/pipx_installer.go new file mode 100644 index 0000000..a4622b4 --- /dev/null +++ b/installer/pipx_installer.go @@ -0,0 +1,74 @@ +package installer + +import ( + "github.com/chenasraf/sofmani/appconfig" + "github.com/chenasraf/sofmani/utils" +) + +type PipxInstaller struct { + Config *appconfig.AppConfig + Info *appconfig.InstallerData +} + +type PipxOpts struct { + // +} + +// Install implements IInstaller. +func (i *PipxInstaller) Install() error { + name := *i.Info.Name + return utils.RunCmdPassThrough(i.Info.Environ(), "pipx", "install", name) +} + +// Update implements IInstaller. +func (i *PipxInstaller) Update() error { + return utils.RunCmdPassThrough(i.Info.Environ(), "pipx", "upgrade", *i.Info.Name) +} + +// CheckNeedsUpdate implements IInstaller. +func (i *PipxInstaller) CheckNeedsUpdate() (error, bool) { + if i.GetData().CheckHasUpdate != nil { + return utils.RunCmdGetSuccess(i.Info.Environ(), utils.GetOSShell(i.GetData().EnvShell), utils.GetOSShellArgs(*i.GetData().CheckHasUpdate)...) + } + err, success := utils.RunCmdGetSuccess(i.Info.Environ(), "pipx", "upgrade", "--pip-args=--dry-run", *i.Info.Name) + if err != nil { + return err, false + } + return nil, !success +} + +// CheckIsInstalled implements IInstaller. +func (i *PipxInstaller) CheckIsInstalled() (error, bool) { + return utils.RunCmdGetSuccess(i.Info.Environ(), utils.GetShellWhich(), i.GetBinName()) +} + +// GetData implements IInstaller. +func (i *PipxInstaller) GetData() *appconfig.InstallerData { + return i.Info +} + +func (i *PipxInstaller) GetOpts() *PipxOpts { + opts := &PipxOpts{} + info := i.Info + if info.Opts != nil { + // + } + return opts +} + +func (i *PipxInstaller) GetBinName() string { + info := i.GetData() + if info.BinName != nil && len(*info.BinName) > 0 { + return *info.BinName + } + return *info.Name +} + +func NewPipxInstaller(cfg *appconfig.AppConfig, installer *appconfig.InstallerData) *PipxInstaller { + i := &PipxInstaller{ + Config: cfg, + Info: installer, + } + + return i +}