-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdescribe-changed.go
More file actions
102 lines (87 loc) · 2.98 KB
/
describe-changed.go
File metadata and controls
102 lines (87 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/gitpod-io/leeway/pkg/leeway"
"github.com/gitpod-io/leeway/pkg/leeway/cache"
"github.com/gitpod-io/leeway/pkg/leeway/cache/local"
)
const (
// ExitChangedUnchanged indicates the package is cached and does not need a rebuild.
ExitChangedUnchanged = 0
// ExitChangedNeedsRebuild indicates the package has changed and needs a rebuild.
ExitChangedNeedsRebuild = 1
// ExitChangedError indicates an error occurred while checking.
ExitChangedError = 2
)
// describeChangedCmd checks whether a package has changed by looking up its
// current version hash in the local and remote caches. If the version exists
// in either cache the package is unchanged (exit 0); otherwise it has changed
// and needs a rebuild (exit 1). Errors exit with code 2.
var describeChangedCmd = &cobra.Command{
Use: "changed <package>",
Short: "Checks whether a package needs to be rebuilt by consulting the cache",
Long: `Computes the version hash of a package and checks whether a build artifact
for that version already exists in the local or remote cache.
Exit codes:
0 - package is cached (unchanged, no rebuild needed)
1 - package is not cached (changed, needs rebuild)
2 - an error occurred
This is useful for CI branching decisions:
if leeway describe changed my-component:my-package; then
echo "unchanged, skipping build"
else
echo "changed, rebuilding"
leeway build my-component:my-package
fi`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
_, pkg, _, exists := getTarget(args, false)
if !exists {
os.Exit(ExitChangedError)
}
if pkg == nil {
log.Error("changed requires a package, not a component")
os.Exit(ExitChangedError)
}
version, err := pkg.Version()
if err != nil {
log.WithError(err).Error("cannot compute package version")
os.Exit(ExitChangedError)
}
// Check local cache
localCacheLoc := os.Getenv(leeway.EnvvarCacheDir)
if localCacheLoc == "" {
localCacheLoc = filepath.Join(os.TempDir(), "leeway", "cache")
}
localCache, err := local.NewFilesystemCache(localCacheLoc)
if err != nil {
log.WithError(err).Error("cannot set up local cache")
os.Exit(ExitChangedError)
}
if _, found := localCache.Location(pkg); found {
fmt.Printf("%s\t%s\tcached locally\n", pkg.FullName(), version)
os.Exit(ExitChangedUnchanged)
}
// Check remote cache
remoteCache := getRemoteCacheFromEnv()
remote, err := remoteCache.ExistingPackages(context.Background(), []cache.Package{pkg})
if err != nil {
log.WithError(err).Error("cannot check remote cache")
os.Exit(ExitChangedError)
}
if _, found := remote[pkg]; found {
fmt.Printf("%s\t%s\tcached remotely\n", pkg.FullName(), version)
os.Exit(ExitChangedUnchanged)
}
fmt.Printf("%s\t%s\tchanged\n", pkg.FullName(), version)
os.Exit(ExitChangedNeedsRebuild)
},
}
func init() {
describeCmd.AddCommand(describeChangedCmd)
}