-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add image list and rm commands #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b3ab19
feat: allow listing images from the CLI
billyb2 d251e2f
feat: add image rm command
billyb2 768a5b0
docs: explain why transformation logic is necessary
billyb2 0c05148
refactor: specify pageSize in an easier to read way
billyb2 56d7a4c
fix: handle errors from fmt.Scanln
billyb2 56cf9cd
refactor: don't shadow projectID
billyb2 cee6fff
Support deleting by tag
jacobwgillespie 89a63a8
Merge branch 'main' into billy/feat/depot-cli-img
billyb2 1619ec4
Merge branch 'main' into billy/feat/depot-cli-img
billyb2 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
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
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,22 @@ | ||
| package image | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdImage() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "image", | ||
| Short: "Manage container images in the registry", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return fmt.Errorf("missing subcommand, please run `depot image --help`") | ||
| }, | ||
| } | ||
|
|
||
| cmd.AddCommand(NewCmdList()) | ||
| cmd.AddCommand(NewCmdRM()) | ||
|
|
||
| return cmd | ||
| } |
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,354 @@ | ||
| package image | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/csv" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "connectrpc.com/connect" | ||
| "github.com/charmbracelet/bubbles/table" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/charmbracelet/lipgloss" | ||
| "github.com/depot/cli/pkg/api" | ||
| "github.com/depot/cli/pkg/helpers" | ||
| v1 "github.com/depot/cli/pkg/proto/depot/build/v1" | ||
| "github.com/depot/cli/pkg/proto/depot/build/v1/buildv1connect" | ||
| "github.com/pkg/errors" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdList() *cobra.Command { | ||
| var projectID string | ||
| var token string | ||
| var outputFormat string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Aliases: []string{"ls"}, | ||
| Short: "List images in the registry", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cwd, _ := os.Getwd() | ||
| resolvedProjectID := helpers.ResolveProjectID(projectID, cwd) | ||
| if resolvedProjectID == "" { | ||
| return errors.Errorf("unknown project ID (run `depot init` or use --project or $DEPOT_PROJECT_ID)") | ||
| } | ||
|
|
||
| token, err := helpers.ResolveProjectAuth(context.Background(), token) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if token == "" { | ||
| return fmt.Errorf("missing API token, please run `depot login`") | ||
| } | ||
|
|
||
| client := api.NewRegistryClient() | ||
|
|
||
| // Auto-detect CSV output for non-terminal | ||
| if !helpers.IsTerminal() && outputFormat == "" { | ||
| outputFormat = "csv" | ||
| } | ||
|
|
||
| if outputFormat != "" { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| images, err := fetchAllImages(ctx, resolvedProjectID, token, client) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(images) == 0 { | ||
| fmt.Println("No images found") | ||
| return nil | ||
| } | ||
|
|
||
| switch outputFormat { | ||
| case "csv": | ||
| return images.WriteCSV() | ||
| case "json": | ||
| return images.WriteJSON() | ||
| default: | ||
| return errors.Errorf("unknown format: %s. Requires csv or json", outputFormat) | ||
| } | ||
| } | ||
|
|
||
| // Interactive table view | ||
| columns := []table.Column{ | ||
| {Title: "Tag", Width: 50}, | ||
| {Title: "Size", Width: 15}, | ||
| {Title: "Pushed", Width: 20}, | ||
| {Title: "Digest", Width: 30}, | ||
| } | ||
|
|
||
| styles := table.DefaultStyles() | ||
| styles.Header = styles.Header. | ||
| BorderStyle(lipgloss.NormalBorder()). | ||
| BorderForeground(lipgloss.Color("240")). | ||
| BorderBottom(true). | ||
| Bold(false) | ||
|
|
||
| styles.Selected = styles.Selected. | ||
| Foreground(lipgloss.Color("229")). | ||
| Background(lipgloss.Color("57")). | ||
| Bold(false) | ||
|
|
||
| tbl := table.New( | ||
| table.WithColumns(columns), | ||
| table.WithFocused(true), | ||
| table.WithStyles(styles), | ||
| ) | ||
|
|
||
| m := imagesModel{ | ||
| client: client, | ||
| imagesTable: tbl, | ||
| columns: columns, | ||
| projectID: resolvedProjectID, | ||
| token: token, | ||
| } | ||
|
|
||
| _, err = tea.NewProgram(m, tea.WithAltScreen()).Run() | ||
| return err | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVar(&projectID, "project", "", "Depot project ID") | ||
| flags.StringVar(&token, "token", "", "Depot token") | ||
| flags.StringVar(&outputFormat, "output", "", "Non-interactive output format (json, csv)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| type DepotImage struct { | ||
| Tag string `json:"tag"` | ||
| Digest string `json:"digest"` | ||
| SizeBytes uint64 `json:"size_bytes"` | ||
| PushedAt *time.Time `json:"pushed_at,omitempty"` | ||
| } | ||
|
|
||
| type DepotImages []DepotImage | ||
|
|
||
| func fetchAllImages(ctx context.Context, projectID, token string, client buildv1connect.RegistryServiceClient) (DepotImages, error) { | ||
| var allImages DepotImages | ||
| var pageToken string | ||
|
|
||
| for { | ||
| pageSize := int32(100) | ||
| req := connect.NewRequest(&v1.ListImagesRequest{ | ||
| ProjectId: projectID, | ||
| PageSize: &pageSize, | ||
| }) | ||
| if pageToken != "" { | ||
| req.Msg.PageToken = &pageToken | ||
| } | ||
|
|
||
| req = api.WithAuthentication(req, token) | ||
| resp, err := client.ListImages(ctx, req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to list images: %w", err) | ||
| } | ||
|
|
||
| for _, img := range resp.Msg.Images { | ||
| var pushedAt *time.Time | ||
| if img.PushedAt != nil { | ||
| t := img.PushedAt.AsTime() | ||
| pushedAt = &t | ||
| } | ||
| allImages = append(allImages, DepotImage{ | ||
| Tag: img.Tag, | ||
| Digest: img.Digest, | ||
| SizeBytes: img.SizeBytes, | ||
| PushedAt: pushedAt, | ||
| }) | ||
| } | ||
|
|
||
| if resp.Msg.NextPageToken == nil || *resp.Msg.NextPageToken == "" { | ||
| break | ||
| } | ||
| pageToken = *resp.Msg.NextPageToken | ||
| } | ||
|
|
||
| // Sort images by pushedAt timestamp, newest first | ||
| sort.Slice(allImages, func(i, j int) bool { | ||
| // Handle nil timestamps - put images without timestamps at the end | ||
| if allImages[i].PushedAt == nil && allImages[j].PushedAt == nil { | ||
| return false | ||
| } | ||
| if allImages[i].PushedAt == nil { | ||
| return false | ||
| } | ||
| if allImages[j].PushedAt == nil { | ||
| return true | ||
| } | ||
| // Sort by newest first | ||
| return allImages[i].PushedAt.After(*allImages[j].PushedAt) | ||
| }) | ||
|
|
||
| return allImages, nil | ||
| } | ||
|
|
||
| func (images DepotImages) WriteCSV() error { | ||
| w := csv.NewWriter(os.Stdout) | ||
| if len(images) > 0 { | ||
| if err := w.Write([]string{"Tag", "Digest", "Size (bytes)", "Pushed At"}); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| for _, img := range images { | ||
| var pushedAt string | ||
| if img.PushedAt != nil { | ||
| pushedAt = img.PushedAt.Format(time.RFC3339) | ||
| } else { | ||
| pushedAt = "" | ||
| } | ||
|
|
||
| row := []string{img.Tag, img.Digest, fmt.Sprintf("%d", img.SizeBytes), pushedAt} | ||
| if err := w.Write(row); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| w.Flush() | ||
| return w.Error() | ||
| } | ||
|
|
||
| // WriteJSON outputs images in JSON format | ||
| func (images DepotImages) WriteJSON() error { | ||
| enc := json.NewEncoder(os.Stdout) | ||
| enc.SetIndent("", " ") | ||
| return enc.Encode(images) | ||
| } | ||
|
|
||
| // Bubbletea model for interactive image list | ||
| type imagesModel struct { | ||
| client buildv1connect.RegistryServiceClient | ||
| imagesTable table.Model | ||
| columns []table.Column | ||
| projectID string | ||
| token string | ||
| err error | ||
| } | ||
|
|
||
| func (m imagesModel) Init() tea.Cmd { | ||
| return m.loadImages() | ||
| } | ||
|
|
||
| func (m imagesModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| var cmd tea.Cmd | ||
| switch msg := msg.(type) { | ||
| case tea.KeyMsg: | ||
| if msg.Type == tea.KeyCtrlC || msg.Type == tea.KeyEsc { | ||
| return m, tea.Quit | ||
| } | ||
|
|
||
| if msg.String() == "q" { | ||
| return m, tea.Quit | ||
| } | ||
|
|
||
| if msg.String() == "r" { | ||
| return m, m.loadImages() | ||
| } | ||
|
|
||
| case tea.WindowSizeMsg: | ||
| m.resizeTable(msg) | ||
|
|
||
| case imageRows: | ||
| m.err = nil | ||
| m.imagesTable.SetRows(msg) | ||
|
|
||
| case errMsg: | ||
| m.err = msg.error | ||
| } | ||
|
|
||
| m.imagesTable, cmd = m.imagesTable.Update(msg) | ||
| return m, cmd | ||
| } | ||
|
|
||
| func (m *imagesModel) resizeTable(msg tea.WindowSizeMsg) { | ||
| h, v := baseStyle.GetFrameSize() | ||
| m.imagesTable.SetHeight(msg.Height - v - 3) | ||
| m.imagesTable.SetWidth(msg.Width - h) | ||
|
|
||
| colWidth := 0 | ||
| for _, col := range m.columns { | ||
| colWidth += col.Width | ||
| } | ||
|
|
||
| remainingWidth := msg.Width - colWidth | ||
| if remainingWidth > 0 { | ||
| m.columns[len(m.columns)-1].Width += remainingWidth - h - 4 | ||
| m.imagesTable.SetColumns(m.columns) | ||
| } | ||
| } | ||
|
|
||
| func (m imagesModel) View() string { | ||
| s := baseStyle.Render(m.imagesTable.View()) + "\n" | ||
| if m.err != nil { | ||
| s = "Error: " + m.err.Error() + "\n" | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| type imageRows []table.Row | ||
| type errMsg struct{ error } | ||
|
|
||
| func (m imagesModel) loadImages() tea.Cmd { | ||
| return func() tea.Msg { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| images, err := fetchAllImages(ctx, m.projectID, m.token, m.client) | ||
| if err != nil { | ||
| return errMsg{err} | ||
| } | ||
|
|
||
| rows := []table.Row{} | ||
| for _, img := range images { | ||
| tag := img.Tag | ||
| if len(tag) > 50 { | ||
| tag = tag[:47] + "..." | ||
| } | ||
|
|
||
| size := formatSize(img.SizeBytes) | ||
|
|
||
| var pushedStr string | ||
| if img.PushedAt != nil { | ||
| pushedStr = img.PushedAt.Format(time.RFC3339) | ||
| } else { | ||
| pushedStr = "-" | ||
| } | ||
|
|
||
| digest := img.Digest | ||
| if len(digest) > 30 { | ||
| digest = digest[:27] + "..." | ||
| } | ||
|
|
||
| rows = append(rows, table.Row{tag, size, pushedStr, digest}) | ||
| } | ||
|
|
||
| return imageRows(rows) | ||
| } | ||
| } | ||
|
|
||
| var baseStyle = lipgloss.NewStyle(). | ||
| BorderStyle(lipgloss.NormalBorder()). | ||
| BorderForeground(lipgloss.Color("240")) | ||
|
|
||
| func formatSize(bytes uint64) string { | ||
| const unit = 1024 | ||
| if bytes < unit { | ||
| return fmt.Sprintf("%d B", bytes) | ||
| } | ||
| div, exp := uint64(unit), 0 | ||
| for n := bytes / unit; n >= unit; n /= unit { | ||
| div *= unit | ||
| exp++ | ||
| } | ||
| return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp]) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.