Skip to content

Commit

Permalink
Added sorting CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Oct 2, 2023
1 parent fbb29bd commit e38b113
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 13 deletions.
19 changes: 18 additions & 1 deletion src/cmd/0-init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package cmd

import "github.com/f1bonacc1/process-compose/src/config"
import (
"fmt"
"github.com/f1bonacc1/process-compose/src/config"
"github.com/f1bonacc1/process-compose/src/tui"
"github.com/spf13/pflag"
"strings"
)

var pcFlags *config.Flags
var commonFlags *pflag.FlagSet

func init() {
pcFlags = config.NewFlags()
commonFlags = pflag.NewFlagSet("", pflag.ContinueOnError)
commonFlags.BoolVarP(pcFlags.IsReverseSort, "reverse", "R", *pcFlags.IsReverseSort, "sort in reverse order")
commonFlags.StringVarP(
pcFlags.SortColumn,
"sort",
"S",
*pcFlags.SortColumn,
fmt.Sprintf("sort column name. legal values (case insensitive): [%s]", strings.Join(tui.ColumnNames(), ", ")),
)

}
7 changes: 4 additions & 3 deletions src/cmd/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"github.com/f1bonacc1/process-compose/src/client"
"github.com/f1bonacc1/process-compose/src/tui"
"github.com/spf13/cobra"
"time"
)

// attachCmd represents the attach command
Expand All @@ -13,7 +11,7 @@ var attachCmd = &cobra.Command{
Short: "Attach the Process Compose TUI Remotely to a Running Process Compose Server",
Run: func(cmd *cobra.Command, args []string) {
pcClient := client.NewClient(*pcFlags.Address, *pcFlags.PortNum, *pcFlags.LogLength)
tui.SetupTui(pcClient, time.Duration(*pcFlags.RefreshRate)*time.Second)
startTui(pcClient)
},
}

Expand All @@ -22,4 +20,7 @@ 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"))

}
16 changes: 14 additions & 2 deletions src/cmd/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func setSignal(signalHandler func()) {

func runHeadless(project *app.ProjectRunner) int {
setSignal(func() {
project.ShutDownProject()
_ = project.ShutDownProject()
})
exitCode := project.Run()
return exitCode
Expand All @@ -67,12 +67,24 @@ func runTui(project *app.ProjectRunner) int {
tui.Stop()
})
defer quiet()()
go tui.SetupTui(project, time.Duration(*pcFlags.RefreshRate)*time.Second)
go startTui(project)
exitCode := project.Run()
tui.Stop()
return exitCode
}

func startTui(runner app.IProject) {
col, err := tui.StringToColumnID(*pcFlags.SortColumn)
if err != nil {
log.Err(err).Msgf("Invalid column name %s provided. Using %s", *pcFlags.SortColumn, tui.ProcessStateName)
col = tui.ProcessStateName
}
tui.SetupTui(runner,
tui.WithRefreshRate(time.Duration(*pcFlags.RefreshRate)*time.Second),
tui.WithStateSorter(col, !*pcFlags.IsReverseSort),
)
}

func quiet() func() {
null, _ := os.Open(os.DevNull)
sout := os.Stdout
Expand Down
7 changes: 5 additions & 2 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ var (
logFile = setupLogger()
log.Info().Msgf("Process Compose %s", config.Version)
},
Run: run,
RunE: run,
}
)

