feat: add autoPull in config

This commit is contained in:
Maxim Slipenko 2025-01-22 18:10:57 +03:00
parent 30f95a4cbf
commit 309ecf784f
9 changed files with 115 additions and 80 deletions

@ -65,10 +65,12 @@ func BuildCmd() *cli.Command {
script = filepath.Join(config.GetPaths(ctx).RepoDir, c.String("package"), "alr.sh") script = filepath.Join(config.GetPaths(ctx).RepoDir, c.String("package"), "alr.sh")
} }
err := repos.Pull(ctx, config.Config(ctx).Repos) if config.GetInstance(ctx).AutoPull(ctx) {
if err != nil { err := repos.Pull(ctx, config.Config(ctx).Repos)
slog.Error(gotext.Get("Error pulling repositories"), "err", err) if err != nil {
os.Exit(1) slog.Error(gotext.Get("Error pulling repositories"), "err", err)
os.Exit(1)
}
} }
mgr := manager.Detect() mgr := manager.Detect()

15
info.go

@ -52,6 +52,11 @@ func InfoCmd() *cli.Command {
cfg := config.New() cfg := config.New()
db := db.New(cfg) db := db.New(cfg)
err := db.Init(ctx)
if err != nil {
slog.Error(gotext.Get("Error initialization database"), "err", err)
os.Exit(1)
}
rs := repos.New(cfg, db) rs := repos.New(cfg, db)
args := c.Args() args := c.Args()
@ -60,10 +65,12 @@ func InfoCmd() *cli.Command {
os.Exit(1) os.Exit(1)
} }
err := rs.Pull(ctx, cfg.Repos(ctx)) if cfg.AutoPull(ctx) {
if err != nil { err := rs.Pull(ctx, cfg.Repos(ctx))
slog.Error(gotext.Get("Error pulling repositories")) if err != nil {
os.Exit(1) slog.Error(gotext.Get("Error pulling repos"), "err", err)
os.Exit(1)
}
} }
found, _, err := rs.FindPkgs(ctx, args.Slice()) found, _, err := rs.FindPkgs(ctx, args.Slice())

@ -64,10 +64,12 @@ func InstallCmd() *cli.Command {
os.Exit(1) os.Exit(1)
} }
err := repos.Pull(ctx, config.Config(ctx).Repos) if config.GetInstance(ctx).AutoPull(ctx) {
if err != nil { err := repos.Pull(ctx, config.Config(ctx).Repos)
slog.Error(gotext.Get("Error pulling repositories"), "err", err) if err != nil {
os.Exit(1) slog.Error(gotext.Get("Error pulling repositories"), "err", err)
os.Exit(1)
}
} }
found, notFound, err := repos.FindPkgs(ctx, args.Slice()) found, notFound, err := repos.FindPkgs(ctx, args.Slice())

@ -45,6 +45,7 @@ var defaultConfig = &types.Config{
RootCmd: "sudo", RootCmd: "sudo",
PagerStyle: "native", PagerStyle: "native",
IgnorePkgUpdates: []string{}, IgnorePkgUpdates: []string{},
AutoPull: true,
Repos: []types.Repo{ Repos: []types.Repo{
{ {
Name: "default", Name: "default",
@ -162,3 +163,10 @@ func (c *ALRConfig) IgnorePkgUpdates(ctx context.Context) []string {
}) })
return c.cfg.IgnorePkgUpdates return c.cfg.IgnorePkgUpdates
} }
func (c *ALRConfig) AutoPull(ctx context.Context) bool {
c.cfgOnce.Do(func() {
c.Load(ctx)
})
return c.cfg.AutoPull
}

