@@ -19,26 +19,43 @@ import {Exec} from '../exec';
19
19
import { Inputs } from './inputs' ;
20
20
import { Util } from '../util' ;
21
21
22
+ import { ExecOptions } from '@actions/exec' ;
22
23
import { BakeDefinition } from '../types/bake' ;
23
24
24
25
export interface BakeOpts {
25
26
buildx ?: Buildx ;
26
27
}
27
28
29
+ export interface BakeCmdOpts {
30
+ files ?: Array < string > ;
31
+ load ?: boolean ;
32
+ noCache ?: boolean ;
33
+ overrides ?: Array < string > ;
34
+ provenance ?: string ;
35
+ push ?: boolean ;
36
+ sbom ?: string ;
37
+ source ?: string ;
38
+ targets ?: Array < string > ;
39
+ }
40
+
28
41
export class Bake {
29
42
private readonly buildx : Buildx ;
30
43
31
44
constructor ( opts ?: BakeOpts ) {
32
45
this . buildx = opts ?. buildx || new Buildx ( ) ;
33
46
}
34
47
35
- public async parseDefinitions ( sources : Array < string > , targets ?: Array < string > , overrides ?: Array < string > , load ?: boolean , push ?: boolean , workdir ?: string ) : Promise < BakeDefinition > {
48
+ public async getDefinition ( cmdOpts : BakeCmdOpts , execOptions ?: ExecOptions ) : Promise < BakeDefinition > {
49
+ execOptions = execOptions || { ignoreReturnCode : true } ;
50
+ execOptions . ignoreReturnCode = true ;
51
+
36
52
const args = [ 'bake' ] ;
37
53
38
- let remoteDef ;
54
+ let remoteDef : string | undefined ;
39
55
const files : Array < string > = [ ] ;
56
+ const sources = [ ...( cmdOpts . files || [ ] ) , cmdOpts . source ] ;
40
57
if ( sources ) {
41
- for ( const source of sources . map ( v => v . trim ( ) ) ) {
58
+ for ( const source of sources . map ( v => ( v ? v . trim ( ) : '' ) ) ) {
42
59
if ( source . length == 0 ) {
43
60
continue ;
44
61
}
@@ -47,7 +64,7 @@ export class Bake {
47
64
continue ;
48
65
}
49
66
if ( remoteDef ) {
50
- throw new Error ( `Only one remote bake definition is allowed ` ) ;
67
+ throw new Error ( `Only one remote bake definition can be defined ` ) ;
51
68
}
52
69
remoteDef = source ;
53
70
}
@@ -58,31 +75,40 @@ export class Bake {
58
75
for ( const file of files ) {
59
76
args . push ( '--file' , file ) ;
60
77
}
61
- if ( overrides ) {
62
- for ( const override of overrides ) {
78
+ if ( cmdOpts . overrides ) {
79
+ for ( const override of cmdOpts . overrides ) {
63
80
args . push ( '--set' , override ) ;
64
81
}
65
82
}
66
- if ( load ) {
83
+ if ( cmdOpts . load ) {
67
84
args . push ( '--load' ) ;
68
85
}
69
- if ( push ) {
86
+ if ( cmdOpts . noCache ) {
87
+ args . push ( '--no-cache' ) ;
88
+ }
89
+ if ( cmdOpts . provenance ) {
90
+ args . push ( '--provenance' , cmdOpts . provenance ) ;
91
+ }
92
+ if ( cmdOpts . push ) {
70
93
args . push ( '--push' ) ;
71
94
}
95
+ if ( cmdOpts . sbom ) {
96
+ args . push ( '--sbom' , cmdOpts . sbom ) ;
97
+ }
72
98
73
- const printCmd = await this . buildx . getCommand ( [ ...args , '--print' , ...( targets || [ ] ) ] ) ;
74
- return await Exec . getExecOutput ( printCmd . command , printCmd . args , {
75
- cwd : workdir ,
76
- ignoreReturnCode : true ,
77
- silent : true
78
- } ) . then ( res => {
99
+ const printCmd = await this . buildx . getCommand ( [ ...args , '--print' , ...( cmdOpts . targets || [ ] ) ] ) ;
100
+ return await Exec . getExecOutput ( printCmd . command , printCmd . args , execOptions ) . then ( res => {
79
101
if ( res . stderr . length > 0 && res . exitCode != 0 ) {
80
102
throw new Error ( `cannot parse bake definitions: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
81
103
}
82
- return < BakeDefinition > JSON . parse ( res . stdout . trim ( ) ) ;
104
+ return Bake . parseDefinition ( res . stdout . trim ( ) ) ;
83
105
} ) ;
84
106
}
85
107
108
+ public static parseDefinition ( dt : string ) : BakeDefinition {
109
+ return < BakeDefinition > JSON . parse ( dt ) ;
110
+ }
111
+
86
112
public static hasLocalExporter ( def : BakeDefinition ) : boolean {
87
113
return Inputs . hasExporterType ( 'local' , Bake . exporters ( def ) ) ;
88
114
}
0 commit comments