|
| 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