func run(cmd *cobra.Command, args []string) {
func run(cmd *cobra.Command, args []string) error {
defer func() {
_ = logFile.Close()
}()
runner := getProjectRunner([]string{}, false)
api.StartHttpServer(!*pcFlags.Headless, *pcFlags.PortNum, runner)
runProject(runner)
return nil
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -64,6 +65,8 @@ func init() {
rootCmd.Flags().StringArrayVarP(&opts.FileNames, "config", "f", config.GetConfigDefault(), "path to config files to load (env: "+config.ConfigEnvVarName+")")
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"))
}

func logFatal(err error, format string, args ...interface{}) {
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ 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"))

}
11 changes: 10 additions & 1 deletion src/config/Flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import "math"
import (
"math"
)

const (
// DefaultRefreshRate represents the refresh interval.
Expand All @@ -17,6 +19,9 @@ const (

// DefaultLogLength represents the default log length.
DefaultLogLength = 1000

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

const (
Expand All @@ -40,6 +45,8 @@ type Flags struct {
Write *bool
NoDependencies *bool
HideDisabled *bool
SortColumn *string
IsReverseSort *bool
}

// NewFlags returns new configuration flags.
Expand All @@ -56,6 +63,8 @@ func NewFlags() *Flags {
LogTailLength: intPtr(math.MaxInt),
NoDependencies: boolPtr(false),
HideDisabled: boolPtr(false),
SortColumn: strPtr(DefaultSortColumn),
IsReverseSort: boolPtr(false),
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/tui/procstate_sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/f1bonacc1/process-compose/src/types"
"sort"
"strings"
)

type ColumnID int
Expand All @@ -20,6 +21,52 @@ const (
ProcessStateExit ColumnID = 7
)

var columnNames = map[ColumnID]string{
ProcessStateUndefined: "",
ProcessStatePid: "PID",
ProcessStateName: "NAME",
ProcessStateNamespace: "NAMESPACE",
ProcessStateStatus: "STATUS",
ProcessStateAge: "AGE",
ProcessStateHealth: "HEALTH",
ProcessStateRestarts: "RESTARTS",
ProcessStateExit: "EXIT",
}

var columnIDs = map[string]ColumnID{
"": ProcessStateUndefined,
"PID": ProcessStatePid,
"NAME": ProcessStateName,
"NAMESPACE": ProcessStateNamespace,
"STATUS": ProcessStateStatus,
"AGE": ProcessStateAge,
"HEALTH": ProcessStateHealth,
"RESTARTS": ProcessStateRestarts,
"EXIT": ProcessStateExit,
}

func (c ColumnID) String() string {
return columnNames[c]
}

func StringToColumnID(s string) (ColumnID, error) {
id, ok := columnIDs[strings.ToUpper(s)]
if !ok {
return ProcessStateUndefined, fmt.Errorf("unknown column name: %s", s)
}
return id, nil
}
func ColumnNames() []string {
var names []string
for _, name := range columnNames {
if name != "" {
names = append(names, name)
}
}
sort.Strings(names)
return names
}

type StateSorter struct {
sortByColumn ColumnID
isAsc bool
Expand Down
19 changes: 19 additions & 0 deletions src/tui/tui_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tui

import "time"

type Option func(view *pcView) error

func WithRefreshRate(rate time.Duration) Option {
return func(view *pcView) error {
view.refreshRate = rate
return nil
}
}

func WithStateSorter(column ColumnID, isAscending bool) Option {
return func(view *pcView) error {
view.stateSorter = StateSorter{sortByColumn: column, isAsc: isAscending}
return nil
}
}
13 changes: 9 additions & 4 deletions src/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type pcView struct {
refreshRate time.Duration
}

func newPcView(project app.IProject, refreshRate time.Duration) *pcView {
func newPcView(project app.IProject) *pcView {
//_ = pv.shortcuts.loadFromFile("short-cuts-new.yaml")
pv := &pcView{
appView: tview.NewApplication(),
Expand All @@ -72,12 +72,12 @@ func newPcView(project app.IProject, refreshRate time.Duration) *pcView {
logsTextArea: tview.NewTextArea(),
logSelect: false,
project: project,
refreshRate: time.Second,
stateSorter: StateSorter{
sortByColumn: ProcessStateName,
isAsc: true,
},
procColumns: map[ColumnID]string{},
refreshRate: refreshRate,
}
pv.statTable = pv.createStatTable()
go pv.loadProcNames()
Expand Down Expand Up @@ -374,9 +374,14 @@ func (pv *pcView) startMonitoring() {
}(pcClient)
}

func SetupTui(project app.IProject, refreshRate time.Duration) {
func SetupTui(project app.IProject, options ...Option) {

pv := newPcView(project, refreshRate)
pv := newPcView(project)
for _, option := range options {
if err := option(pv); err != nil {
log.Error().Err(err).Msg("Failed to apply option")
}
}

go pv.updateTable()
go pv.updateLogs()
Expand Down

0 comments on commit e38b113

Please sign in to comment.