Skip to content

Commit

Permalink
Issue #175: Don't load settings in headless mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed May 6, 2024
1 parent 4afe98e commit 3697dfc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
12 changes: 9 additions & 3 deletions src/cmd/0-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import (
var pcFlags *config.Flags
var commonFlags *pflag.FlagSet

const (
flagTheme = "theme"
flagReverse = "reverse"
flagSort = "sort"
)

func init() {
pcFlags = config.NewFlags()
commonFlags = pflag.NewFlagSet("", pflag.ContinueOnError)
commonFlags.BoolVarP(pcFlags.IsReverseSort, "reverse", "R", *pcFlags.IsReverseSort, "sort in reverse order")
commonFlags.BoolVarP(pcFlags.IsReverseSort, flagReverse, "R", *pcFlags.IsReverseSort, "sort in reverse order")
commonFlags.StringVarP(
pcFlags.SortColumn,
"sort",
flagSort,
"S",
*pcFlags.SortColumn,
fmt.Sprintf("sort column name. legal values (case insensitive): [%s]", strings.Join(tui.ColumnNames(), ", ")),
)
commonFlags.StringVar(pcFlags.PcTheme, "theme", *pcFlags.PcTheme, "select process compose theme")
commonFlags.StringVar(pcFlags.PcTheme, flagTheme, *pcFlags.PcTheme, "select process compose theme")
}
6 changes: 3 additions & 3 deletions src/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func init() {
attachCmd.Flags().IntVarP(pcFlags.RefreshRate, "ref-rate", "r", *pcFlags.RefreshRate, "TUI refresh rate in seconds")
attachCmd.Flags().StringVarP(pcFlags.Address, "address", "a", *pcFlags.Address, "address of the target process compose server")
attachCmd.Flags().IntVarP(pcFlags.LogLength, "log-length", "l", *pcFlags.LogLength, "log length to display in TUI")
attachCmd.Flags().AddFlag(commonFlags.Lookup("reverse"))
attachCmd.Flags().AddFlag(commonFlags.Lookup("sort"))
attachCmd.Flags().AddFlag(commonFlags.Lookup("theme"))
attachCmd.Flags().AddFlag(commonFlags.Lookup(flagReverse))
attachCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
attachCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))

}
32 changes: 26 additions & 6 deletions src/cmd/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,36 @@ func runTui(project *app.ProjectRunner) int {
}

func startTui(runner app.IProject) {
col, err := tui.StringToColumnID(*pcFlags.SortColumn)
tuiOptions := []tui.Option{
tui.WithRefreshRate(time.Duration(*pcFlags.RefreshRate) * time.Second),
}
settings := config.NewSettings().Load()

tuiOptions = append(tuiOptions,
ternary(pcFlags.PcThemeChanged, tui.WithTheme(*pcFlags.PcTheme), tui.WithTheme(settings.Theme)))

tuiOptions = append(tuiOptions,
ternary(pcFlags.SortColumnChanged,
tui.WithStateSorter(getColumnId(*pcFlags.SortColumn), !*pcFlags.IsReverseSort),
tui.WithStateSorter(getColumnId(settings.Sort.By), !settings.Sort.IsReversed)))

tui.SetupTui(runner, tuiOptions...)
}

func getColumnId(columnName string) tui.ColumnID {
col, err := tui.StringToColumnID(columnName)
if err != nil {
log.Err(err).Msgf("Invalid column name %s provided. Using %s", *pcFlags.SortColumn, config.DefaultSortColumn)
col = tui.ProcessStateName
}
tui.SetupTui(runner,
tui.WithRefreshRate(time.Duration(*pcFlags.RefreshRate)*time.Second),
tui.WithStateSorter(col, !*pcFlags.IsReverseSort),
tui.WithTheme(*pcFlags.PcTheme),
)
return col
}

func ternary[T any](cond bool, a, b T) T {
if cond {
return a
}
return b
}

