cli/cmd/ghcs/list.go
Christian Gregg 09a6609050 Show * after branch name if codespace working directory is dirty
Append a `*` to the end of a branch name in `ghcs list` if the working
directory of the codespace is dirty (has uncommited or unpushed
changes).

Closes: #104
2021-09-09 12:57:39 +01:00

77 lines
1.5 KiB
Go

package main
import (
"context"
"fmt"
"os"
"github.com/github/ghcs/api"
"github.com/github/ghcs/cmd/ghcs/output"
"github.com/spf13/cobra"
)
type listOptions struct {
asJSON bool
}
func newListCmd() *cobra.Command {
opts := &listOptions{}
listCmd := &cobra.Command{
Use: "list",
Short: "List your codespaces",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return list(opts)
},
}
listCmd.Flags().BoolVar(&opts.asJSON, "json", false, "Output as JSON")
return listCmd
}
func init() {
rootCmd.AddCommand(newListCmd())
}
func list(opts *listOptions) error {
apiClient := api.New(os.Getenv("GITHUB_TOKEN"))
ctx := context.Background()
user, err := apiClient.GetUser(ctx)
if err != nil {
return fmt.Errorf("error getting user: %v", err)
}
codespaces, err := apiClient.ListCodespaces(ctx, user)
if err != nil {
return fmt.Errorf("error getting codespaces: %v", err)
}
table := output.NewTable(os.Stdout, opts.asJSON)
table.SetHeader([]string{"Name", "Repository", "Branch", "State", "Created At"})
for _, codespace := range codespaces {
table.Append([]string{
codespace.Name,
codespace.RepositoryNWO,
branch(codespace),
codespace.Environment.State,
codespace.CreatedAt,
})
}
table.Render()
return nil
}
func branch(codespace *api.Codespace) string {
name := codespace.Branch
gitStatus := codespace.Environment.GitStatus
if gitStatus.HasUncommitedChanges || gitStatus.HasUnpushedChanges {
name += "*"
}
return name
}