Skip to content

Commit

Permalink
Fix changeTag pattern; fix file name for writing
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Dec 22, 2020
1 parent bab75ab commit 137745b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Request struct {

// helper function to change tag in provided string (yaml content)
func changeTag(s string, r Request) string {
pat := fmt.Sprintf("image: %s:.*", r.Repository)
pat := fmt.Sprintf("image: %s.*", r.Repository)
re := regexp.MustCompile(pat)
img := fmt.Sprintf("image: %s:%s", r.Repository, r.Tag)
return re.ReplaceAllString(s, img)
Expand All @@ -48,7 +48,8 @@ func exeRequest(r Request) error {
log.Println("NEW YAML", content)

// write new yml file
fname := fmt.Sprintf("/tmp/%s-%s-%s-%s.yaml", r.Repository, r.Service, r.Namespace, r.Tag)
fname := fmt.Sprintf("/tmp/%s-%s-%s.yaml", r.Service, r.Namespace, r.Tag)
log.Println("fname", fname)
err = ioutil.WriteFile(fname, []byte(content), 0777)
if err != nil {
return err
Expand Down
12 changes: 10 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ import (
func TestChangeTag(t *testing.T) {
vals := []string{"bla-bla", "image: repo/srv:tag", "goo-goo"}
r := Request{Service: "srv", Namespace: "test", Tag: "123", Repository: "repo/srv"}
// fmt.Println("input", strings.Join(vals, "\n"))
res := changeTag(strings.Join(vals, "\n"), r)
// fmt.Println("output", res)
if !strings.Contains(res, "123") {
t.Errorf("Fail TestChangeTag, %s\n", res)
}
}

// TestChangeTagNoTag
func TestChangeTagNoTag(t *testing.T) {
vals := []string{"bla-bla", "image: repo/srv", "goo-goo"}
r := Request{Service: "srv", Namespace: "test", Tag: "123", Repository: "repo/srv"}
res := changeTag(strings.Join(vals, "\n"), r)
if !strings.Contains(res, "123") {
t.Errorf("Fail TestChangeTagNoTag, %s\n", res)
}
}

// TestToken
func TestToken(t *testing.T) {
r := Request{Service: "srv", Namespace: "test", Tag: "123", Repository: "repo/srv"}
Expand Down

0 comments on commit 137745b

Please sign in to comment.