diff --git a/config.go b/config.go index 9eb0713..50da8ea 100644 --- a/config.go +++ b/config.go @@ -76,6 +76,7 @@ var configKeys = []string{ "autoPull", "logLevel", "ignorePkgUpdates", + "updateSystemOnUpgrade", } func SetConfig() *cli.Command { @@ -137,6 +138,12 @@ func SetConfig() *cli.Command { } } deps.Cfg.System.SetIgnorePkgUpdates(updates) + case "updateSystemOnUpgrade": + boolValue, err := strconv.ParseBool(value) + if err != nil { + return cliutils.FormatCliExit(gotext.Get("invalid boolean value for %s: %s", key, value), err) + } + deps.Cfg.System.SetUpdateSystemOnUpgrade(boolValue) case "repo", "repos": return cliutils.FormatCliExit(gotext.Get("use 'repo add/remove' commands to manage repositories"), nil) default: @@ -206,6 +213,8 @@ func GetConfig() *cli.Command { } else { fmt.Println(strings.Join(updates, ", ")) } + case "updateSystemOnUpgrade": + fmt.Println(deps.Cfg.UpdateSystemOnUpgrade()) case "repo", "repos": repos := deps.Cfg.Repos() if len(repos) == 0 { diff --git a/internal/config/config.go b/internal/config/config.go index ef12d83..a88f578 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -56,6 +56,7 @@ func defaultConfigKoanf() *koanf.Koanf { "ignorePkgUpdates": []string{}, "logLevel": "info", "autoPull": true, + "updateSystemOnUpgrade": false, "repos": []types.Repo{ { Name: "alr-default", @@ -114,6 +115,11 @@ func (c *ALRConfig) Load() error { } } + // Выполняем миграцию конфигурации при необходимости + if err := c.migrateConfig(); err != nil { + return fmt.Errorf("failed to migrate config: %w", err) + } + return nil } @@ -125,6 +131,45 @@ func (c *ALRConfig) ToYAML() (string, error) { return string(data), nil } +func (c *ALRConfig) migrateConfig() error { + // Проверяем, существует ли конфигурационный файл + if _, err := os.Stat(constants.SystemConfigPath); os.IsNotExist(err) { + // Если файла нет, но конфигурация уже загружена (из defaults или env), + // создаем файл с настройкой по умолчанию + needsCreation := false + + // Проверяем, установлена ли переменная окружения ALR_UPDATESYSTEMONUPGRADE + if os.Getenv("ALR_UPDATESYSTEMONUPGRADE") == "" { + // Если переменная не установлена, проверяем наличие пакетов ALR + // чтобы определить, нужно ли включить эту опцию для обновления + needsCreation = true + } + + if needsCreation { + // Устанавливаем значение false по умолчанию для новой опции + c.System.SetUpdateSystemOnUpgrade(false) + // Сохраняем конфигурацию + if err := c.System.Save(); err != nil { + // Если не удается сохранить - это не критично, продолжаем работу + return nil + } + } + } else { + // Если файл существует, проверяем, есть ли в нем новая опция + if !c.System.k.Exists("updateSystemOnUpgrade") { + // Если опции нет, добавляем ее со значением по умолчанию + c.System.SetUpdateSystemOnUpgrade(false) + // Сохраняем обновленную конфигурацию + if err := c.System.Save(); err != nil { + // Если не удается сохранить - это не критично, продолжаем работу + return nil + } + } + } + + return nil +} + func (c *ALRConfig) RootCmd() string { return c.cfg.RootCmd } func (c *ALRConfig) PagerStyle() string { return c.cfg.PagerStyle } func (c *ALRConfig) AutoPull() bool { return c.cfg.AutoPull } @@ -133,4 +178,5 @@ func (c *ALRConfig) SetRepos(repos []types.Repo) { c.System.SetRepos(repos) } func (c *ALRConfig) IgnorePkgUpdates() []string { return c.cfg.IgnorePkgUpdates } func (c *ALRConfig) LogLevel() string { return c.cfg.LogLevel } func (c *ALRConfig) UseRootCmd() bool { return c.cfg.UseRootCmd } +func (c *ALRConfig) UpdateSystemOnUpgrade() bool { return c.cfg.UpdateSystemOnUpgrade } func (c *ALRConfig) GetPaths() *Paths { return c.paths } diff --git a/internal/config/system_config.go b/internal/config/system_config.go index 393dd96..49c98a0 100644 --- a/internal/config/system_config.go +++ b/internal/config/system_config.go @@ -142,3 +142,10 @@ func (c *SystemConfig) SetRepos(v []types.Repo) { panic(err) } } + +func (c *SystemConfig) SetUpdateSystemOnUpgrade(v bool) { + err := c.k.Set("updateSystemOnUpgrade", v) + if err != nil { + panic(err) + } +} diff --git a/pkg/types/config.go b/pkg/types/config.go index f11cc68..9b18e76 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -21,13 +21,14 @@ package types // Config represents the ALR configuration file type Config struct { - RootCmd string `json:"rootCmd" koanf:"rootCmd"` - UseRootCmd bool `json:"useRootCmd" koanf:"useRootCmd"` - PagerStyle string `json:"pagerStyle" koanf:"pagerStyle"` - IgnorePkgUpdates []string `json:"ignorePkgUpdates" koanf:"ignorePkgUpdates"` - Repos []Repo `json:"repo" koanf:"repo"` - AutoPull bool `json:"autoPull" koanf:"autoPull"` - LogLevel string `json:"logLevel" koanf:"logLevel"` + RootCmd string `json:"rootCmd" koanf:"rootCmd"` + UseRootCmd bool `json:"useRootCmd" koanf:"useRootCmd"` + PagerStyle string `json:"pagerStyle" koanf:"pagerStyle"` + IgnorePkgUpdates []string `json:"ignorePkgUpdates" koanf:"ignorePkgUpdates"` + Repos []Repo `json:"repo" koanf:"repo"` + AutoPull bool `json:"autoPull" koanf:"autoPull"` + LogLevel string `json:"logLevel" koanf:"logLevel"` + UpdateSystemOnUpgrade bool `json:"updateSystemOnUpgrade" koanf:"updateSystemOnUpgrade"` } // Repo represents a ALR repo within a configuration file diff --git a/upgrade.go b/upgrade.go index 2d67d68..ec84a5b 100644 --- a/upgrade.go +++ b/upgrade.go @@ -84,6 +84,19 @@ func UpgradeCmd() *cli.Command { } defer deps.Defer() + // Обновляем систему, если это включено в конфигурации + if deps.Cfg.UpdateSystemOnUpgrade() { + slog.Info(gotext.Get("Updating system packages...")) + err = deps.Manager.UpgradeAll(&manager.Opts{ + NoConfirm: !c.Bool("interactive"), + Args: manager.Args, + }) + if err != nil { + return cliutils.FormatCliExit(gotext.Get("Error updating system packages"), err) + } + slog.Info(gotext.Get("System packages updated successfully")) + } + builder, err := build.NewMainBuilder( deps.Cfg, deps.Manager,