@@ -2,47 +2,30 @@ import fs from "fs";
22import path from "path" ;
33import { AuthConfig } from "dockerode" ;
44
5- type DockerConfigAuths = Record < string , { auth ?: string } > ;
6-
7- export interface DockerConfigFile {
8- auths ?: DockerConfigAuths ;
9- }
5+ export async function loadAuthConfig ( ) : Promise < AuthConfig | undefined > {
6+ const configDir = process . env . DOCKER_CONFIG ?? path . join ( process . env . HOME ?? "~" , ".docker" ) ;
7+ const configPath = path . join ( configDir , "config.json" ) ;
108
11- function extractRegistry ( image : string ) : string {
12- const parts = image . split ( "/" ) ;
13- const first = parts [ 0 ] ;
14- if ( parts . length >= 2 && first && ( first . includes ( "." ) || first . includes ( ":" ) ) ) {
15- return first ;
9+ let content : string ;
10+ try {
11+ content = await fs . promises . readFile ( configPath , "utf-8" ) ;
12+ } catch {
13+ return undefined ;
1614 }
17- return "https://index.docker.io/v1/" ;
18- }
19-
20- export function getAuthForImage ( image : string , dockerConfig : DockerConfigFile ) : AuthConfig | undefined {
21- if ( ! dockerConfig . auths ) return undefined ;
2215
23- const registry = extractRegistry ( image ) ;
24- const entry = dockerConfig . auths [ registry ] ;
25- if ( ! entry ?. auth ) return undefined ;
16+ const config = JSON . parse ( content ) as { auths ?: Record < string , { auth ?: string } > } ;
17+ const firstEntry = Object . entries ( config . auths ?? { } ) . find ( ( [ , v ] ) => v . auth ) ;
18+ if ( ! firstEntry ) return undefined ;
2619
20+ const [ serveraddress , entry ] = firstEntry ;
21+ if ( ! entry . auth ) return undefined ;
2722 const decoded = Buffer . from ( entry . auth , "base64" ) . toString ( "utf-8" ) ;
2823 const separatorIndex = decoded . indexOf ( ":" ) ;
2924 if ( separatorIndex === - 1 ) return undefined ;
3025
3126 return {
3227 username : decoded . substring ( 0 , separatorIndex ) ,
3328 password : decoded . substring ( separatorIndex + 1 ) ,
34- serveraddress : registry ,
29+ serveraddress,
3530 } ;
3631}
37-
38- export async function loadDockerConfig ( ) : Promise < DockerConfigFile > {
39- const configDir = process . env . DOCKER_CONFIG ?? path . join ( process . env . HOME ?? "~" , ".docker" ) ;
40- const configPath = path . join ( configDir , "config.json" ) ;
41-
42- try {
43- const content = await fs . promises . readFile ( configPath , "utf-8" ) ;
44- return JSON . parse ( content ) as DockerConfigFile ;
45- } catch {
46- return { } ;
47- }
48- }
0 commit comments