mirror of
https://github.com/mudler/luet.git
synced 2025-08-29 12:13:09 +00:00
cmd/config: simplify code (#246)
This commit is contained in:
parent
3402641241
commit
0f545952cd
@ -19,7 +19,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
config "github.com/mudler/luet/pkg/config"
|
config "github.com/mudler/luet/pkg/config"
|
||||||
installer "github.com/mudler/luet/pkg/installer"
|
. "github.com/mudler/luet/pkg/logger"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -30,46 +30,12 @@ var configCmd = &cobra.Command{
|
|||||||
Long: `Show luet configuration`,
|
Long: `Show luet configuration`,
|
||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println(config.LuetCfg.GetLogging())
|
data, err := config.LuetCfg.YAML()
|
||||||
fmt.Println(config.LuetCfg.GetGeneral())
|
if err != nil {
|
||||||
fmt.Println(config.LuetCfg.GetSystem())
|
Fatal(err.Error())
|
||||||
if len(config.LuetCfg.CacheRepositories) > 0 {
|
|
||||||
fmt.Println("repetitors:")
|
|
||||||
for _, r := range config.LuetCfg.CacheRepositories {
|
|
||||||
fmt.Println(" - ", r.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(config.LuetCfg.SystemRepositories) > 0 {
|
|
||||||
fmt.Println("repositories:")
|
|
||||||
for _, r := range config.LuetCfg.SystemRepositories {
|
|
||||||
fmt.Println(" - ", r.String())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(config.LuetCfg.RepositoriesConfDir) > 0 {
|
fmt.Println(string(data))
|
||||||
fmt.Println("repos_confdir:")
|
|
||||||
for _, dir := range config.LuetCfg.RepositoriesConfDir {
|
|
||||||
fmt.Println(" - ", dir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(config.LuetCfg.ConfigProtectConfDir) > 0 {
|
|
||||||
|
|
||||||
// Load config protect configs
|
|
||||||
installer.LoadConfigProtectConfs(config.LuetCfg)
|
|
||||||
|
|
||||||
fmt.Println("config_protect_confdir:")
|
|
||||||
for _, dir := range config.LuetCfg.ConfigProtectConfDir {
|
|
||||||
fmt.Println(" - ", dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(config.LuetCfg.GetConfigProtectConfFiles()) > 0 {
|
|
||||||
fmt.Println("protect_conf_files:")
|
|
||||||
for _, file := range config.LuetCfg.GetConfigProtectConfFiles() {
|
|
||||||
fmt.Println(" - ", file.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ var upgradeCmd = &cobra.Command{
|
|||||||
downloadOnly, _ := cmd.Flags().GetBool("download-only")
|
downloadOnly, _ := cmd.Flags().GetBool("download-only")
|
||||||
|
|
||||||
util.SetSystemConfig()
|
util.SetSystemConfig()
|
||||||
util.SetSolverConfig()
|
opts := util.SetSolverConfig()
|
||||||
|
|
||||||
if concurrent {
|
if concurrent {
|
||||||
LuetCfg.GetSolverOptions().Implementation = solver.ParallelSimple
|
LuetCfg.GetSolverOptions().Implementation = solver.ParallelSimple
|
||||||
@ -66,7 +66,7 @@ var upgradeCmd = &cobra.Command{
|
|||||||
LuetCfg.GetSolverOptions().Implementation = solver.SingleCoreSimple
|
LuetCfg.GetSolverOptions().Implementation = solver.SingleCoreSimple
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug("Solver", LuetCfg.GetSolverOptions().String())
|
Debug("Solver", opts.CompactString())
|
||||||
|
|
||||||
// Load config protect configs
|
// Load config protect configs
|
||||||
installer.LoadConfigProtectConfs(LuetCfg)
|
installer.LoadConfigProtectConfs(LuetCfg)
|
||||||
|
@ -26,14 +26,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
|
|
||||||
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
fileHelper "github.com/mudler/luet/pkg/helpers/file"
|
||||||
|
|
||||||
pkg "github.com/mudler/luet/pkg/package"
|
pkg "github.com/mudler/luet/pkg/package"
|
||||||
solver "github.com/mudler/luet/pkg/solver"
|
solver "github.com/mudler/luet/pkg/solver"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
v "github.com/spf13/viper"
|
v "github.com/spf13/viper"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var LuetCfg = NewLuetConfig(v.GetViper())
|
var LuetCfg = NewLuetConfig(v.GetViper())
|
||||||
@ -57,22 +56,22 @@ type LuetLoggingConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LuetGeneralConfig struct {
|
type LuetGeneralConfig struct {
|
||||||
SameOwner bool `mapstructure:"same_owner"`
|
SameOwner bool `yaml:"same_owner,omitempty" mapstructure:"same_owner"`
|
||||||
Concurrency int `mapstructure:"concurrency"`
|
Concurrency int `yaml:"concurrency,omitempty" mapstructure:"concurrency"`
|
||||||
Debug bool `mapstructure:"debug"`
|
Debug bool `yaml:"debug,omitempty" mapstructure:"debug"`
|
||||||
ShowBuildOutput bool `mapstructure:"show_build_output"`
|
ShowBuildOutput bool `yaml:"show_build_output,omitempty" mapstructure:"show_build_output"`
|
||||||
SpinnerMs int `mapstructure:"spinner_ms"`
|
SpinnerMs int `yaml:"spinner_ms,omitempty" mapstructure:"spinner_ms"`
|
||||||
SpinnerCharset int `mapstructure:"spinner_charset"`
|
SpinnerCharset int `yaml:"spinner_charset,omitempty" mapstructure:"spinner_charset"`
|
||||||
FatalWarns bool `mapstructure:"fatal_warnings"`
|
FatalWarns bool `yaml:"fatal_warnings,omitempty" mapstructure:"fatal_warnings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LuetSolverOptions struct {
|
type LuetSolverOptions struct {
|
||||||
solver.Options
|
solver.Options `yaml:"options,omitempty"`
|
||||||
Type string `mapstructure:"type"`
|
Type string `yaml:"type,omitempty" mapstructure:"type"`
|
||||||
LearnRate float32 `mapstructure:"rate"`
|
LearnRate float32 `yaml:"rate,omitempty" mapstructure:"rate"`
|
||||||
Discount float32 `mapstructure:"discount"`
|
Discount float32 `yaml:"discount,omitempty" mapstructure:"discount"`
|
||||||
MaxAttempts int `mapstructure:"max_attempts"`
|
MaxAttempts int `yaml:"max_attempts,omitempty" mapstructure:"max_attempts"`
|
||||||
Implementation solver.SolverType `mapstructure:"implementation"`
|
Implementation solver.SolverType `yaml:"implementation,omitempty" mapstructure:"implementation"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opts LuetSolverOptions) ResolverIsSet() bool {
|
func (opts LuetSolverOptions) ResolverIsSet() bool {
|
||||||
@ -179,9 +178,9 @@ type LuetRepository struct {
|
|||||||
// Serialized options not used in repository configuration
|
// Serialized options not used in repository configuration
|
||||||
|
|
||||||
// Incremented value that identify revision of the repository in a user-friendly way.
|
// Incremented value that identify revision of the repository in a user-friendly way.
|
||||||
Revision int `json:"revision,omitempty" yaml:"-,omitempty" mapstructure:"-,omitempty"`
|
Revision int `json:"revision,omitempty" yaml:"-" mapstructure:"-"`
|
||||||
// Epoch time in seconds
|
// Epoch time in seconds
|
||||||
LastUpdate string `json:"last_update,omitempty" yaml:"-,omitempty" mapstructure:"-,omitempty"`
|
LastUpdate string `json:"last_update,omitempty" yaml:"-" mapstructure:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLuetRepository(name, t, descr string, urls []string, priority int, enable, cached bool) *LuetRepository {
|
func NewLuetRepository(name, t, descr string, urls []string, priority int, enable, cached bool) *LuetRepository {
|
||||||
@ -227,23 +226,23 @@ type LuetKV struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LuetConfig struct {
|
type LuetConfig struct {
|
||||||
Viper *v.Viper
|
Viper *v.Viper `yaml:"-"`
|
||||||
|
|
||||||
Logging LuetLoggingConfig `mapstructure:"logging"`
|
Logging LuetLoggingConfig `yaml:"logging,omitempty" mapstructure:"logging"`
|
||||||
General LuetGeneralConfig `mapstructure:"general"`
|
General LuetGeneralConfig `yaml:"general,omitempty" mapstructure:"general"`
|
||||||
System LuetSystemConfig `mapstructure:"system"`
|
System LuetSystemConfig `yaml:"system" mapstructure:"system"`
|
||||||
Solver LuetSolverOptions `mapstructure:"solver"`
|
Solver LuetSolverOptions `yaml:"solver,omitempty" mapstructure:"solver"`
|
||||||
|
|
||||||
RepositoriesConfDir []string `mapstructure:"repos_confdir"`
|
RepositoriesConfDir []string `yaml:"repos_confdir,omitempty" mapstructure:"repos_confdir"`
|
||||||
ConfigProtectConfDir []string `mapstructure:"config_protect_confdir"`
|
ConfigProtectConfDir []string `yaml:"config_protect_confdir,omitempty" mapstructure:"config_protect_confdir"`
|
||||||
ConfigProtectSkip bool `mapstructure:"config_protect_skip"`
|
ConfigProtectSkip bool `yaml:"config_protect_skip,omitempty" mapstructure:"config_protect_skip"`
|
||||||
ConfigFromHost bool `mapstructure:"config_from_host"`
|
ConfigFromHost bool `yaml:"config_from_host,omitempty" mapstructure:"config_from_host"`
|
||||||
CacheRepositories []LuetRepository `mapstructure:"repetitors"`
|
CacheRepositories []LuetRepository `yaml:"repetitors,omitempty" mapstructure:"repetitors"`
|
||||||
SystemRepositories []LuetRepository `mapstructure:"repositories"`
|
SystemRepositories []LuetRepository `yaml:"repositories,omitempty" mapstructure:"repositories"`
|
||||||
|
|
||||||
FinalizerEnvs []LuetKV `json:"finalizer_envs,omitempty" yaml:"finalizer_envs,omitempty" mapstructure:"finalizer_envs,omitempty"`
|
FinalizerEnvs []LuetKV `json:"finalizer_envs,omitempty" yaml:"finalizer_envs,omitempty" mapstructure:"finalizer_envs,omitempty"`
|
||||||
|
|
||||||
ConfigProtectConfFiles []ConfigProtectConfFile
|
ConfigProtectConfFiles []ConfigProtectConfFile `yaml:"-" mapstructure:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLuetConfig(viper *v.Viper) *LuetConfig {
|
func NewLuetConfig(viper *v.Viper) *LuetConfig {
|
||||||
@ -381,6 +380,10 @@ func (c *LuetConfig) GetSolverOptions() *LuetSolverOptions {
|
|||||||
return &c.Solver
|
return &c.Solver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *LuetConfig) YAML() ([]byte, error) {
|
||||||
|
return yaml.Marshal(c)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *LuetConfig) GetConfigProtectConfFiles() []ConfigProtectConfFile {
|
func (c *LuetConfig) GetConfigProtectConfFiles() []ConfigProtectConfFile {
|
||||||
return c.ConfigProtectConfFiles
|
return c.ConfigProtectConfFiles
|
||||||
}
|
}
|
||||||
@ -409,34 +412,6 @@ func (c *LuetConfig) GetSystemRepository(name string) (*LuetRepository, error) {
|
|||||||
return ans, nil
|
return ans, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LuetSolverOptions) String() string {
|
|
||||||
ans := fmt.Sprintf(`
|
|
||||||
solver:
|
|
||||||
type: %s
|
|
||||||
rate: %f
|
|
||||||
discount: %f
|
|
||||||
max_attempts: %d`, c.Type, c.LearnRate, c.Discount,
|
|
||||||
c.MaxAttempts)
|
|
||||||
|
|
||||||
return ans
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LuetGeneralConfig) String() string {
|
|
||||||
ans := fmt.Sprintf(`
|
|
||||||
general:
|
|
||||||
concurrency: %d
|
|
||||||
same_owner: %t
|
|
||||||
debug: %t
|
|
||||||
fatal_warnings: %t
|
|
||||||
show_build_output: %t
|
|
||||||
spinner_ms: %d
|
|
||||||
spinner_charset: %d`, c.Concurrency, c.SameOwner, c.Debug,
|
|
||||||
c.FatalWarns, c.ShowBuildOutput,
|
|
||||||
c.SpinnerMs, c.SpinnerCharset)
|
|
||||||
|
|
||||||
return ans
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LuetGeneralConfig) GetSpinnerMs() time.Duration {
|
func (c *LuetGeneralConfig) GetSpinnerMs() time.Duration {
|
||||||
duration, err := time.ParseDuration(fmt.Sprintf("%dms", c.SpinnerMs))
|
duration, err := time.ParseDuration(fmt.Sprintf("%dms", c.SpinnerMs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -449,34 +424,6 @@ func (c *LuetLoggingConfig) SetLogLevel(s string) {
|
|||||||
c.Level = s
|
c.Level = s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LuetLoggingConfig) String() string {
|
|
||||||
ans := fmt.Sprintf(`
|
|
||||||
logging:
|
|
||||||
enable_logfile: %t
|
|
||||||
path: %s
|
|
||||||
json_format: %t
|
|
||||||
color: %t
|
|
||||||
enable_emoji: %t
|
|
||||||
level: %s`, c.EnableLogFile, c.Path, c.JsonFormat,
|
|
||||||
c.Color, c.EnableEmoji, c.Level)
|
|
||||||
|
|
||||||
return ans
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LuetSystemConfig) String() string {
|
|
||||||
ans := fmt.Sprintf(`
|
|
||||||
system:
|
|
||||||
database_engine: %s
|
|
||||||
database_path: %s
|
|
||||||
pkgs_cache_path: %s
|
|
||||||
tmpdir_base: %s
|
|
||||||
rootfs: %s`,
|
|
||||||
c.DatabaseEngine, c.DatabasePath, c.PkgsCachePath,
|
|
||||||
c.TmpDirBase, c.Rootfs)
|
|
||||||
|
|
||||||
return ans
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LuetSystemConfig) InitTmpDir() error {
|
func (c *LuetSystemConfig) InitTmpDir() error {
|
||||||
if !filepath.IsAbs(c.TmpDirBase) {
|
if !filepath.IsAbs(c.TmpDirBase) {
|
||||||
abs, err := fileHelper.Rel2Abs(c.TmpDirBase)
|
abs, err := fileHelper.Rel2Abs(c.TmpDirBase)
|
||||||
|
@ -64,8 +64,8 @@ type Solver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Type SolverType
|
Type SolverType `yaml:"type,omitempty"`
|
||||||
Concurrency int
|
Concurrency int `yaml:"concurrency,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSolver accepts as argument two lists of packages, the first is the initial set,
|
// NewSolver accepts as argument two lists of packages, the first is the initial set,
|
||||||
|
Loading…
Reference in New Issue
Block a user