Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the envTemplate code to make it reusable #601

Merged
merged 1 commit into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 8 additions & 27 deletions pkg/skaffold/build/tag/env_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ limitations under the License.
package tag

import (
"bytes"
"fmt"
"os"
"strings"
"text/template"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
)

Expand All @@ -33,12 +29,9 @@ type EnvTemplateTagger struct {
Template *template.Template
}

// For testing
var environ = os.Environ

// NewEnvTemplateTagger creates a new EnvTemplateTagger
func NewEnvTemplateTagger(t string) (*EnvTemplateTagger, error) {
tmpl, err := template.New("envTemplate").Parse(t)
tmpl, err := util.ParseEnvTemplate(t)
if err != nil {
return nil, errors.Wrap(err, "parsing template")
}
Expand All @@ -49,30 +42,18 @@ func NewEnvTemplateTagger(t string) (*EnvTemplateTagger, error) {

// GenerateFullyQualifiedImageName tags an image with the custom tag
func (c *EnvTemplateTagger) GenerateFullyQualifiedImageName(workingDir string, opts *TagOptions) (string, error) {
var buf bytes.Buffer
envMap := map[string]string{}
for _, env := range environ() {
kvp := strings.SplitN(env, "=", 2)
if len(kvp) != 2 {
return "", fmt.Errorf("error parsing environment variables, %s does not contain an =", kvp)
}
envMap[kvp[0]] = kvp[1]
}
customMap := map[string]string{}

envMap["IMAGE_NAME"] = opts.ImageName
customMap["IMAGE_NAME"] = opts.ImageName
digest := opts.Digest
envMap["DIGEST"] = digest
customMap["DIGEST"] = digest
if digest != "" {
names := strings.SplitN(digest, ":", 2)
if len(names) >= 2 {
envMap["DIGEST_ALGO"] = names[0]
envMap["DIGEST_HEX"] = names[1]
customMap["DIGEST_ALGO"] = names[0]
customMap["DIGEST_HEX"] = names[1]
}
}

logrus.Debugf("Executing template %v with environment %v", c.Template, envMap)
if err := c.Template.Execute(&buf, envMap); err != nil {
return "", errors.Wrap(err, "executing template")
}
return buf.String(), nil
return util.ExecuteEnvTemplate(c.Template, customMap)
}
3 changes: 2 additions & 1 deletion pkg/skaffold/build/tag/env_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"text/template"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
)

Expand Down Expand Up @@ -76,7 +77,7 @@ func TestEnvTemplateTagger_GenerateFullyQualifiedImageName(t *testing.T) {
c := &EnvTemplateTagger{
Template: template.Must(template.New("").Parse(test.template)),
}
environ = func() []string {
util.OSEnviron = func() []string {
return test.env
}

Expand Down
60 changes: 60 additions & 0 deletions pkg/skaffold/util/env_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"bytes"
"fmt"
"os"
"strings"
"text/template"

"github.com/sirupsen/logrus"

"github.com/pkg/errors"
)

var OSEnviron = os.Environ

// Simple wrapper to parse an env template
func ParseEnvTemplate(t string) (*template.Template, error) {
tmpl, err := template.New("envTemplate").Parse(t)
return tmpl, err
}

// Executes an envTemplate based on OS environment variables and a custom map
func ExecuteEnvTemplate(envTemplate *template.Template, customMap map[string]string) (string, error) {
var buf bytes.Buffer
envMap := map[string]string{}
for _, env := range OSEnviron() {
kvp := strings.SplitN(env, "=", 2)
if len(kvp) != 2 {
return "", fmt.Errorf("error parsing environment variables, %s does not contain an =", kvp)
}
envMap[kvp[0]] = kvp[1]
}

for k, v := range customMap {
envMap[k] = v
}

logrus.Debugf("Executing template %v with environment %v", envTemplate, envMap)
if err := envTemplate.Execute(&buf, envMap); err != nil {
return "", errors.Wrap(err, "executing template")
}
return buf.String(), nil
}