Skip to content

Commit

Permalink
[WIP] pkg/utils: Make use of configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryMichal committed Jul 8, 2021
1 parent f1ae87d commit 0df5933
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/docker/go-units"
"github.com/godbus/dbus/v5"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -133,6 +134,9 @@ func init() {
}

ContainerNameDefault = ContainerNamePrefixDefault + "-" + releaseDefault

viper.SetDefault("general.default_distro", distroDefault)
viper.SetDefault("general.default_release", releaseDefault)
}

func AskForConfirmation(prompt string) bool {
Expand Down Expand Up @@ -288,7 +292,7 @@ func GetContainerNamePrefixForImage(image string) (string, error) {

func GetDefaultImageForDistro(distro, release string) string {
if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distro = "fedora"
distro = viper.GetString("general.default_distro")
}

distroObj, supportedDistro := supportedDistros[distro]
Expand Down Expand Up @@ -577,11 +581,11 @@ func ShortID(id string) string {

func ParseRelease(distro, str string) (string, error) {
if distro == "" {
distro = distroDefault
distro = viper.GetString("general.default_distro")
}

if _, supportedDistro := supportedDistros[distro]; !supportedDistro {
distro = "fedora"
distro = viper.GetString("general.default_distro")
}

distroObj, supportedDistro := supportedDistros[distro]
Expand Down Expand Up @@ -727,31 +731,39 @@ func ResolveImageName(distro, image, release string) (string, string, error) {
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)

defer func() {
logrus.Debug("Resolved image name")
logrus.Debugf("Distribution: '%s'", distro)
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)
}()

if viper.IsSet("general.default_image") {
image = viper.GetString("general.default_image")

}

if distro == "" {
distro = distroDefault
distro = viper.GetString("general.default_distro")
}

if distro != distroDefault && release == "" {
return "", "", fmt.Errorf("release not found for non-default distribution %s", distro)
}

if release == "" {
release = releaseDefault
release = viper.GetString("general.default_release")
}

if image == "" {
image = GetDefaultImageForDistro(distro, release)
} else {
release = ImageReferenceGetTag(image)
if release == "" {
release = releaseDefault
release = viper.GetString("general.default_release")
}
}

logrus.Debug("Resolved container and image names")
logrus.Debugf("Image: '%s'", image)
logrus.Debugf("Release: '%s'", release)

return image, release, nil
}

Expand Down

0 comments on commit 0df5933

Please sign in to comment.