Skip to content

Commit 60f3625

Browse files
committed
Discover workspace root
1 parent 2598b13 commit 60f3625

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

cmd/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,14 @@ func Execute() {
131131
func init() {
132132
workspaceRoot := os.Getenv(EnvvarWorkspaceRoot)
133133
if workspaceRoot == "" {
134-
workspaceRoot = "."
134+
var err error
135+
workspaceRoot, err = leeway.DiscoverWorkspaceRoot()
136+
if err != nil {
137+
log.WithError(err).Debug("cannot determine workspace root - defaulting to .")
138+
workspaceRoot = "."
139+
} else {
140+
log.WithField("workspace", workspaceRoot).Debug("found workspace root")
141+
}
135142
}
136143

137144
rootCmd.PersistentFlags().StringVarP(&workspace, "workspace", "w", workspaceRoot, "Workspace root")

pkg/leeway/workspace.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ type WorkspaceProvenance struct {
5858
key *in_toto.Key `yaml:"-"`
5959
}
6060

61+
func DiscoverWorkspaceRoot() (string, error) {
62+
wd, err := os.Getwd()
63+
if err != nil {
64+
return "", err
65+
}
66+
67+
for i := 0; i < 100; i++ {
68+
if _, err := os.Stat(filepath.Join(wd, "WORKSPACE.yaml")); err == nil {
69+
return wd, nil
70+
}
71+
72+
wd = filepath.Dir(wd)
73+
if wd == "/" || wd == "" {
74+
break
75+
}
76+
}
77+
78+
return "", xerrors.Errorf("cannot find workspace root")
79+
}
80+
6181
// EnvironmentManifest is a collection of environment manifest entries
6282
type EnvironmentManifest []EnvironmentManifestEntry
6383

0 commit comments

Comments
 (0)