@ -26,23 +26,23 @@ msgid ""
"Build package from scratch even if there's an already built package available" "Build package from scratch even if there's an already built package available"
msgstr "" msgstr ""
#: build.go:70 #: build.go:71
msgid "Error pulling repositories" msgid "Error pulling repos"
msgstr "" msgstr ""
#: build.go:76 #: build.go:78
msgid "Unable to detect a supported package manager on the system" msgid "Unable to detect a supported package manager on the system"
msgstr "" msgstr ""
#: build.go:87 #: build.go:89
msgid "Error building package" msgid "Error building package"
msgstr "" msgstr ""
#: build.go:93 #: build.go:95
msgid "Error getting working directory" msgid "Error getting working directory"
msgstr "" msgstr ""
#: build.go:101 #: build.go:103
msgid "Error moving the package" msgid "Error moving the package"
msgstr "" msgstr ""
@ -66,10 +66,6 @@ msgstr ""
msgid "Unable to create new cache directory" msgid "Unable to create new cache directory"
msgstr "" msgstr ""
#: fix.go:62
msgid "Error pulling repos"
msgstr ""
#: fix.go:66 #: fix.go:66
msgid "Done" msgid "Done"
msgstr "" msgstr ""
@ -110,43 +106,51 @@ msgstr ""
msgid "Command info expected at least 1 argument, got %d" msgid "Command info expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: info.go:71 #: info.go:73
msgid "Error finding packages" msgid "Error finding packages"
msgstr "" msgstr ""
#: info.go:87 #: info.go:89
msgid "Error parsing os-release file" msgid "Error parsing os-release file"
msgstr "" msgstr ""
#: info.go:96 #: info.go:98
msgid "Error resolving overrides" msgid "Error resolving overrides"
msgstr "" msgstr ""
#: info.go:105 info.go:111 #: info.go:107 info.go:113
msgid "Error encoding script variables" msgid "Error encoding script variables"
msgstr "" msgstr ""
#: install.go:43
msgid "Install a new package"
msgstr ""
#: install.go:57 #: install.go:57
msgid "Command install expected at least 1 argument, got %d" msgid "Command install expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: install.go:90 #: install.go:70
msgid "Error pulling repositories"
msgstr ""
#: install.go:92
msgid "Error getting packages" msgid "Error getting packages"
msgstr "" msgstr ""
#: install.go:99 #: install.go:101
msgid "Error iterating over packages" msgid "Error iterating over packages"
msgstr "" msgstr ""
#: install.go:112 #: install.go:114
msgid "Remove an installed package" msgid "Remove an installed package"
msgstr "" msgstr ""
#: install.go:117 #: install.go:119
msgid "Command remove expected at least 1 argument, got %d" msgid "Command remove expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: install.go:129 #: install.go:131
msgid "Error removing packages" msgid "Error removing packages"
msgstr "" msgstr ""
@ -174,39 +178,39 @@ msgstr ""
msgid "Choose which optional package(s) to install" msgid "Choose which optional package(s) to install"
msgstr "" msgstr ""
#: internal/config/config.go:63 #: internal/config/config.go:64
msgid "Error opening config file, using defaults" msgid "Error opening config file, using defaults"
msgstr "" msgstr ""
#: internal/config/config.go:76 #: internal/config/config.go:77
msgid "Error decoding config file, using defaults" msgid "Error decoding config file, using defaults"
msgstr "" msgstr ""
#: internal/config/config.go:88 #: internal/config/config.go:89
msgid "Unable to detect user config directory" msgid "Unable to detect user config directory"
msgstr "" msgstr ""
#: internal/config/config.go:96 #: internal/config/config.go:97
msgid "Unable to create ALR config directory" msgid "Unable to create ALR config directory"
msgstr "" msgstr ""
#: internal/config/config.go:105 #: internal/config/config.go:106
msgid "Unable to create ALR config file" msgid "Unable to create ALR config file"
msgstr "" msgstr ""
#: internal/config/config.go:111 #: internal/config/config.go:112
msgid "Error encoding default configuration" msgid "Error encoding default configuration"
msgstr "" msgstr ""
#: internal/config/config.go:120 #: internal/config/config.go:121
msgid "Unable to detect cache directory" msgid "Unable to detect cache directory"
msgstr "" msgstr ""
#: internal/config/config.go:130 #: internal/config/config.go:131
msgid "Unable to create repo cache directory" msgid "Unable to create repo cache directory"
msgstr "" msgstr ""
#: internal/config/config.go:136 #: internal/config/config.go:137
msgid "Unable to create package cache directory" msgid "Unable to create package cache directory"
msgstr "" msgstr ""
@ -255,7 +259,7 @@ msgstr ""
msgid "Error initialization database" msgid "Error initialization database"
msgstr "" msgstr ""
#: list.go:88 #: list.go:91
msgid "Error listing installed packages" msgid "Error listing installed packages"
msgstr "" msgstr ""
@ -437,6 +441,6 @@ msgstr ""
msgid "Upgrade all installed packages" msgid "Upgrade all installed packages"
msgstr "" msgstr ""
#: upgrade.go:79 #: upgrade.go:83
msgid "Error checking for updates" msgid "Error checking for updates"
msgstr "" msgstr ""

