@@ -19,26 +19,43 @@ import {Exec} from '../exec';
1919import { Inputs } from './inputs' ;
2020import { Util } from '../util' ;
2121
22+ import { ExecOptions } from '@actions/exec' ;
2223import { BakeDefinition } from '../types/bake' ;
2324
2425export interface BakeOpts {
2526 buildx ?: Buildx ;
2627}
2728
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+
2841export class Bake {
2942 private readonly buildx : Buildx ;
3043
3144 constructor ( opts ?: BakeOpts ) {
3245 this . buildx = opts ?. buildx || new Buildx ( ) ;
3346 }
3447
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+
3652 const args = [ 'bake' ] ;
3753
38- let remoteDef ;
54+ let remoteDef : string | undefined ;
3955 const files : Array < string > = [ ] ;
56+ const sources = [ ...( cmdOpts . files || [ ] ) , cmdOpts . source ] ;
4057 if ( sources ) {
41- for ( const source of sources . map ( v => v . trim ( ) ) ) {
58+ for ( const source of sources . map ( v => ( v ? v . trim ( ) : '' ) ) ) {
4259 if ( source . length == 0 ) {
4360 continue ;
4461 }
@@ -47,7 +64,7 @@ export class Bake {
4764 continue ;
4865 }
4966 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 ` ) ;
5168 }
5269 remoteDef = source ;
5370 }
@@ -58,31 +75,40 @@ export class Bake {
5875 for ( const file of files ) {
5976 args . push ( '--file' , file ) ;
6077 }
61- if ( overrides ) {
62- for ( const override of overrides ) {
78+ if ( cmdOpts . overrides ) {
79+ for ( const override of cmdOpts . overrides ) {
6380 args . push ( '--set' , override ) ;
6481 }
6582 }
66- if ( load ) {
83+ if ( cmdOpts . load ) {
6784 args . push ( '--load' ) ;
6885 }
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 ) {
7093 args . push ( '--push' ) ;
7194 }
95+ if ( cmdOpts . sbom ) {
96+ args . push ( '--sbom' , cmdOpts . sbom ) ;
97+ }
7298
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 => {
79101 if ( res . stderr . length > 0 && res . exitCode != 0 ) {
80102 throw new Error ( `cannot parse bake definitions: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
81103 }
82- return < BakeDefinition > JSON . parse ( res . stdout . trim ( ) ) ;
104+ return Bake . parseDefinition ( res . stdout . trim ( ) ) ;
83105 } ) ;
84106 }
85107
108+ public static parseDefinition ( dt : string ) : BakeDefinition {
109+ return < BakeDefinition > JSON . parse ( dt ) ;
110+ }
111+
86112 public static hasLocalExporter ( def : BakeDefinition ) : boolean {
87113 return Inputs . hasExporterType ( 'local' , Bake . exporters ( def ) ) ;
88114 }
0 commit comments