grim/convey

Update to Go 1.21 and use some new helpers

4 months ago, Elliott Sales de Andrade
adb6eb2a7d21
Parents 62cc853bf396
Children 2dc09584dc67
Update to Go 1.21 and use some new helpers

`strings.Cut` was added in 1.18, and `maps`/`slices` in 1.21.

Testing Done:
Ran `go test ./...`

Reviewed at https://reviews.imfreedom.org/r/2870/
--- a/environment/environment.go Sun Dec 10 05:12:31 2023 -0600
+++ b/environment/environment.go Sun Dec 10 05:21:38 2023 -0600
@@ -18,21 +18,12 @@
package environment
import (
+ "maps"
"strings"
)
type Environment map[string]string
-// splitEqual takes a string like "foo=bar" and returns it as ("foo", "bar")
-func splitEqual(value string) (string, string) {
- parts := strings.SplitN(value, "=", 2)
- if len(parts) == 1 {
- return parts[0], ""
- }
-
- return parts[0], parts[1]
-}
-
func New(items ...string) Environment {
r := Environment{}
@@ -41,10 +32,7 @@
func (e Environment) Copy() Environment {
r := Environment{}
-
- for k, v := range e {
- r[k] = v
- }
+ maps.Copy(r, e)
return r
}
@@ -52,9 +40,7 @@
// Merge will merge another environment into the current environment
// overwriting any existing items
func (e Environment) Merge(env Environment) Environment {
- for k, v := range env {
- e[k] = v
- }
+ maps.Copy(e, env)
return e
}
@@ -62,7 +48,7 @@
// MergeSlice will merge a slice of strings in key or key=value formats.
func (e Environment) MergeSlice(items []string) Environment {
for _, item := range items {
- k, v := splitEqual(item)
+ k, v, _ := strings.Cut(item, "=")
e[k] = v
}
--- a/environment/mapper.go Sun Dec 10 05:12:31 2023 -0600
+++ b/environment/mapper.go Sun Dec 10 05:21:38 2023 -0600
@@ -41,9 +41,9 @@
}
for _, item := range e.env {
- if parts := strings.SplitN(item, "=", 2); parts[0] == name {
- if len(parts) == 2 {
- return parts[1]
+ if varName, varValue, valueFound := strings.Cut(item, "="); varName == name {
+ if valueFound {
+ return varValue
}
return os.Getenv(name)
--- a/exec/generator.go Sun Dec 10 05:12:31 2023 -0600
+++ b/exec/generator.go Sun Dec 10 05:21:38 2023 -0600
@@ -17,7 +17,7 @@
package exec
import (
- "math"
+ "slices"
"sync"
)
@@ -28,15 +28,13 @@
// Generator represents a command generator.
type Generator struct {
args []string
- next int
lock sync.Mutex
}
// NewGenerator creates a new generator initialized with the gives arguments.
func NewGenerator(args ...string) *Generator {
g := &Generator{
- args: make([]string, minAlloc),
- next: 0,
+ args: make([]string, 0, minAlloc),
}
g.Appendv(args)
@@ -44,24 +42,6 @@
return g
}
-// resize will resize the slice to the required size or minimum allocation
-// which ever is larger.
-func (g *Generator) resize(reqSize int) {
- if g.next >= len(g.args) {
- newSize := len(g.args)
-
- if reqSize > minAlloc {
- newSize += reqSize
- } else {
- newSize += minAlloc
- }
-
- newArgs := make([]string, newSize)
- copy(newArgs, g.args)
- g.args = newArgs
- }
-}
-
// Append appends the given arguments to the end of the arguments.
func (g *Generator) Append(args ...string) {
g.Appendv(args)
@@ -76,11 +56,7 @@
return
}
- for _, item := range args {
- g.resize(1)
- g.args[g.next] = item
- g.next++
- }
+ g.args = slices.Insert(g.args, len(g.args), args...)
}
// Prepend prepends the given arguments to the beginning of the arguments.
@@ -97,27 +73,7 @@
return
}
- // allocate a new slice that's the size of the
- newSize := int(math.Max(float64(len(args)), float64(minAlloc))) + len(g.args)
- newArgs := make([]string, newSize)
-
- // set our current index to 0
- idx := 0
-
- // add the prepended items
- for ; idx < len(args); idx++ {
- newArgs[idx] = args[idx]
- }
-
- // add the original items
- offset := len(args)
- for ; idx < g.next+offset; idx++ {
- newArgs[idx] = g.args[idx-offset]
- }
-
- // now assign newArgs
- g.args = newArgs
- g.next += len(args)
+ g.args = slices.Insert(g.args, 0, args...)
}
// Command returns the currently built command.
@@ -125,10 +81,5 @@
g.lock.Lock()
defer g.lock.Unlock()
- ret := make([]string, g.next)
- for i := 0; i < g.next; i++ {
- ret[i] = g.args[i]
- }
-
- return ret
+ return slices.Clone(g.args)
}
--- a/fs/pathspec.go Sun Dec 10 05:12:31 2023 -0600
+++ b/fs/pathspec.go Sun Dec 10 05:21:38 2023 -0600
@@ -29,17 +29,13 @@
// If there is no colon, then either "." (the current working directory)
// will be returned as the destination.
func ParsePathSpec(spec string) *PathSpec {
- parts := strings.SplitN(spec, ":", 2)
+ src, dst, found := strings.Cut(spec, ":")
- dst := parts[0]
- if len(parts) > 1 {
- dst = parts[1]
+ if !found {
+ dst = src
}
- return &PathSpec{
- src: parts[0],
- dst: dst,
- }
+ return &PathSpec{src: src, dst: dst}
}
func (ps *PathSpec) Source() string {
--- a/go.mod Sun Dec 10 05:12:31 2023 -0600
+++ b/go.mod Sun Dec 10 05:21:38 2023 -0600
@@ -24,4 +24,4 @@
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
-go 1.17
+go 1.21
--- a/tasks/clean.go Sun Dec 10 05:12:31 2023 -0600
+++ b/tasks/clean.go Sun Dec 10 05:21:38 2023 -0600
@@ -53,20 +53,6 @@
return pathname, nil
}
-func sanitizeFiles(base string, files, env environment.Environment) ([]string, error) {
- list := []string{}
- for _, file := range files {
- sanitized, err := sanitizeFile(base, file, env)
- if err != nil {
- return nil, err
- }
-
- list = append(list, sanitized)
- }
-
- return list, nil
-}
-
// Execute runs the clean task.
func (c *Clean) Execute(name string, logger *log.Entry, env environment.Environment, rt *runtime.Runtime) error {
fullEnv := environment.New()