Skip to content

Commit

Permalink
set commit hash in AppConfig (#155)
Browse files Browse the repository at this point in the history
Signed-off-by: Benji Visser <[email protected]>
  • Loading branch information
noqcks authored Aug 28, 2023
1 parent 8e4fb17 commit 777e3c4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Application struct {
FailOnEolFound bool `yaml:"fail-on-eol-found" json:"fail-on-eol-found" mapstructure:"fail-on-eol-found"` // whether to exit with a non-zero exit code if any EOLs are found
APIKey string `yaml:"api-key" json:"api-key" mapstructure:"api-key"`
ProjectName string `yaml:"project-name" json:"project-name" mapstructure:"project-name"`
CommitHash string `yaml:"commit-hash" json:"commit-hash" mapstructure:"commit-hash"`
ImagePath string `yaml:"image-path" json:"image-path" mapstructure:"image-path"`
Registry registry `yaml:"registry" json:"registry" mapstructure:"registry"`
Platform string `yaml:"platform" json:"platform" mapstructure:"platform"` // --platform, override the target platform for a container image
Expand Down Expand Up @@ -89,7 +90,9 @@ func (cfg Application) loadDefaultValues(v *viper.Viper) {
// set the default values for primitive fields in this struct
v.SetDefault("check-for-app-update", true)
v.SetDefault("fail-on-eol-found", false)
v.SetDefault("project-name", getDefaultProjectName())
project, commit := getDefaultProjectNameAndCommit()
v.SetDefault("project-name", project)
v.SetDefault("commit-hash", commit)
v.SetDefault("image-path", "Dockerfile")
v.SetDefault("default-image-pull-source", "")

Expand All @@ -104,14 +107,19 @@ func (cfg Application) loadDefaultValues(v *viper.Viper) {
}
}

func getDefaultProjectName() string {
func getDefaultProjectNameAndCommit() (string, string) {
repo, err := git.PlainOpen(".")
if err != nil {
return ""
return "", ""
}

h, err := repo.Head()
if err != nil {
return "", ""
}

p := NewProject(repo)
return p.Name
return p.Name, h.Hash().String()
}

// readConfig attempts to read the given config path from disk or discover an alternate store location
Expand Down

0 comments on commit 777e3c4

Please sign in to comment.