forked from hetznercloud/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow sorting to be configured globally (closes hetznercloud#418)
I am a heavy user of the hcloud-cli. However, I really don't like the default sorting by server-id, and would prefer sorting by age (see hetznercloud#417/hetznercloud#420) or name. Currently, I always type `hcloud server list -s name` to sort by name. since the `-s` flag is part of the `list` subcommand and not the `hcloud` base command (like in many other software that makes use of the Cobra framework), I cannot simply `alias hcloud-"hcloud -s name"` in my shell to default the sorting to "by name". This commit adds a `[defaults]` section to the config toml which allows to override the default sorting behavior for individual `hcloud` subcommands. The following behavior is used to determine the sorting-order | config file | flag | result | |-------------|---------|----------------------| | not-set | not-set | sort by id (default) | | not-set | set | sort by flag value | | set | not-set | sort by config value | | set | set | sort by flag value |
- Loading branch information
Showing
24 changed files
with
338 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/hetznercloud/cli/internal/cmd/config/sorting" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "config [FLAGS]", | ||
Short: "Manage config", | ||
Args: cobra.NoArgs, | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
cmd.AddCommand( | ||
sorting.NewSortCommand(cli), | ||
) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package sorting | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/output" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var listTableOutput *output.Table | ||
|
||
func init() { | ||
listTableOutput = output.NewTable(). | ||
AddAllowedFields(SortPresentation{}) | ||
} | ||
|
||
type SortPresentation struct { | ||
Command string | ||
Column string | ||
Order string | ||
} | ||
|
||
func newListCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "list [COMMAND]", | ||
Short: "See the default sorting order for a command", | ||
Args: cobra.MaximumNArgs(1), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: cli.Wrap(runList), | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runList(cli *state.State, cmd *cobra.Command, args []string) error { | ||
if len(args) == 1 { | ||
return runListCommand(cli, cmd, args) | ||
} | ||
|
||
return runListAll(cli, cmd, args) | ||
} | ||
|
||
func runListAll(cli *state.State, cmd *cobra.Command, args []string) error { | ||
outOpts := output.FlagsForCommand(cmd) | ||
|
||
cols := []string{"command", "column"} | ||
|
||
tw := listTableOutput | ||
if err := tw.ValidateColumns(cols); err != nil { | ||
return err | ||
} | ||
|
||
if !outOpts.IsSet("noheader") { | ||
tw.WriteHeader(cols) | ||
} | ||
|
||
for command, defaults := range cli.Config.SubcommandDefaults { | ||
if defaults != nil { | ||
presentation := SortPresentation{ | ||
Command: command, | ||
Column: strings.Join(defaults.Sorting, ", "), | ||
Order: "", | ||
} | ||
|
||
tw.Write(cols, presentation) | ||
} | ||
} | ||
|
||
tw.Flush() | ||
return nil | ||
} | ||
|
||
func runListCommand(cli *state.State, cmd *cobra.Command, args []string) error { | ||
outOpts := output.FlagsForCommand(cmd) | ||
|
||
cols := []string{"column", "order"} | ||
|
||
command := args[0] | ||
|
||
tw := listTableOutput | ||
if err := tw.ValidateColumns(cols); err != nil { | ||
return err | ||
} | ||
|
||
if !outOpts.IsSet("noheader") { | ||
tw.WriteHeader(cols) | ||
} | ||
|
||
defaults := cli.Config.SubcommandDefaults[command] | ||
|
||
if defaults != nil { | ||
for _, column := range defaults.Sorting { | ||
order := "asc" | ||
|
||
// handle special case where colum-name includes the : to specify the order | ||
if strings.Contains(column, ":") { | ||
columnWithOrdering := strings.Split(column, ":") | ||
if len(columnWithOrdering) != 2 { | ||
return errors.New("Column sort syntax invalid") | ||
} | ||
|
||
column = columnWithOrdering[0] | ||
order = columnWithOrdering[1] | ||
} | ||
|
||
presentation := SortPresentation{ | ||
Command: command, | ||
Column: column, | ||
Order: order, | ||
} | ||
|
||
tw.Write(cols, presentation) | ||
} | ||
} | ||
|
||
tw.Flush() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package sorting | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newResetCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "reset COMMAND", | ||
Short: "Reset to the application default sorting order for a command", | ||
Args: cobra.ExactArgs(1), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: cli.Wrap(runReset), | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runReset(cli *state.State, cmd *cobra.Command, args []string) error { | ||
command := strings.TrimSpace(args[0]) | ||
if command == "" { | ||
return errors.New("invalid command") | ||
} | ||
|
||
if cli.Config.SubcommandDefaults == nil { | ||
return nil | ||
} | ||
|
||
defaults := cli.Config.SubcommandDefaults[command] | ||
if defaults != nil { | ||
defaults.Sorting = nil | ||
} | ||
|
||
cli.Config.SubcommandDefaults[command] = defaults | ||
|
||
if err := cli.WriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Reset sorting to the default sorting order for command '%s list'\n", command) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package sorting | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newSetCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "set COMMAND COLUMNS...", | ||
Short: "Set the default sorting order for a command", | ||
Long: "Configure how the list subcommand of each command sorts it output. Append `:asc` or `:desc` to the column name control sorting (Default: `:asc`). You can also sort by multiple columns", | ||
Args: cobra.MinimumNArgs(2), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
RunE: cli.Wrap(runSet), | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runSet(cli *state.State, cmd *cobra.Command, args []string) error { | ||
command := strings.TrimSpace(args[0]) | ||
if command == "" { | ||
return errors.New("invalid command") | ||
} | ||
|
||
if len(args[1:]) == 0 { | ||
return errors.New("invalid columns") | ||
} | ||
|
||
columns := make([]string, len(args[1:])) | ||
for index, columnName := range args[1:] { | ||
columns[index] = strings.TrimSpace(columnName) | ||
} | ||
|
||
if cli.Config.SubcommandDefaults == nil { | ||
cli.Config.SubcommandDefaults = make(map[string]*state.SubcommandDefaults) | ||
} | ||
|
||
defaults := cli.Config.SubcommandDefaults[command] | ||
if defaults == nil { | ||
defaults = &state.SubcommandDefaults{ | ||
Sorting: columns, | ||
} | ||
} else { | ||
defaults.Sorting = columns | ||
} | ||
|
||
cli.Config.SubcommandDefaults[command] = defaults | ||
|
||
if err := cli.WriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Sorting by columns '%s' by default for command '%s list'\n", strings.Join(columns, ", "), command) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sorting | ||
|
||
import ( | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewSortCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "sort COMMAND", | ||
Short: "Configure the default sorting order for a command", | ||
Args: cobra.MinimumNArgs(2), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
cmd.AddCommand( | ||
newSetCommand(cli), | ||
newListCommand(cli), | ||
) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.