-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds command for scanning for deployment files
- Loading branch information
Showing
7 changed files
with
284 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
overrides: | ||
- app: jormungandr | ||
path: exporter.image.tag | ||
value: GITHUB_SHA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/afero" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Filename is the static name of a deployment configuration file. | ||
const Filename = "deployment.yml" | ||
|
||
// DeploymentFile represents a deployment configuration file. | ||
type DeploymentFile struct { | ||
Overrides []OverrideConfig `json:"overrides" yaml:"overrides"` | ||
} | ||
|
||
// OverrideConfig represents configuration for overriding a value in a CUE file. | ||
type OverrideConfig struct { | ||
App string `json:"app" yaml:"app"` | ||
Path string `json:"path" yaml:"path"` | ||
Value string `json:"value" yaml:"value"` | ||
} | ||
|
||
// ScanForDeploymentFiles scans a directory for deployment configuration files. | ||
func ScanForDeploymentFiles(dir string, fs afero.Fs) ([]DeploymentFile, error) { | ||
|
||
files := []DeploymentFile{} | ||
err := afero.Walk(fs, dir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
if info.Name() == Filename { | ||
contents, err := afero.ReadFile(fs, path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read deployment file at %q: %v", path, err) | ||
} | ||
|
||
deploymentFile := DeploymentFile{} | ||
if err := yaml.Unmarshal(contents, &deploymentFile); err != nil { | ||
// If we can't unmarshal the file, we assume it's not a deployment file and just log a warning. | ||
fmt.Fprintf(os.Stderr, "warning: failed to parse deployment file %q: %v", path, err) | ||
return nil | ||
} | ||
|
||
files = append(files, deploymentFile) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return files, nil | ||
} | ||
|
||
// ApplyTemplateValue applies a template value to a list of overrides. | ||
func ApplyTemplateValue(overrides []OverrideConfig, key, value string) { | ||
for i, override := range overrides { | ||
if override.Value == key { | ||
overrides[i].Value = value | ||
} | ||
} | ||
} |
Oops, something went wrong.