Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus/remote/list: Add column flag #1101

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 136 additions & 42 deletions cmd/incus/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type cmdRemote struct {
global *cmdGlobal
}

type remoteColumn struct {
Name string
Data func(string, config.Remote) string
}

// Command returns a cobra.Command for use with (*cobra.Command).AddCommand.
func (c *cmdRemote) Command() *cobra.Command {
cmd := &cobra.Command{}
Expand Down Expand Up @@ -706,7 +711,8 @@ type cmdRemoteList struct {
global *cmdGlobal
remote *cmdRemote

flagFormat string
flagFormat string
flagColumns string
}

// Command returns a cobra.Command for use with (*cobra.Command).AddCommand.
Expand All @@ -716,14 +722,130 @@ func (c *cmdRemoteList) Command() *cobra.Command {
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List the available remotes")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List the available remotes`))
`List the available remotes

Default column layout: nupaPsg

== Columns ==
The -c option takes a comma separated list of arguments that control
which instance attributes to output when displaying in table or csv
format.

Column arguments are either pre-defined shorthand chars (see below),
or (extended) config keys.

Commas between consecutive shorthand chars are optional.

Pre-defined column shorthand chars:
n - Name
u - URL
p - Protocol
a - Auth Type
P - Public
s - Static
g - Global`))

cmd.RunE = c.Run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultRemoteColumns, i18n.G("Columns")+"``")

return cmd
}

const defaultRemoteColumns = "nupaPsg"

func (c *cmdRemoteList) parseColumns() ([]remoteColumn, error) {
columnsShorthandMap := map[rune]remoteColumn{
'n': {i18n.G("NAME"), c.remoteNameColumnData},
'u': {i18n.G("URL"), c.addrColumnData},
'p': {i18n.G("PROTOCOL"), c.protocolColumnData},
'a': {i18n.G("AUTH TYPE"), c.authTypeColumnData},
'P': {i18n.G("PUBLIC"), c.publicColumnData},
's': {i18n.G("STATIC"), c.staticColumnData},
'g': {i18n.G("GLOBAL"), c.globalColumnData},
}

columnList := strings.Split(c.flagColumns, ",")
columns := []remoteColumn{}

for _, columnEntry := range columnList {
if columnEntry == "" {
return nil, fmt.Errorf(i18n.G("Empty column entry (redundant, leading or trailing command) in '%s'"), c.flagColumns)
}

for _, columnRune := range columnEntry {
column, ok := columnsShorthandMap[columnRune]
if !ok {
return nil, fmt.Errorf(i18n.G("Unknown column shorthand char '%c' in '%s'"), columnRune, columnEntry)
}

columns = append(columns, column)
}
}

return columns, nil
}

func (c *cmdRemoteList) remoteNameColumnData(name string, rc config.Remote) string {
conf := c.global.conf

strName := name
if name == conf.DefaultRemote {
strName = fmt.Sprintf("%s (%s)", name, i18n.G("current"))
}

return strName
}

func (c *cmdRemoteList) addrColumnData(name string, rc config.Remote) string {
return rc.Addr
}

func (c *cmdRemoteList) protocolColumnData(name string, rc config.Remote) string {
return rc.Protocol
}

func (c *cmdRemoteList) authTypeColumnData(name string, rc config.Remote) string {
if rc.AuthType == "" {
if strings.HasPrefix(rc.Addr, "unix:") {
rc.AuthType = "file access"
} else if rc.Protocol != "incus" {
rc.AuthType = "none"
} else {
rc.AuthType = api.AuthenticationMethodTLS
}
}

return rc.AuthType
}

func (c *cmdRemoteList) publicColumnData(name string, rc config.Remote) string {
strPublic := i18n.G("NO")
if rc.Public {
strPublic = i18n.G("YES")
}

return strPublic
}

func (c *cmdRemoteList) staticColumnData(name string, rc config.Remote) string {
strStatic := i18n.G("NO")
if rc.Static {
strStatic = i18n.G("YES")
}

return strStatic
}

func (c *cmdRemoteList) globalColumnData(name string, rc config.Remote) string {
strGlobal := i18n.G("NO")
if rc.Global {
strGlobal = i18n.G("YES")
}

return strGlobal
}

// Run is used in the RunE field of the cobra.Command returned by Command.
func (c *cmdRemoteList) Run(cmd *cobra.Command, args []string) error {
conf := c.global.conf
Expand All @@ -734,56 +856,28 @@ func (c *cmdRemoteList) Run(cmd *cobra.Command, args []string) error {
return err
}

columns, err := c.parseColumns()
if err != nil {
return err
}

// List the remotes
data := [][]string{}
for name, rc := range conf.Remotes {
strPublic := i18n.G("NO")
if rc.Public {
strPublic = i18n.G("YES")
}

strStatic := i18n.G("NO")
if rc.Static {
strStatic = i18n.G("YES")
}

strGlobal := i18n.G("NO")
if rc.Global {
strGlobal = i18n.G("YES")
}

if rc.Protocol == "" {
rc.Protocol = "incus"
}

if rc.AuthType == "" {
if strings.HasPrefix(rc.Addr, "unix:") {
rc.AuthType = "file access"
} else if rc.Protocol != "incus" {
rc.AuthType = "none"
} else {
rc.AuthType = api.AuthenticationMethodTLS
}
}

strName := name
if name == conf.DefaultRemote {
strName = fmt.Sprintf("%s (%s)", name, i18n.G("current"))
line := []string{}
for _, column := range columns {
line = append(line, column.Data(name, rc))
}

data = append(data, []string{strName, rc.Addr, rc.Protocol, rc.AuthType, strPublic, strStatic, strGlobal})
data = append(data, line)
}

sort.Sort(cli.SortColumnsNaturally(data))

header := []string{
i18n.G("NAME"),
i18n.G("URL"),
i18n.G("PROTOCOL"),
i18n.G("AUTH TYPE"),
i18n.G("PUBLIC"),
i18n.G("STATIC"),
i18n.G("GLOBAL"),
header := []string{}
for _, column := range columns {
header = append(header, column.Name)
}

return cli.RenderTable(c.flagFormat, header, data, conf.Remotes)
Expand Down
Loading
Loading