func quiet() func() {
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
if isUnixSocketMode(cmd) {
*pcFlags.IsUnixSocket = true
}
pcFlags.PcThemeChanged = cmd.Flags().Changed(flagTheme)
pcFlags.SortColumnChanged = cmd.Flags().Changed(flagSort)
},
RunE: run,
}
Expand Down Expand Up @@ -75,9 +77,9 @@ func init() {
rootCmd.Flags().StringArrayVarP(&opts.FileNames, "config", "f", config.GetConfigDefault(), "path to config files to load (env: "+config.EnvVarNameConfig+")")
rootCmd.Flags().StringArrayVarP(&nsAdmitter.EnabledNamespaces, "namespace", "n", nil, "run only specified namespaces (default all)")
rootCmd.PersistentFlags().StringVarP(pcFlags.LogFile, "log-file", "L", *pcFlags.LogFile, "Specify the log file path (env: "+config.LogPathEnvVarName+")")
rootCmd.Flags().AddFlag(commonFlags.Lookup("reverse"))
rootCmd.Flags().AddFlag(commonFlags.Lookup("sort"))
rootCmd.Flags().AddFlag(commonFlags.Lookup("theme"))
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagReverse))
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
rootCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))

if runtime.GOOS != "windows" {
//rootCmd.Flags().BoolVarP(pcFlags.IsDetached, "detached", "D", *pcFlags.IsDetached, "run process-compose in detached mode")
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func init() {
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("ref-rate"))
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("tui"))
upCmd.Flags().AddFlag(rootCmd.Flags().Lookup("hide-disabled"))
upCmd.Flags().AddFlag(commonFlags.Lookup("reverse"))
upCmd.Flags().AddFlag(commonFlags.Lookup("sort"))
upCmd.Flags().AddFlag(commonFlags.Lookup("theme"))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagReverse))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagSort))
upCmd.Flags().AddFlag(commonFlags.Lookup(flagTheme))

}
12 changes: 8 additions & 4 deletions src/config/Flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const (

// DefaultSortColumn represents the default sort column.
DefaultSortColumn = "NAME"

// DefaultThemeName represents the default theme
DefaultThemeName = "Default"
)

const (
Expand All @@ -48,18 +51,19 @@ type Flags struct {
NoDependencies *bool
HideDisabled *bool
SortColumn *string
SortColumnChanged bool
IsReverseSort *bool
NoServer *bool
KeepTuiOn *bool
IsOrderedShutDown *bool
PcTheme *string
PcThemeChanged bool
UnixSocketPath *string
IsUnixSocket *bool
}

// NewFlags returns new configuration flags.
func NewFlags() *Flags {
set := NewSettings().Load()
return &Flags{
RefreshRate: toPtr(DefaultRefreshRate),
Headless: toPtr(getTuiDefault()),
Expand All @@ -72,12 +76,12 @@ func NewFlags() *Flags {
LogTailLength: toPtr(math.MaxInt),
NoDependencies: toPtr(false),
HideDisabled: toPtr(false),
SortColumn: toPtr(set.Sort.By),
IsReverseSort: toPtr(set.Sort.IsReversed),
SortColumn: toPtr(DefaultSortColumn),
IsReverseSort: toPtr(false),
NoServer: toPtr(getNoServerDefault()),
KeepTuiOn: toPtr(false),
IsOrderedShutDown: toPtr(false),
PcTheme: toPtr(set.Theme),
PcTheme: toPtr(DefaultThemeName),
UnixSocketPath: toPtr(""),
IsUnixSocket: toPtr(false),
}
Expand Down
16 changes: 15 additions & 1 deletion src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (

func NewSettings() *Settings {
return &Settings{
Theme: "Default",
Theme: DefaultThemeName,
Sort: Sort{
By: DefaultSortColumn,
IsReversed: false,
Expand All @@ -29,14 +29,20 @@ func NewSettings() *Settings {

func (s *Settings) Load() *Settings {
filePath := GetSettingsPath()
if !fileExists(filePath) {
return s
}
b, err := os.ReadFile(filePath)
if err != nil {
log.Warn().Err(err).Msgf("Error reading settings file %s", filePath)
return s
}
err = yaml.Unmarshal(b, s)
if err != nil {
log.Warn().Err(err).Msgf("Error parsing settings file %s", filePath)
return s
}
log.Debug().Msgf("Loaded settings from %s", filePath)
return s
}

Expand All @@ -50,3 +56,11 @@ func (s *Settings) Save() error {
err = os.WriteFile(filePath, b, 0644)
return err
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

0 comments on commit 3697dfc

Please sign in to comment.