This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Implement coder images ls #189
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
implement coder images ls
- Loading branch information
There are no files selected for viewing
This file contains hidden or 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,43 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/pkg/tcli" | ||
) | ||
|
||
func TestImagesCLI(t *testing.T) { | ||
t.Parallel() | ||
|
||
run(t, "coder-cli-images-tests", func(t *testing.T, ctx context.Context, c *tcli.ContainerRunner) { | ||
headlessLogin(ctx, t, c) | ||
|
||
// Successfully output help. | ||
c.Run(ctx, "coder images --help").Assert(t, | ||
tcli.Success(), | ||
tcli.StdoutMatches(regexp.QuoteMeta("Manage existing images and/or import new ones.")), | ||
tcli.StderrEmpty(), | ||
) | ||
|
||
// OK - human output | ||
c.Run(ctx, "coder images ls").Assert(t, | ||
tcli.Success(), | ||
) | ||
|
||
imgs := []coder.Image{} | ||
// OK - json output | ||
c.Run(ctx, "coder images ls --output json").Assert(t, | ||
tcli.Success(), | ||
tcli.StdoutJSONUnmarshal(&imgs), | ||
) | ||
|
||
// Org not found | ||
c.Run(ctx, "coder images ls --org doesntexist").Assert(t, | ||
tcli.Error(), | ||
tcli.StderrMatches(regexp.QuoteMeta("org name \"doesntexist\" not found\n\n")), | ||
) | ||
}) | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
## coder images | ||
|
||
Manage Coder images | ||
|
||
### Synopsis | ||
|
||
Manage existing images and/or import new ones. | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for images | ||
--user string Specifies the user by email (default "me") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-v, --verbose show verbose output | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [coder](coder.md) - coder provides a CLI for working with an existing Coder Enterprise installation | ||
* [coder images ls](coder_images_ls.md) - list all images available to the active user | ||
|
This file contains hidden or 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,31 @@ | ||
## coder images ls | ||
|
||
list all images available to the active user | ||
|
||
### Synopsis | ||
|
||
List all Coder images available to the active user. | ||
|
||
``` | ||
coder images ls [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for ls | ||
--org string organization name | ||
--output string human | json (default "human") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
--user string Specifies the user by email (default "me") | ||
-v, --verbose show verbose output | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [coder images](coder_images.md) - Manage Coder images | ||
|
This file contains hidden or 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 hidden or 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,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/pkg/clog" | ||
"cdr.dev/coder-cli/pkg/tablewriter" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func imgsCmd() *cobra.Command { | ||
var user string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "images", | ||
Short: "Manage Coder images", | ||
Long: "Manage existing images and/or import new ones.", | ||
} | ||
|
||
cmd.PersistentFlags().StringVar(&user, "user", coder.Me, "Specifies the user by email") | ||
cmd.AddCommand(lsImgsCommand(&user)) | ||
return cmd | ||
} | ||
|
||
func lsImgsCommand(user *string) *cobra.Command { | ||
var ( | ||
orgName string | ||
outputFmt string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "ls", | ||
Short: "list all images available to the active user", | ||
Long: "List all Coder images available to the active user.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
client, err := newClient(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
imgs, err := getImgs(ctx, client, | ||
getImgsConf{ | ||
email: *user, | ||
orgName: orgName, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(imgs) < 1 { | ||
clog.LogInfo("no images found") | ||
imgs = []coder.Image{} // ensures that json output still marshals | ||
} | ||
|
||
switch outputFmt { | ||
case jsonOutput: | ||
enc := json.NewEncoder(os.Stdout) | ||
// pretty print the json | ||
enc.SetIndent("", "\t") | ||
|
||
if err := enc.Encode(imgs); err != nil { | ||
return xerrors.Errorf("write images as JSON: %w", err) | ||
} | ||
return nil | ||
case humanOutput: | ||
err = tablewriter.WriteTable(len(imgs), func(i int) interface{} { | ||
return imgs[i] | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("write table: %w", err) | ||
} | ||
return nil | ||
default: | ||
return xerrors.Errorf("%q is not a supported value for --output", outputFmt) | ||
} | ||
}, | ||
} | ||
cmd.Flags().StringVar(&orgName, "org", "", "organization name") | ||
cmd.Flags().StringVar(&outputFmt, "output", humanOutput, "human | json") | ||
return cmd | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 💯