From fcc3efbc8015848a124b12e34e0c0297ca6ece1d Mon Sep 17 00:00:00 2001 From: Ben Keil Date: Sat, 10 Mar 2018 21:42:16 +0100 Subject: [PATCH] - renamed helper.go to imageutil.go - image property in hydra.yaml now supports environment variables --- cmd/cmd-build.go | 14 +++++++------- cmd/cmd-push.go | 12 ++++++------ cmd/helper.go | 31 ------------------------------- cmd/imageutil.go | 32 ++++++++++++++++++++++++++++++++ examples/nginx-base/hydra.yaml | 7 ++----- 5 files changed, 47 insertions(+), 49 deletions(-) delete mode 100644 cmd/helper.go create mode 100644 cmd/imageutil.go diff --git a/cmd/cmd-build.go b/cmd/cmd-build.go index 0cb7112..8d440c7 100644 --- a/cmd/cmd-build.go +++ b/cmd/cmd-build.go @@ -17,11 +17,11 @@ import ( ) type buildCmd struct { - version string - out io.Writer - workdir string - imageHelper ImageHelper - push bool + version string + out io.Writer + workdir string + imageUtil ImageUtil + push bool } // BuildResponse contains the response from the docker client for "docker build" @@ -30,7 +30,7 @@ type BuildResponse struct { } func newBuildCmd(out io.Writer, workdir string) *cobra.Command { - c := &buildCmd{out: out, workdir: workdir, imageHelper: NewDefaultImageHelper()} + c := &buildCmd{out: out, workdir: workdir, imageUtil: NewDefaultImageUtil()} cmd := &cobra.Command{ Use: "build VERSION", @@ -94,7 +94,7 @@ func (c *buildCmd) run() error { // Build image buildResponse, err := cli.ImageBuild(context.Background(), tarfile, types.ImageBuildOptions{ PullParent: true, - Tags: c.imageHelper.getImageTags(config, tags), + Tags: c.imageUtil.getImageTags(config, tags), Context: tarfile, Dockerfile: dockerfile, }) diff --git a/cmd/cmd-push.go b/cmd/cmd-push.go index 61adec4..bee8f5d 100644 --- a/cmd/cmd-push.go +++ b/cmd/cmd-push.go @@ -16,10 +16,10 @@ import ( ) type pushCmd struct { - version string - out io.Writer - workdir string - imageHelper ImageHelper + version string + out io.Writer + workdir string + imageUtil ImageUtil } // PushResponse contains the response from the docker client for "docker build" @@ -29,7 +29,7 @@ type PushResponse struct { } func newPushCmd(out io.Writer, workdir string) *cobra.Command { - c := &pushCmd{out: out, workdir: workdir, imageHelper: NewDefaultImageHelper()} + c := &pushCmd{out: out, workdir: workdir, imageUtil: NewDefaultImageUtil()} cmd := &cobra.Command{ Use: "push VERSION", @@ -70,7 +70,7 @@ func (c *pushCmd) run() error { // Build image logger.Debugf("response from docker daemon:") - for _, image := range c.imageHelper.getImageTags(config, tags) { + for _, image := range c.imageUtil.getImageTags(config, tags) { fmt.Printf("push %s\n", image) pushResponse, err := cli.ImagePush(context.Background(), image, types.ImagePushOptions{ RegistryAuth: "hydra", diff --git a/cmd/helper.go b/cmd/helper.go deleted file mode 100644 index 6ab044f..0000000 --- a/cmd/helper.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "fmt" -) - -// ImageHelper provides some helper methods -type ImageHelper interface { - getImageTags(config Config, tags []string) []string -} - -// DefaultImageHelper is the default implementation for the ImageHelper interface -type DefaultImageHelper struct { -} - -// NewDefaultImageHelper creates a new instance of DefaultImageHelper -func NewDefaultImageHelper() *DefaultImageHelper { - return new(DefaultImageHelper) -} - -// getImageTags returns the complete image name (including registry url, image name and tag) -func (helper *DefaultImageHelper) getImageTags(config Config, tags []string) []string { - // Define all images we want to build - imageTags := []string{} - for _, image := range config.Image { - for _, tag := range tags { - imageTags = append(imageTags, fmt.Sprintf("%s:%s", image, tag)) - } - } - return imageTags -} diff --git a/cmd/imageutil.go b/cmd/imageutil.go new file mode 100644 index 0000000..961ec47 --- /dev/null +++ b/cmd/imageutil.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" +) + +// ImageUtil provides some helper methods +type ImageUtil interface { + getImageTags(config Config, tags []string) []string +} + +// DefaultImageUtil is the default implementation for the ImageUtil interface +type DefaultImageUtil struct { +} + +// NewDefaultImageUtil creates a new instance of DefaultImageUtil +func NewDefaultImageUtil() *DefaultImageUtil { + return new(DefaultImageUtil) +} + +// getImageTags returns the complete image name (including registry url, image name and tag) +func (helper *DefaultImageUtil) getImageTags(config Config, tags []string) []string { + // Define all images we want to build + imageTags := []string{} + for _, image := range config.Image { + for _, tag := range tags { + imageTags = append(imageTags, fmt.Sprintf("%s:%s", os.ExpandEnv(image), tag)) + } + } + return imageTags +} diff --git a/examples/nginx-base/hydra.yaml b/examples/nginx-base/hydra.yaml index ac3f0c5..3468113 100644 --- a/examples/nginx-base/hydra.yaml +++ b/examples/nginx-base/hydra.yaml @@ -1,10 +1,7 @@ image: -- my.private.registry:5000/docker-common/nginx-base +- ${DOCKER_REGISTRY}/docker-common/nginx-base versions: -- directory: . - args: - dockerfile: - tags: +- tags: - semver-nginx - semver - nginx