Skip to content

Commit

Permalink
incus/remote/list: Add support for column selection
Browse files Browse the repository at this point in the history
Part of lxc#1027

Signed-off-by: HassanAlsamahi <hassanalsamahi12@gmail.com>
  • Loading branch information
HassanAlsamahi authored and stgraber committed Aug 9, 2024
1 parent a819663 commit 9618520
Showing 1 changed file with 134 additions and 42 deletions.
176 changes: 134 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,128 @@ 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,58 +854,30 @@ 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")
line := []string{}
for _, column := range columns {
line = append(line, column.Data(name, rc))
}

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

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"))
}

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

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

0 comments on commit 9618520

Please sign in to comment.