|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "text/tabwriter" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/fatih/color" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func projectList() *cobra.Command { |
| 13 | + return &cobra.Command{ |
| 14 | + Use: "list", |
| 15 | + Aliases: []string{"ls"}, |
| 16 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 17 | + client, err := createClient(cmd) |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + start := time.Now() |
| 22 | + organization, err := currentOrganization(cmd, client) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + projects, err := client.Projects(cmd.Context(), organization.Name) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + if len(projects) == 0 { |
| 32 | + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s No projects found in %s! Create one:\n\n", caret, color.HiWhiteString(organization.Name)) |
| 33 | + _, _ = fmt.Fprintln(cmd.OutOrStdout(), color.HiMagentaString(" $ coder projects create <directory>\n")) |
| 34 | + return nil |
| 35 | + } |
| 36 | + |
| 37 | + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Projects found in %s %s\n\n", |
| 38 | + caret, |
| 39 | + color.HiWhiteString(organization.Name), |
| 40 | + color.HiBlackString("[%dms]", |
| 41 | + time.Since(start).Milliseconds())) |
| 42 | + |
| 43 | + writer := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 4, ' ', 0) |
| 44 | + _, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n", |
| 45 | + color.HiBlackString("Project"), |
| 46 | + color.HiBlackString("Source"), |
| 47 | + color.HiBlackString("Last Updated"), |
| 48 | + color.HiBlackString("Used By")) |
| 49 | + for _, project := range projects { |
| 50 | + suffix := "" |
| 51 | + if project.WorkspaceOwnerCount != 1 { |
| 52 | + suffix = "s" |
| 53 | + } |
| 54 | + _, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n", |
| 55 | + color.New(color.FgHiCyan).Sprint(project.Name), |
| 56 | + color.WhiteString("Archive"), |
| 57 | + color.WhiteString(project.UpdatedAt.Format("January 2, 2006")), |
| 58 | + color.New(color.FgHiWhite).Sprintf("%d developer%s", project.WorkspaceOwnerCount, suffix)) |
| 59 | + } |
| 60 | + return writer.Flush() |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
0 commit comments