@ -34,23 +34,23 @@ msgid ""
"Build package from scratch even if there's an already built package available" "Build package from scratch even if there's an already built package available"
msgstr "" msgstr ""
#: build.go:70 #: build.go:71
msgid "Error pulling repositories" msgid "Error pulling repos"
msgstr "" msgstr ""
#: build.go:76 #: build.go:78
msgid "Unable to detect a supported package manager on the system" msgid "Unable to detect a supported package manager on the system"
msgstr "" msgstr ""
#: build.go:87 #: build.go:89
msgid "Error building package" msgid "Error building package"
msgstr "Ошибка при сборке пакета" msgstr "Ошибка при сборке пакета"
#: build.go:93 #: build.go:95
msgid "Error getting working directory" msgid "Error getting working directory"
msgstr "" msgstr ""
#: build.go:101 #: build.go:103
msgid "Error moving the package" msgid "Error moving the package"
msgstr "" msgstr ""
@ -74,10 +74,6 @@ msgstr ""
msgid "Unable to create new cache directory" msgid "Unable to create new cache directory"
msgstr "" msgstr ""
#: fix.go:62
msgid "Error pulling repos"
msgstr ""
#: fix.go:66 #: fix.go:66
msgid "Done" msgid "Done"
msgstr "" msgstr ""
@ -118,43 +114,51 @@ msgstr "Показывать всю информацию, а не только
msgid "Command info expected at least 1 argument, got %d" msgid "Command info expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: info.go:71 #: info.go:73
msgid "Error finding packages" msgid "Error finding packages"
msgstr "" msgstr ""
#: info.go:87 #: info.go:89
msgid "Error parsing os-release file" msgid "Error parsing os-release file"
msgstr "" msgstr ""
#: info.go:96 #: info.go:98
msgid "Error resolving overrides" msgid "Error resolving overrides"
msgstr "" msgstr ""
#: info.go:105 info.go:111 #: info.go:107 info.go:113
msgid "Error encoding script variables" msgid "Error encoding script variables"
msgstr "" msgstr ""
#: install.go:43
msgid "Install a new package"
msgstr ""
#: install.go:57 #: install.go:57
msgid "Command install expected at least 1 argument, got %d" msgid "Command install expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: install.go:90 #: install.go:70
msgid "Error pulling repositories"
msgstr ""
#: install.go:92
msgid "Error getting packages" msgid "Error getting packages"
msgstr "" msgstr ""
#: install.go:99 #: install.go:101
msgid "Error iterating over packages" msgid "Error iterating over packages"
msgstr "" msgstr ""
#: install.go:112 #: install.go:114
msgid "Remove an installed package" msgid "Remove an installed package"
msgstr "" msgstr ""
#: install.go:117 #: install.go:119
msgid "Command remove expected at least 1 argument, got %d" msgid "Command remove expected at least 1 argument, got %d"
msgstr "" msgstr ""
#: install.go:129 #: install.go:131
msgid "Error removing packages" msgid "Error removing packages"
msgstr "" msgstr ""
@ -182,39 +186,39 @@ msgstr ""
msgid "Choose which optional package(s) to install" msgid "Choose which optional package(s) to install"
msgstr "" msgstr ""
#: internal/config/config.go:63 #: internal/config/config.go:64
msgid "Error opening config file, using defaults" msgid "Error opening config file, using defaults"
msgstr "" msgstr ""
#: internal/config/config.go:76 #: internal/config/config.go:77
msgid "Error decoding config file, using defaults" msgid "Error decoding config file, using defaults"
msgstr "" msgstr ""
#: internal/config/config.go:88 #: internal/config/config.go:89
msgid "Unable to detect user config directory" msgid "Unable to detect user config directory"
msgstr "" msgstr ""
#: internal/config/config.go:96 #: internal/config/config.go:97
msgid "Unable to create ALR config directory" msgid "Unable to create ALR config directory"
msgstr "" msgstr ""
#: internal/config/config.go:105 #: internal/config/config.go:106
msgid "Unable to create ALR config file" msgid "Unable to create ALR config file"
msgstr "" msgstr ""
#: internal/config/config.go:111 #: internal/config/config.go:112
msgid "Error encoding default configuration" msgid "Error encoding default configuration"
msgstr "" msgstr ""
#: internal/config/config.go:120 #: internal/config/config.go:121
msgid "Unable to detect cache directory" msgid "Unable to detect cache directory"
msgstr "" msgstr ""
#: internal/config/config.go:130 #: internal/config/config.go:131
msgid "Unable to create repo cache directory" msgid "Unable to create repo cache directory"
msgstr "" msgstr ""
#: internal/config/config.go:136 #: internal/config/config.go:137
msgid "Unable to create package cache directory" msgid "Unable to create package cache directory"
msgstr "" msgstr ""
@ -263,7 +267,7 @@ msgstr ""
msgid "Error initialization database" msgid "Error initialization database"
msgstr "" msgstr ""
#: list.go:88 #: list.go:91
msgid "Error listing installed packages" msgid "Error listing installed packages"
msgstr "" msgstr ""
@ -446,6 +450,6 @@ msgstr ""
msgid "Upgrade all installed packages" msgid "Upgrade all installed packages"
msgstr "" msgstr ""
#: upgrade.go:79 #: upgrade.go:83
msgid "Error checking for updates" msgid "Error checking for updates"
msgstr "" msgstr ""

