Skip to content

Commit a4af5ed

Browse files
committed
feat: add project id helper
This scans for existing project ID either by env var or within the directories. Signed-off-by: Chris Goller <[email protected]>
1 parent dea53b5 commit a4af5ed

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/spf13/viper v1.18.2
1111
google.golang.org/grpc v1.63.2
1212
google.golang.org/protobuf v1.33.0
13+
gopkg.in/yaml.v2 v2.4.0
1314
)
1415

1516
require (

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
340340
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
341341
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
342342
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
343+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
344+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
343345
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
344346
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
345347
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

project/project.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package project
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"gopkg.in/yaml.v2"
9+
)
10+
11+
// Returns the project ID from the environment or config file.
12+
// Searches from the directory of each of the files.
13+
func ResolveProjectID(id string, files ...string) string {
14+
if id != "" {
15+
return id
16+
}
17+
18+
id = os.Getenv("DEPOT_PROJECT_ID")
19+
if id != "" {
20+
return id
21+
}
22+
23+
dirs, err := WorkingDirectories(files...)
24+
if err != nil {
25+
return ""
26+
}
27+
28+
// Only a single project ID is allowed.
29+
uniqueIDs := make(map[string]struct{})
30+
31+
for _, dir := range dirs {
32+
cwd, _ := filepath.Abs(dir)
33+
config, _, err := ReadConfig(cwd)
34+
if err == nil {
35+
id = config.ID
36+
uniqueIDs[id] = struct{}{}
37+
}
38+
}
39+
40+
return id
41+
}
42+
43+
// Returns all directories for any files. If no files are specified then
44+
// the current working directory is returned. Special handling for stdin
45+
// is also included by assuming the current working directory.
46+
func WorkingDirectories(files ...string) ([]string, error) {
47+
directories := []string{}
48+
if len(files) == 0 {
49+
cwd, err := os.Getwd()
50+
if err != nil {
51+
return nil, err
52+
}
53+
directories = append(directories, cwd)
54+
}
55+
56+
for _, file := range files {
57+
if file == "-" || file == "" {
58+
cwd, err := os.Getwd()
59+
if err != nil {
60+
return nil, err
61+
}
62+
directories = append(directories, cwd)
63+
continue
64+
}
65+
66+
if fi, err := os.Stat(file); err == nil && fi.IsDir() {
67+
directories = append(directories, file)
68+
} else {
69+
directories = append(directories, filepath.Dir(file))
70+
}
71+
}
72+
73+
return directories, nil
74+
}
75+
76+
type ProjectConfig struct {
77+
ID string `json:"id" yaml:"id"`
78+
}
79+
80+
func ReadConfig(cwd string) (*ProjectConfig, string, error) {
81+
filename, err := FindConfigFileUp(cwd)
82+
if err != nil {
83+
return nil, "", err
84+
}
85+
86+
data, err := os.ReadFile(filename)
87+
if err != nil {
88+
return nil, "", err
89+
}
90+
91+
var config ProjectConfig
92+
err = yaml.Unmarshal(data, &config)
93+
if err != nil {
94+
return nil, "", err
95+
}
96+
97+
return &config, filename, nil
98+
}
99+
100+
func FindConfigFileUp(current string) (string, error) {
101+
for {
102+
path := filepath.Join(current, "depot.json")
103+
if _, err := os.Stat(path); err == nil {
104+
return path, nil
105+
}
106+
path = filepath.Join(current, "depot.yml")
107+
if _, err := os.Stat(path); err == nil {
108+
return path, nil
109+
}
110+
path = filepath.Join(current, "depot.yaml")
111+
if _, err := os.Stat(path); err == nil {
112+
return path, nil
113+
}
114+
next := filepath.Dir(current)
115+
if next == current {
116+
break
117+
}
118+
current = next
119+
}
120+
return "", fmt.Errorf("no project config found")
121+
}

0 commit comments

Comments
 (0)