Skip to content

Commit

Permalink
Merge pull request #35 from depot/feat/project-helpers
Browse files Browse the repository at this point in the history
feat: add project id helper
  • Loading branch information
goller authored May 22, 2024
2 parents dea53b5 + a4af5ed commit 7ebdf18
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/spf13/viper v1.18.2
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
121 changes: 121 additions & 0 deletions project/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package project

import (
"fmt"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)

// Returns the project ID from the environment or config file.
// Searches from the directory of each of the files.
func ResolveProjectID(id string, files ...string) string {
if id != "" {
return id
}

id = os.Getenv("DEPOT_PROJECT_ID")
if id != "" {
return id
}

dirs, err := WorkingDirectories(files...)
if err != nil {
return ""
}

// Only a single project ID is allowed.
uniqueIDs := make(map[string]struct{})

for _, dir := range dirs {
cwd, _ := filepath.Abs(dir)
config, _, err := ReadConfig(cwd)
if err == nil {
id = config.ID
uniqueIDs[id] = struct{}{}
}
}

return id
}

// Returns all directories for any files. If no files are specified then
// the current working directory is returned. Special handling for stdin
// is also included by assuming the current working directory.
func WorkingDirectories(files ...string) ([]string, error) {
directories := []string{}
if len(files) == 0 {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
directories = append(directories, cwd)
}

for _, file := range files {
if file == "-" || file == "" {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
directories = append(directories, cwd)
continue
}

if fi, err := os.Stat(file); err == nil && fi.IsDir() {
directories = append(directories, file)
} else {
directories = append(directories, filepath.Dir(file))
}
}

return directories, nil
}

type ProjectConfig struct {
ID string `json:"id" yaml:"id"`
}

func ReadConfig(cwd string) (*ProjectConfig, string, error) {
filename, err := FindConfigFileUp(cwd)
if err != nil {
return nil, "", err
}

data, err := os.ReadFile(filename)
if err != nil {
return nil, "", err
}

var config ProjectConfig
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, "", err
}

return &config, filename, nil
}

func FindConfigFileUp(current string) (string, error) {
for {
path := filepath.Join(current, "depot.json")
if _, err := os.Stat(path); err == nil {
return path, nil
}
path = filepath.Join(current, "depot.yml")
if _, err := os.Stat(path); err == nil {
return path, nil
}
path = filepath.Join(current, "depot.yaml")
if _, err := os.Stat(path); err == nil {
return path, nil
}
next := filepath.Dir(current)
if next == current {
break
}
current = next
}
return "", fmt.Errorf("no project config found")
}

0 comments on commit 7ebdf18

Please sign in to comment.