-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add "projects list" command to the CLI (#333)
This adds a WorkspaceOwnerCount parameter returned from the projects API. It's helpful to display the amount of usage a specific project has.
- Loading branch information
Showing
13 changed files
with
311 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"text/tabwriter" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func projectList() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
start := time.Now() | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return err | ||
} | ||
projects, err := client.Projects(cmd.Context(), organization.Name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(projects) == 0 { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s No projects found in %s! Create one:\n\n", caret, color.HiWhiteString(organization.Name)) | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), color.HiMagentaString(" $ coder projects create <directory>\n")) | ||
return nil | ||
} | ||
|
||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Projects found in %s %s\n\n", | ||
caret, | ||
color.HiWhiteString(organization.Name), | ||
color.HiBlackString("[%dms]", | ||
time.Since(start).Milliseconds())) | ||
|
||
writer := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 4, ' ', 0) | ||
_, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n", | ||
color.HiBlackString("Project"), | ||
color.HiBlackString("Source"), | ||
color.HiBlackString("Last Updated"), | ||
color.HiBlackString("Used By")) | ||
for _, project := range projects { | ||
suffix := "" | ||
if project.WorkspaceOwnerCount != 1 { | ||
suffix = "s" | ||
} | ||
_, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n", | ||
color.New(color.FgHiCyan).Sprint(project.Name), | ||
color.WhiteString("Archive"), | ||
color.WhiteString(project.UpdatedAt.Format("January 2, 2006")), | ||
color.New(color.FgHiWhite).Sprintf("%d developer%s", project.WorkspaceOwnerCount, suffix)) | ||
} | ||
return writer.Flush() | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cli_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/pty/ptytest" | ||
) | ||
|
||
func TestProjectList(t *testing.T) { | ||
t.Parallel() | ||
t.Run("None", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t) | ||
coderdtest.CreateInitialUser(t, client) | ||
cmd, root := clitest.New(t, "projects", "list") | ||
clitest.SetupConfig(t, client, root) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
closeChan := make(chan struct{}) | ||
go func() { | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
close(closeChan) | ||
}() | ||
pty.ExpectMatch("No projects found") | ||
<-closeChan | ||
}) | ||
t.Run("List", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t) | ||
user := coderdtest.CreateInitialUser(t, client) | ||
daemon := coderdtest.NewProvisionerDaemon(t, client) | ||
job := coderdtest.CreateProjectImportJob(t, client, user.Organization, nil) | ||
coderdtest.AwaitProjectImportJob(t, client, user.Organization, job.ID) | ||
_ = daemon.Close() | ||
project := coderdtest.CreateProject(t, client, user.Organization, job.ID) | ||
cmd, root := clitest.New(t, "projects", "list") | ||
clitest.SetupConfig(t, client, root) | ||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
closeChan := make(chan struct{}) | ||
go func() { | ||
err := cmd.Execute() | ||
require.NoError(t, err) | ||
close(closeChan) | ||
}() | ||
pty.ExpectMatch(project.Name) | ||
<-closeChan | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.