Skip to content

Commit

Permalink
Merge pull request #141 from superfly/codepope/issue136
Browse files Browse the repository at this point in the history
codepope/issue136
  • Loading branch information
codepope committed Jun 4, 2020
2 parents 085e5f1 + 2da7057 commit d29d3c0
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
110 changes: 110 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/docstrings"
)

func newListCommand() *Command {
ks := docstrings.Get("list")

listCmd := &Command{
Command: &cobra.Command{
Use: ks.Usage,
Aliases: []string{"ls"},
Short: ks.Short,
Long: ks.Long,
},
}

laks := docstrings.Get("list.apps")
listAppsCmd := BuildCommand(listCmd, runListApps, laks.Usage, laks.Short, laks.Long, os.Stdout)

listAppsCmd.AddStringFlag(StringFlagOpts{
Name: "org",
Shorthand: "o",
Description: `Show only apps in this organisation`,
})

listAppsCmd.AddStringFlag(StringFlagOpts{
Name: "status",
Shorthand: "s",
Description: `Show only apps with this status`,
})

loks := docstrings.Get("list.orgs")
BuildCommand(listCmd, runListOrgs, loks.Usage, loks.Short, loks.Long, os.Stdout)

return listCmd
}

func runList(ctx *CmdContext) error {
fmt.Fprintln(ctx.Out, "list can display apps (list apps) or orgs (list orgs)")
return nil
}

func runListApps(ctx *CmdContext) error {

appPart := ""

if len(ctx.Args) == 1 {
appPart = ctx.Args[0]
} else if len(ctx.Args) > 0 {
fmt.Fprintln(ctx.Out, "Too many arguments - discarding excess")
}

orgslug, _ := ctx.Config.GetString("org")

status, _ := ctx.Config.GetString("status")

apps, err := ctx.Client.API().GetApps()
if err != nil {
return err
}

fmt.Fprintf(ctx.Out, "%32s %10s %16s\n", "Name", "Status", "Organization")

for _, app := range apps {
print := false

if appPart != "" {
print = strings.Contains(app.Name, appPart)
} else {
print = true
}

if orgslug != "" {
print = (print && orgslug == app.Organization.Slug)
}

if status != "" {
print = (print && status == app.Status)
}

if print {
fmt.Fprintf(ctx.Out, "%32s %10s %16s\n", app.Name, app.Status, app.Organization.Slug)
}
}

return nil
}

func runListOrgs(ctx *CmdContext) error {
orgs, err := ctx.Client.API().GetOrganizations()

if err != nil {
return err
}

fmt.Fprintf(ctx.Out, "%16s %-32s\n", "Short Name", "Full Name")

for _, org := range orgs {
fmt.Fprintf(ctx.Out, "%16s %-32s\n", org.Slug, org.Name)
}

return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func init() {
newRegionsCommand(),
newOpenCommand(),
newMonitorCommand(),
newListCommand(),
)

initConfig()
Expand Down
33 changes: 33 additions & 0 deletions helpgen/flyctlhelp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,39 @@ shortHelp="Release an IP address"
longHelp="""Releases an IP address from the application.
"""

[list]
usage="list"
shortHelp="Lists your Fly resources"
longHelp="""The list command is for listing your resources on has two subcommands, apps and orgs.
The apps command lists your applications. There are filtering options available.
The orgs command lists all the organizations you are a member of.
"""

[list.apps]
usage="apps [text] [-o org] [-s status]"
shortHelp="Lists all your apps"
longHelp="""The list apps command lists all your applications. As this may be a
long list, there are options to filter the results.
Specifying a text string as a parameter will only return applications where the
application name contains the text.
The --orgs/-o flag allows you to specify the name of an organization that the
application must be owned by. (see list orgs for organization names).
The --status/-s flag allows you to specify status applications should be at to be
returned in the results. e.g. -s running would only return running applications.
"""

[list.orgs]
usage="orgs"
shortHelp="List all your organizations"
longHelp="""Lists all organizations which your are a member of. It will show the
short name of the organization and the long name.
"""

[logs]
usage="logs"
shortHelp="View App logs"
Expand Down

0 comments on commit d29d3c0

Please sign in to comment.