33 ImageRegistriesService ,
44} from "@cloudflare/containers-shared" ;
55import { logger } from "../../logger" ;
6+ import { getAccountId } from "../../user" ;
67import { handleFailure , promiseSpinner } from "../common" ;
78import type { Config } from "../../config" ;
89import type { containersScope } from "../../containers" ;
@@ -100,7 +101,7 @@ async function handleDeleteImageCommand(
100101 `Failed to delete image ${ args . image } : ${ gcResponse . status } ${ gcResponse . statusText } `
101102 ) ;
102103 }
103- await logger . log ( `Deleted tag: ${ args . image } ` ) ;
104+ logger . log ( `Deleted tag: ${ args . image } ` ) ;
104105 } ) ,
105106 { message : "Deleting" , json : args . json }
106107 ) ;
@@ -111,24 +112,27 @@ async function handleDeleteImageCommand(
111112
112113async function handleListImagesCommand (
113114 args : StrictYargsOptionsToInterfaceJSON < typeof listImagesYargs > ,
114- _config : Config
115+ config : Config
115116) {
116117 try {
117118 return await promiseSpinner (
118119 getCreds ( ) . then ( async ( creds ) => {
119120 const repos = await listRepos ( creds ) ;
120- const tags : TagsResponse [ ] = [ ] ;
121+ const responses : TagsResponse [ ] = [ ] ;
122+ const accountId = config . account_id || ( await getAccountId ( config ) ) ;
123+ const accountIdPrefix = new RegExp ( `^${ accountId } /` ) ;
124+ const filter = new RegExp ( args . filter ?? "" ) ;
121125 for ( const repo of repos ) {
122126 const stripped = repo . replace ( / ^ \/ + / , "" ) ;
123- const regex = new RegExp ( args . filter ?? "" ) ;
124- if ( regex . test ( stripped ) ) {
127+ if ( filter . test ( stripped ) ) {
125128 // get all tags for repo
126- const repoTags = await listTags ( stripped , creds ) ;
127- tags . push ( { name : stripped , tags : repoTags } ) ;
129+ const tags = await listTags ( stripped , creds ) ;
130+ const name = stripped . replace ( accountIdPrefix , "" ) ;
131+ responses . push ( { name, tags } ) ;
128132 }
129133 }
130134
131- await ListTags ( tags , false , args . json ) ;
135+ await ListTags ( responses , false , args . json ) ;
132136 } ) ,
133137 { message : "Listing" , json : args . json }
134138 ) ;
@@ -157,15 +161,21 @@ async function ListTags(
157161 if ( json ) {
158162 logger . log ( JSON . stringify ( responses , null , 2 ) ) ;
159163 } else {
160- const rows = responses
161- . map ( ( resp ) => {
162- return {
163- REPOSITORY : resp . name ,
164- TAG : resp . tags . join ( " " ) ,
165- } ;
166- } )
167- . flat ( ) ;
168- logger . table ( rows ) ;
164+ const rows = responses . flatMap ( ( r ) => r . tags . map ( ( t ) => [ r . name , t ] ) ) ;
165+ const headers = [ "REPOSITORY" , "TAG" ] ;
166+ const widths = new Array ( headers . length ) . fill ( 0 ) ;
167+
168+ // Find the maximum length of each column (except for the last)
169+ for ( let i = 0 ; i < widths . length - 1 ; i ++ ) {
170+ widths [ i ] = rows
171+ . map ( ( r ) => r [ i ] . length )
172+ . reduce ( ( a , b ) => Math . max ( a , b ) , headers [ i ] . length ) ;
173+ }
174+
175+ logger . log ( headers . map ( ( h , i ) => h . padEnd ( widths [ i ] , " " ) ) . join ( " " ) ) ;
176+ for ( const row of rows ) {
177+ logger . log ( row . map ( ( v , i ) => v . padEnd ( widths [ i ] , " " ) ) . join ( " " ) ) ;
178+ }
169179 }
170180}
171181
0 commit comments