@@ -27,6 +27,7 @@ type DiffOption struct {
2727 Qualifier * string `help:"the qualifier to compare"`
2828 FunctionURL string `help:"path to function-url definition" default:"" env:"LAMBROLL_FUNCTION_URL"`
2929 Ignore string `help:"ignore diff by jq query" default:""`
30+ ExitCode bool `help:"exit with code 2 if there are differences" default:"false"`
3031
3132 ZipOption
3233}
@@ -93,6 +94,7 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
9394 remoteJSON , _ := marshalAny (remoteFunc )
9495 newJSON , _ := marshalAny (newFunc )
9596 remoteArn := fullQualifiedFunctionName (app .functionArn (ctx , name ), opt .Qualifier )
97+ hasDiff := false
9698
9799 if diff , err := jsondiff .Diff (
98100 & jsondiff.Input {Name : remoteArn , X : remoteJSON },
@@ -101,6 +103,7 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
101103 ); err != nil {
102104 return fmt .Errorf ("failed to diff: %w" , err )
103105 } else if diff != "" {
106+ hasDiff = true
104107 fmt .Print (coloredDiff (diff ))
105108 }
106109
@@ -126,25 +129,33 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error {
126129 fmt .Println (color .RedString ("---" + app .functionArn (ctx , name )))
127130 fmt .Println (color .GreenString ("+++" + "--src=" + opt .Src ))
128131 fmt .Println (coloredDiff (ds ))
132+ hasDiff = true
129133 }
130134 }
131135
132- if opt .FunctionURL == "" {
133- return nil
136+ if opt .FunctionURL != "" {
137+ if d , err := app .diffFunctionURL (ctx , name , opt ); err != nil {
138+ return err
139+ } else if d {
140+ hasDiff = true
141+ }
134142 }
135143
136- if err := app .diffFunctionURL (ctx , name , opt ); err != nil {
137- return err
144+ if hasDiff && opt .ExitCode {
145+ // exit with code 2 if there are differences
146+ // but actually, it's not an error
147+ return ErrDiff
138148 }
139149 return nil
140150}
141151
142- func (app * App ) diffFunctionURL (ctx context.Context , name string , opt * DiffOption ) error {
152+ func (app * App ) diffFunctionURL (ctx context.Context , name string , opt * DiffOption ) ( bool , error ) {
143153 var remote , local * types.FunctionUrlConfig
154+ var hasDiff bool
144155
145156 fu , err := app .loadFunctionUrl (opt .FunctionURL , name )
146157 if err != nil {
147- return fmt .Errorf ("failed to load function-url: %w" , err )
158+ return hasDiff , fmt .Errorf ("failed to load function-url: %w" , err )
148159 } else {
149160 fillDefaultValuesFunctionUrlConfig (fu .Config )
150161 local = & types.FunctionUrlConfig {
@@ -170,7 +181,7 @@ func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOptio
170181 // empty
171182 remote = & types.FunctionUrlConfig {}
172183 } else {
173- return fmt .Errorf ("failed to get function url config: %w" , err )
184+ return hasDiff , fmt .Errorf ("failed to get function url config: %w" , err )
174185 }
175186 } else {
176187 log .Println ("[debug] FunctionUrlConfig found" )
@@ -187,15 +198,16 @@ func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOptio
187198 & jsondiff.Input {Name : fqName , X : r },
188199 & jsondiff.Input {Name : opt .FunctionURL , X : l },
189200 ); err != nil {
190- return fmt .Errorf ("failed to diff: %w" , err )
201+ return hasDiff , fmt .Errorf ("failed to diff: %w" , err )
191202 } else if diff != "" {
203+ hasDiff = true
192204 fmt .Print (coloredDiff (diff ))
193205 }
194206
195207 // permissions
196208 adds , removes , err := app .calcFunctionURLPermissionsDiff (ctx , fu )
197209 if err != nil {
198- return err
210+ return hasDiff , err
199211 }
200212 var addsB []byte
201213 for _ , in := range adds {
@@ -211,9 +223,10 @@ func (app *App) diffFunctionURL(ctx context.Context, name string, opt *DiffOptio
211223 fmt .Println (color .RedString ("--- permissions" ))
212224 fmt .Println (color .GreenString ("+++ permissions" ))
213225 fmt .Print (coloredDiff (ds ))
226+ hasDiff = true
214227 }
215228
216- return nil
229+ return hasDiff , nil
217230}
218231
219232func coloredDiff (src string ) string {
0 commit comments