2022-03-22 19:17:50 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/coder/coder/cli/cliui"
|
2022-04-01 19:42:36 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-03-22 19:17:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func workspaceList() *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()
|
2022-04-01 19:42:36 +00:00
|
|
|
workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
|
2022-03-22 19:17:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(workspaces) == 0 {
|
|
|
|
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Prompt.String()+"No workspaces found! Create one:")
|
|
|
|
_, _ = fmt.Fprintln(cmd.OutOrStdout())
|
|
|
|
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+cliui.Styles.Code.Render("coder workspaces create <name>"))
|
|
|
|
_, _ = fmt.Fprintln(cmd.OutOrStdout())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s Workspaces found %s\n\n",
|
|
|
|
caret,
|
|
|
|
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\t%s\n",
|
|
|
|
color.HiBlackString("Workspace"),
|
2022-04-06 17:42:40 +00:00
|
|
|
color.HiBlackString("Template"),
|
2022-03-22 19:17:50 +00:00
|
|
|
color.HiBlackString("Status"),
|
|
|
|
color.HiBlackString("Last Built"),
|
|
|
|
color.HiBlackString("Outdated"))
|
|
|
|
for _, workspace := range workspaces {
|
|
|
|
_, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\t%+v\n",
|
|
|
|
color.New(color.FgHiCyan).Sprint(workspace.Name),
|
2022-04-06 17:42:40 +00:00
|
|
|
color.WhiteString(workspace.TemplateName),
|
2022-03-22 19:17:50 +00:00
|
|
|
color.WhiteString(string(workspace.LatestBuild.Transition)),
|
|
|
|
color.WhiteString(workspace.LatestBuild.Job.CompletedAt.Format("January 2, 2006")),
|
|
|
|
workspace.Outdated)
|
|
|
|
}
|
|
|
|
return writer.Flush()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|