-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement apply and request for github
- Loading branch information
1 parent
502a0fc
commit df5141a
Showing
13 changed files
with
424 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package internal | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Change struct { | ||
RegistryName string | ||
ResourceName string | ||
OldVersion string | ||
NewVersion string | ||
File string | ||
Trace yamlTrace | ||
OldValue string | ||
NewValue string | ||
} | ||
|
||
type Changes []Change | ||
|
||
func (c Change) Identifier() string { | ||
return c.File + "#" + c.Trace.ToString() + "#" + c.NewValue | ||
} | ||
|
||
func (c Change) Hash() []byte { | ||
identifier := c.Identifier() | ||
hash := sha256.Sum256([]byte(identifier)) | ||
return hash[:] | ||
} | ||
|
||
func (cs Changes) Hash() []byte { | ||
temp := []byte{} | ||
for _, c := range cs { | ||
temp = append(temp, c.Hash()...) | ||
} | ||
hash := sha256.Sum256(temp) | ||
return hash[:] | ||
} | ||
|
||
func (cs Changes) Branch(prefix string) string { | ||
return prefix + "/" + hex.EncodeToString(cs.Hash()[:8]) | ||
} | ||
|
||
func (c Change) Message() string { | ||
return fmt.Sprintf("Update %s:%s from %s to %s", c.File, c.Trace.ToString(), c.OldValue, c.NewValue) | ||
} | ||
|
||
func (cs Changes) Message() string { | ||
lines := []string{} | ||
if len(cs) > 1 { | ||
lines = append(lines, "Update", "") | ||
} | ||
for _, c := range cs { | ||
lines = append(lines, c.Message()) | ||
} | ||
return strings.Join(lines, "\n") | ||
} | ||
|
||
func (c Change) Apply(dir string, fileHooks ...func(file string) error) error { | ||
file := filepath.Join(dir, c.File) | ||
doc := yaml.Node{} | ||
err := fileReadYaml(file, &doc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
VisitYaml(&doc, func(trace yamlTrace, node *yaml.Node) error { | ||
if trace.Equal(c.Trace) { | ||
node.Value = c.NewValue | ||
} | ||
return nil | ||
}) | ||
|
||
err = fileWriteYaml(file, &doc) | ||
if err != nil { | ||
return err | ||
} | ||
for _, hook := range fileHooks { | ||
err := hook(c.File) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (cs Changes) Apply(dir string, fileHooks ...func(file string) error) error { | ||
for _, c := range cs { | ||
err := c.Apply(dir, fileHooks...) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package internal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestChangeIdentifier(t *testing.T) { | ||
c := Change{ | ||
RegistryName: "my-registry", | ||
ResourceName: "my-resource", | ||
OldVersion: "1.0.0", | ||
NewVersion: "2.0.0", | ||
OldValue: "my-image:1.0.0", | ||
NewValue: "my-image:2.0.0", | ||
File: "folder/file.yaml", | ||
Trace: []interface{}{"spec", 0, "image"}, | ||
} | ||
assert.Equal(t, "folder/file.yaml#spec.0.image#my-image:2.0.0", c.Identifier()) | ||
} | ||
|
||
func TestChangesBranch(t *testing.T) { | ||
cs := Changes{Change{ | ||
RegistryName: "my-registry", | ||
ResourceName: "my-resource", | ||
OldVersion: "1.0.0", | ||
NewVersion: "2.0.0", | ||
OldValue: "my-image:1.0.0", | ||
NewValue: "my-image:2.0.0", | ||
File: "folder/file.yaml", | ||
Trace: []interface{}{"spec", 0, "image"}, | ||
}} | ||
assert.Equal(t, "git-ops-update/b192fbd55a666b30", cs.Branch("git-ops-update")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.