@ -26,6 +26,7 @@ type Config struct {
IgnorePkgUpdates []string `toml:"ignorePkgUpdates"` IgnorePkgUpdates []string `toml:"ignorePkgUpdates"`
Repos []Repo `toml:"repo"` Repos []Repo `toml:"repo"`
Unsafe Unsafe `toml:"unsafe"` Unsafe Unsafe `toml:"unsafe"`
AutoPull bool `toml:"autoPull"`
} }
// Repo represents a ALR repo within a configuration file // Repo represents a ALR repo within a configuration file

11
list.go

@ -55,10 +55,13 @@ func ListCmd() *cli.Command {
os.Exit(1) os.Exit(1)
} }
rs := repos.New(cfg, db) rs := repos.New(cfg, db)
err = rs.Pull(ctx, cfg.Repos(ctx))
if err != nil { if cfg.AutoPull(ctx) {
slog.Error(gotext.Get("Error pulling repositories"), "err", err) err = rs.Pull(ctx, cfg.Repos(ctx))
os.Exit(1) if err != nil {
slog.Error(gotext.Get("Error pulling repositories"), "err", err)
os.Exit(1)
}
} }
where := "true" where := "true"

@ -56,6 +56,8 @@ func UpgradeCmd() *cli.Command {
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
ctx := c.Context ctx := c.Context
cfg := config.GetInstance(ctx)
info, err := distro.ParseOSRelease(ctx) info, err := distro.ParseOSRelease(ctx)
if err != nil { if err != nil {
slog.Error(gotext.Get("Error parsing os-release file"), "err", err) slog.Error(gotext.Get("Error parsing os-release file"), "err", err)
@ -68,10 +70,12 @@ func UpgradeCmd() *cli.Command {
os.Exit(1) os.Exit(1)
} }
err = repos.Pull(ctx, config.Config(ctx).Repos) if cfg.AutoPull(ctx) {
if err != nil { err = repos.Pull(ctx, config.Config(ctx).Repos)
slog.Error(gotext.Get("Error pulling repos"), "err", err) if err != nil {
os.Exit(1) slog.Error(gotext.Get("Error pulling repos"), "err", err)
os.Exit(1)
}
} }
updates, err := checkForUpdates(ctx, mgr, info) updates, err := checkForUpdates(ctx, mgr, info)