@@ -2,26 +2,23 @@ import { Bar as CliProgressBar, Presets as CliProgressBarPresets } from 'cli-pro
22import EasyTable from 'easy-table' ;
33import { diff as semverDiff , valid as semverValid } from 'semver' ;
44
5+ import { CredentialsLoader , CredentialsStore } from './credentials' ;
6+ import {
7+ DockerConfig ,
8+ DockerConfigCredentialsLoader ,
9+ UserInputCredentialsLoader
10+ } from './credentials-loader-implementations' ;
511import { getComposeImages } from './compose-utils' ;
612import {
7- Credentials ,
8- CredentialsStore ,
913 DOCKER_REGISTRY_HOST ,
1014 DockerImage ,
1115 getImageUpdateTags ,
1216 getLatestImageVersion ,
13- listRepositories ,
14- readDockerConfig
17+ listRepositories
1518} from './docker-utils' ;
19+ import { readJsonFile } from './utils' ;
1620
1721// only for testing purposes
18- class LoginCredentials implements Credentials {
19- constructor ( private username : string , private password : string ) { }
20-
21- public getToken ( ) : string {
22- return Buffer . from ( `${ this . username } :${ this . password } ` , 'utf8' ) . toString ( 'base64' ) ;
23- }
24- }
2522
2623async function listLatestImageVersions (
2724 registryHost : string ,
@@ -62,9 +59,64 @@ export interface OutdatedImage {
6259 latestVersion : string ;
6360}
6461
62+ class ProgressBarWrapper {
63+ private readonly progressBar : CliProgressBar ;
64+
65+ private progressInformation : { total : number ; current : number } | undefined ;
66+
67+ constructor ( ) {
68+ this . progressBar = new CliProgressBar ( { } , CliProgressBarPresets . shades_classic ) ;
69+ }
70+
71+ public start ( total : number , initialValue : number ) : void {
72+ this . progressInformation = { total, current : initialValue } ;
73+ this . progressBar . start ( total , initialValue ) ;
74+ }
75+
76+ public stop ( ) : void {
77+ this . progressBar . stop ( ) ;
78+ }
79+
80+ public pause ( ) : void {
81+ this . progressBar . stop ( ) ;
82+ }
83+
84+ public resume ( ) : void {
85+ if ( this . progressInformation == null ) throw new Error ( 'Progressbar was never started' ) ;
86+ this . progressBar . start ( this . progressInformation . total , this . progressInformation . current ) ;
87+ }
88+
89+ public increment ( inc : number ) : void {
90+ if ( this . progressInformation == null ) throw new Error ( 'Progressbar was never started' ) ;
91+ this . progressInformation = {
92+ total : this . progressInformation . total ,
93+ current : this . progressInformation . current + inc
94+ } ;
95+
96+ this . progressBar . increment ( inc ) ;
97+ }
98+ }
99+
65100export async function listOutdated ( options : Options ) : Promise < OutdatedImage [ ] > {
66- const config = await readDockerConfig ( options . dockerConfigPath ) ;
67- const credentials = new CredentialsStore ( config ) ;
101+ const progressBar = new ProgressBarWrapper ( ) ;
102+
103+ const credentialsLoaders : CredentialsLoader [ ] = [ ] ;
104+
105+ try {
106+ const config = await readJsonFile < DockerConfig > ( options . dockerConfigPath ) ;
107+ credentialsLoaders . push ( new DockerConfigCredentialsLoader ( config ) ) ;
108+ } catch ( err ) {
109+ console . error ( 'Error while trying to add credentials-loader from docker config file. Skipping...' , err ) ;
110+ }
111+
112+ credentialsLoaders . push (
113+ new UserInputCredentialsLoader ( {
114+ onPromptStart : ( ) => progressBar . pause ( ) ,
115+ onPromptStop : ( ) => progressBar . resume ( )
116+ } )
117+ ) ;
118+
119+ const credentials = new CredentialsStore ( credentialsLoaders ) ;
68120 // const res = await listLatestImageVersions('docker.solunio.com', credentials, 'common')
69121 // console.log('res: ', res);
70122
@@ -87,7 +139,6 @@ export async function listOutdated(options: Options): Promise<OutdatedImage[]> {
87139 filteredImages . push ( composeImage ) ;
88140 }
89141 const outdatedImages : OutdatedImage [ ] = [ ] ;
90- const progressBar = new CliProgressBar ( { } , CliProgressBarPresets . shades_classic ) ;
91142 progressBar . start ( filteredImages . length , 0 ) ;
92143
93144 try {
0 commit comments