@@ -14,18 +14,31 @@ import (
1414 "github.com/gitpod-io/leeway/pkg/leeway/cache/local"
1515)
1616
17+ const (
18+ // ExitChangedUnchanged indicates the package is cached and does not need a rebuild.
19+ ExitChangedUnchanged = 0
20+ // ExitChangedNeedsRebuild indicates the package has changed and needs a rebuild.
21+ ExitChangedNeedsRebuild = 1
22+ // ExitChangedError indicates an error occurred while checking.
23+ ExitChangedError = 2
24+ )
25+
1726// describeChangedCmd checks whether a package has changed by looking up its
1827// current version hash in the local and remote caches. If the version exists
1928// in either cache the package is unchanged (exit 0); otherwise it has changed
20- // and needs a rebuild (exit 1).
29+ // and needs a rebuild (exit 1). Errors exit with code 2.
2130var describeChangedCmd = & cobra.Command {
2231 Use : "changed <package>" ,
2332 Short : "Checks whether a package needs to be rebuilt by consulting the cache" ,
2433 Long : `Computes the version hash of a package and checks whether a build artifact
2534for that version already exists in the local or remote cache.
2635
27- Exits with code 0 if the package is cached (unchanged), or code 1 if it is not
28- (changed / needs rebuild). This is useful for CI branching decisions:
36+ Exit codes:
37+ 0 - package is cached (unchanged, no rebuild needed)
38+ 1 - package is not cached (changed, needs rebuild)
39+ 2 - an error occurred
40+
41+ This is useful for CI branching decisions:
2942
3043 if leeway describe changed my-component:my-package; then
3144 echo "unchanged, skipping build"
@@ -37,15 +50,17 @@ Exits with code 0 if the package is cached (unchanged), or code 1 if it is not
3750 Run : func (cmd * cobra.Command , args []string ) {
3851 _ , pkg , _ , exists := getTarget (args , false )
3952 if ! exists {
40- return
53+ os . Exit ( ExitChangedError )
4154 }
4255 if pkg == nil {
43- log .Fatal ("changed requires a package, not a component" )
56+ log .Error ("changed requires a package, not a component" )
57+ os .Exit (ExitChangedError )
4458 }
4559
4660 version , err := pkg .Version ()
4761 if err != nil {
48- log .WithError (err ).Fatal ("cannot compute package version" )
62+ log .WithError (err ).Error ("cannot compute package version" )
63+ os .Exit (ExitChangedError )
4964 }
5065
5166 // Check local cache
@@ -55,30 +70,30 @@ Exits with code 0 if the package is cached (unchanged), or code 1 if it is not
5570 }
5671 localCache , err := local .NewFilesystemCache (localCacheLoc )
5772 if err != nil {
58- log .WithError (err ).Fatal ("cannot set up local cache" )
73+ log .WithError (err ).Error ("cannot set up local cache" )
74+ os .Exit (ExitChangedError )
5975 }
6076
6177 if _ , found := localCache .Location (pkg ); found {
6278 fmt .Printf ("%s\t %s\t cached locally\n " , pkg .FullName (), version )
63- os .Exit (0 )
79+ os .Exit (ExitChangedUnchanged )
6480 }
6581
6682 // Check remote cache
6783 remoteCache := getRemoteCacheFromEnv ()
6884 remote , err := remoteCache .ExistingPackages (context .Background (), []cache.Package {pkg })
6985 if err != nil {
70- log .WithError (err ).Warn ("cannot check remote cache, assuming changed" )
71- fmt .Printf ("%s\t %s\t changed\n " , pkg .FullName (), version )
72- os .Exit (1 )
86+ log .WithError (err ).Error ("cannot check remote cache" )
87+ os .Exit (ExitChangedError )
7388 }
7489
7590 if _ , found := remote [pkg ]; found {
7691 fmt .Printf ("%s\t %s\t cached remotely\n " , pkg .FullName (), version )
77- os .Exit (0 )
92+ os .Exit (ExitChangedUnchanged )
7893 }
7994
8095 fmt .Printf ("%s\t %s\t changed\n " , pkg .FullName (), version )
81- os .Exit (1 )
96+ os .Exit (ExitChangedNeedsRebuild )
8297 },
8398}
8499
0 commit comments