| Initial import of the rickub CLI as a standalone public project 1a1d430 Olivier Girardot 9h ago | 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/spf13/cobra" |
| 7 | ) |
| 8 | |
| 9 | func init() { |
| 10 | orgCmd := &cobra.Command{ |
| 11 | Use: "org <handle>", |
| 12 | Short: "Read organization info, members, and teams", |
| 13 | } |
| 14 | |
| 15 | viewCmd := &cobra.Command{ |
| 16 | Use: "view <handle>", |
| 17 | Short: "Show basic org info", |
| 18 | Args: cobra.ExactArgs(1), |
| 19 | RunE: runOrgView, |
| 20 | } |
| 21 | |
| 22 | membersCmd := &cobra.Command{ |
| 23 | Use: "members <handle>", |
| 24 | Short: "List org members (members only)", |
| 25 | Args: cobra.ExactArgs(1), |
| 26 | RunE: runOrgMembers, |
| 27 | } |
| 28 | |
| 29 | teamsCmd := &cobra.Command{ |
| 30 | Use: "teams <handle>", |
| 31 | Short: "List org teams (members only)", |
| 32 | Args: cobra.ExactArgs(1), |
| 33 | RunE: runOrgTeams, |
| 34 | } |
| 35 | |
| 36 | orgCmd.AddCommand(viewCmd, membersCmd, teamsCmd) |
| 37 | rootCmd.AddCommand(orgCmd) |
| 38 | } |
| 39 | |
| 40 | func runOrgView(cmd *cobra.Command, args []string) error { |
| 41 | client, err := newClient() |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | o, err := client.GetOrg(cmd.Context(), args[0]) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | if flagJSON { |
| 50 | return printJSON(cmd.OutOrStdout(), o) |
| 51 | } |
| 52 | out := cmd.OutOrStdout() |
| 53 | fmt.Fprintf(out, "%s\n", o.Handle) |
| 54 | fmt.Fprintf(out, "Display name: %s\n", dash(o.DisplayName)) |
| 55 | fmt.Fprintf(out, "Created: %s\n", humanTime(o.CreatedAt)) |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | func runOrgMembers(cmd *cobra.Command, args []string) error { |
| 60 | client, err := newClient() |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | members, err := client.ListOrgMembers(cmd.Context(), args[0]) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | if flagJSON { |
| 69 | return printJSON(cmd.OutOrStdout(), members) |
| 70 | } |
| 71 | if len(members) == 0 { |
| 72 | fmt.Fprintln(cmd.OutOrStdout(), "No members.") |
| 73 | return nil |
| 74 | } |
| 75 | tw := newTabw(cmd.OutOrStdout()) |
| 76 | fmt.Fprintln(tw, "HANDLE\tROLE\tNAME") |
| 77 | for _, m := range members { |
| 78 | fmt.Fprintf(tw, "%s\t%s\t%s\n", m.Handle, m.Role, dash(m.DisplayName)) |
| 79 | } |
| 80 | tw.Flush() |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func runOrgTeams(cmd *cobra.Command, args []string) error { |
| 85 | client, err := newClient() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | teams, err := client.ListOrgTeams(cmd.Context(), args[0]) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if flagJSON { |
| 94 | return printJSON(cmd.OutOrStdout(), teams) |
| 95 | } |
| 96 | if len(teams) == 0 { |
| 97 | fmt.Fprintln(cmd.OutOrStdout(), "No teams.") |
| 98 | return nil |
| 99 | } |
| 100 | tw := newTabw(cmd.OutOrStdout()) |
| 101 | fmt.Fprintln(tw, "SLUG\tNAME\tSUB-TEAM\tDESCRIPTION") |
| 102 | for _, t := range teams { |
| 103 | fmt.Fprintf(tw, "%s\t%s\t%v\t%s\n", t.Slug, dash(t.Name), t.SubTeam, dash(t.Description)) |
| 104 | } |
| 105 | tw.Flush() |
| 106 | return nil |
| 107 | } |