feat: group installer

This commit is contained in:
2024-12-23 01:18:02 +02:00
parent ad45c24e56
commit d39c36ec55
6 changed files with 125 additions and 25 deletions

View File

@@ -14,23 +14,21 @@ type AppConfig struct {
}
type Installer struct {
Name string `json:"name" yaml:"name"`
BinName *string `json:"bin_name" yaml:"bin_name"`
Type InstallerType `json:"type" yaml:"type"`
Platforms *Platforms `json:"platforms" yaml:"platforms"`
Url *string `json:"url" yaml:"url"`
Command *string `json:"command" yaml:"command"`
Steps *[]Installer `json:"steps" yaml:"steps"`
Name string `json:"name" yaml:"name"`
Type InstallerType `json:"type" yaml:"type"`
Platforms *Platforms `json:"platforms" yaml:"platforms"`
Steps *[]Installer `json:"steps" yaml:"steps"`
Opts *map[string]any `json:"opts" yaml:"opts"`
}
type InstallerType string
const (
Brew InstallerType = "brew"
Apt InstallerType = "apt"
Git InstallerType = "git"
Cmd InstallerType = "cmd"
Group InstallerType = "group"
InstallerTypeCmd InstallerType = "cmd"
InstallerTypeBrew InstallerType = "brew"
InstallerTypeApt InstallerType = "apt"
InstallerTypeGit InstallerType = "git"
InstallerTypeGroup InstallerType = "group"
)
type Platforms struct {
@@ -41,9 +39,9 @@ type Platforms struct {
type Platform string
const (
Macos Platform = "macos"
Linux Platform = "linux"
Windows Platform = "windows"
PlatformMacos Platform = "macos"
PlatformLinux Platform = "linux"
PlatformWindows Platform = "windows"
)
func ParseConfigFile(file string) (*AppConfig, error) {

View File

@@ -15,6 +15,11 @@ type BrewInstaller struct {
Info *appconfig.Installer
}
type BrewOpts struct {
Tap *string
BinName *string
}
// Install implements IInstaller.
func (i *BrewInstaller) Install() error {
cmd := exec.Command("brew", "install", i.Info.Name)
@@ -78,9 +83,24 @@ func (i *BrewInstaller) GetInfo() *appconfig.Installer {
return i.Info
}
func (i *BrewInstaller) GetOpts() *BrewOpts {
opts := &BrewOpts{}
info := i.Info
if info.Opts != nil {
if tap, ok := (*info.Opts)["tap"].(string); ok {
opts.Tap = &tap
}
if binName, ok := (*info.Opts)["bin_name"].(string); ok {
opts.BinName = &binName
}
}
return opts
}
func (i *BrewInstaller) GetBinName() string {
if i.Info.BinName != nil && len(*i.Info.BinName) > 0 {
return *i.Info.BinName
opts := i.GetOpts()
if opts.BinName != nil && len(*opts.BinName) > 0 {
return *opts.BinName
}
return i.Info.Name
}

View File

@@ -0,0 +1,52 @@
package installer
import (
"fmt"
"github.com/chenasraf/sofmani/appconfig"
)
type GroupInstaller struct {
Config *appconfig.AppConfig
Info *appconfig.Installer
}
// Install implements IInstaller.
func (i *GroupInstaller) Install() error {
fmt.Printf("Installing group %s\n", i.Info.Name)
for _, step := range *i.Info.Steps {
err, installer := GetInstaller(i.Config, &step)
if err != nil {
return err
}
RunInstaller(i.Config, installer)
}
return nil
}
// Update implements IInstaller.
func (i *GroupInstaller) Update() error {
return nil
}
// CheckNeedsUpdate implements IInstaller.
func (i *GroupInstaller) CheckNeedsUpdate() (error, bool) {
return nil, false
}
// CheckIsInstalled implements IInstaller.
func (i *GroupInstaller) CheckIsInstalled() (error, bool) {
return nil, false
}
// GetInfo implements IInstaller.
func (i *GroupInstaller) GetInfo() *appconfig.Installer {
return i.Info
}
func NewGroupInstaller(cfg *appconfig.AppConfig, installer *appconfig.Installer) *GroupInstaller {
return &GroupInstaller{
Config: cfg,
Info: installer,
}
}

View File

@@ -16,13 +16,16 @@ type IInstaller interface {
func GetInstaller(config *appconfig.AppConfig, installer *appconfig.Installer) (error, IInstaller) {
switch installer.Type {
case appconfig.Brew:
case appconfig.InstallerTypeBrew:
return nil, NewBrewInstaller(config, installer)
case appconfig.InstallerTypeGroup:
return nil, NewGroupInstaller(config, installer)
}
return fmt.Errorf("Installer type %s is not supported", installer.Type), nil
}
func RunInstaller(config *appconfig.AppConfig, installer IInstaller) error {
fmt.Printf("Checking if %s is installed\n", installer.GetInfo().Name)
err, installed := installer.CheckIsInstalled()
if err != nil {
return err
@@ -43,6 +46,9 @@ func RunInstaller(config *appconfig.AppConfig, installer IInstaller) error {
return nil
}
}
installer.Install()
err = installer.Install()
if err != nil {
return err
}
return nil
}

View File

@@ -21,6 +21,12 @@ func main() {
fmt.Println(err)
return
}
installer.RunInstaller(cfg, installerInstance)
err = installer.RunInstaller(cfg, installerInstance)
if err != nil {
fmt.Println(err)
panic(err)
return
}
}
}

View File

@@ -1,8 +1,26 @@
debug: true
# check_updates: true
install:
- name: "treelike"
type: "brew"
url: "chenasraf/tap/treelike"
- name: treelike
type: brew
platforms:
only: new Listing { "macos" }
only: ['macos']
opts:
tap: chenasraf/tap/treelike
- name: lazygit
type: group
steps:
- name: lazygit
type: brew
platforms:
only: ['macos']
opts:
tap: jesseduffield/lazygit
- name: lazygit
type: cmd
opts:
command: |
cd $(mktemp -d)
lazygit_version=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
curl -Lo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${lazygit_version}_Linux_x86_64.tar.gz"
tar xf lazygit.tar.gz lazygit
sudo install lazygit /usr/local/bin