Skip to content
Open
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
6 changes: 3 additions & 3 deletions internal/dockerclient/docker_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func parseHost(addr string) (string, string, error) {
func SplitDockerImage(img string) (string, string, string) {
var registry, tag string
repository := img
if i := strings.Index(img, "/"); i != -1 {
if first := img[:i]; strings.ContainsAny(first, ".:") || first == "localhost" {
if before, after, ok := strings.Cut(img, "/"); ok {
if first := before; strings.ContainsAny(first, ".:") || first == "localhost" {
registry = first
repository = img[i+1:]
repository = after
}
}

Expand Down
8 changes: 3 additions & 5 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ func (g *generator) generateFromSignals() {
return
}

g.wg.Add(1)
go func() {
defer g.wg.Done()
g.wg.Go(func() {

Comment thread
buchdag marked this conversation as resolved.
sigChan, cleanup := newSignalChannel()
defer cleanup()
Expand All @@ -123,7 +121,7 @@ func (g *generator) generateFromSignals() {
return
}
}
}()
})
}

func (g *generator) generateFromContainers() {
Expand Down Expand Up @@ -338,7 +336,7 @@ func (g *generator) runNotifyCmd(config config.Config) {
log.Printf("Error running notify command: %s, %s\n", config.NotifyCmd, err)
}
if config.NotifyOutput {
for _, line := range strings.Split(string(out), "\n") {
for line := range strings.SplitSeq(string(out), "\n") {
if line != "" {
log.Printf("[%s]: %s", config.NotifyCmd, line)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/template/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

func keys(input interface{}) (interface{}, error) {
func keys(input any) (any, error) {
if input == nil {
return nil, nil
}
Expand All @@ -20,7 +20,7 @@ func keys(input interface{}) (interface{}, error) {
}

vk := val.MapKeys()
k := make([]interface{}, val.Len())
k := make([]any, val.Len())
for i := range k {
k[i] = vk[i].Interface()
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func comment(delimiter string, source string) string {
return regexPattern.ReplaceAllString(source, delimiter)
}

func contains(input interface{}, key interface{}) bool {
func contains(input any, key any) bool {
if input == nil {
return false
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func dirList(path string) ([]string, error) {
}

// coalesce returns the first non nil argument
func coalesce(input ...interface{}) interface{} {
func coalesce(input ...any) any {
for _, v := range input {
if v != nil {
return v
Expand All @@ -114,7 +114,7 @@ func coalesce(input ...interface{}) interface{} {
}

// when returns the trueValue when the condition is true and the falseValue otherwise
func when(condition bool, trueValue, falseValue interface{}) interface{} {
func when(condition bool, trueValue, falseValue any) any {
if condition {
return trueValue
} else {
Expand Down
2 changes: 1 addition & 1 deletion internal/template/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestContainsInteger(t *testing.T) {
}

func TestContainsNilInput(t *testing.T) {
var env interface{} = nil
var env any = nil

assert.False(t, contains(env, 0))
assert.False(t, contains(env, ""))
Expand Down
42 changes: 21 additions & 21 deletions internal/template/groupby.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

// Generalized groupBy function
func generalizedGroupBy(funcName string, entries interface{}, getValue func(interface{}) (interface{}, error), addEntry func(map[string][]interface{}, interface{}, interface{})) (map[string][]interface{}, error) {
func generalizedGroupBy(funcName string, entries any, getValue func(any) (any, error), addEntry func(map[string][]any, any, any)) (map[string][]any, error) {
entriesVal, err := getArrayValues(funcName, entries)

if err != nil {
return nil, err
}

groups := make(map[string][]interface{})
groups := make(map[string][]any)
for i := 0; i < entriesVal.Len(); i++ {
v := entriesVal.Index(i).Interface()
value, err := getValue(v)
Expand All @@ -29,46 +29,46 @@ func generalizedGroupBy(funcName string, entries interface{}, getValue func(inte
return groups, nil
}

func generalizedGroupByKey(funcName string, entries interface{}, key string, addEntry func(map[string][]interface{}, interface{}, interface{})) (map[string][]interface{}, error) {
getKey := func(v interface{}) (interface{}, error) {
func generalizedGroupByKey(funcName string, entries any, key string, addEntry func(map[string][]any, any, any)) (map[string][]any, error) {
getKey := func(v any) (any, error) {
return deepGet(v, key), nil
}
return generalizedGroupBy(funcName, entries, getKey, addEntry)
}

func groupByMulti(entries interface{}, key, sep string) (map[string][]interface{}, error) {
return generalizedGroupByKey("groupByMulti", entries, key, func(groups map[string][]interface{}, value interface{}, v interface{}) {
items := strings.Split(value.(string), sep)
for _, item := range items {
func groupByMulti(entries any, key, sep string) (map[string][]any, error) {
return generalizedGroupByKey("groupByMulti", entries, key, func(groups map[string][]any, value any, v any) {
items := strings.SplitSeq(value.(string), sep)
for item := range items {
groups[item] = append(groups[item], v)
}
})
}

// groupBy groups a generic array or slice by the path property key
func groupBy(entries interface{}, key string) (map[string][]interface{}, error) {
return generalizedGroupByKey("groupBy", entries, key, func(groups map[string][]interface{}, value interface{}, v interface{}) {
func groupBy(entries any, key string) (map[string][]any, error) {
return generalizedGroupByKey("groupBy", entries, key, func(groups map[string][]any, value any, v any) {
groups[value.(string)] = append(groups[value.(string)], v)
})
}

// groupByWithDefault is the same as groupBy but allows a default value to be set
func groupByWithDefault(entries interface{}, key string, defaultValue string) (map[string][]interface{}, error) {
getValueWithDefault := func(v interface{}) (interface{}, error) {
func groupByWithDefault(entries any, key string, defaultValue string) (map[string][]any, error) {
getValueWithDefault := func(v any) (any, error) {
value := deepGet(v, key)
if value == nil {
return defaultValue, nil
}
return value, nil
}
return generalizedGroupBy("groupByWithDefault", entries, getValueWithDefault, func(groups map[string][]interface{}, value interface{}, v interface{}) {
return generalizedGroupBy("groupByWithDefault", entries, getValueWithDefault, func(groups map[string][]any, value any, v any) {
groups[value.(string)] = append(groups[value.(string)], v)
})
}

// groupByKeys is the same as groupBy but only returns a list of keys
func groupByKeys(entries interface{}, key string) ([]string, error) {
keys, err := generalizedGroupByKey("groupByKeys", entries, key, func(groups map[string][]interface{}, value interface{}, v interface{}) {
func groupByKeys(entries any, key string) ([]string, error) {
keys, err := generalizedGroupByKey("groupByKeys", entries, key, func(groups map[string][]any, value any, v any) {
groups[value.(string)] = append(groups[value.(string)], v)
})

Expand All @@ -84,8 +84,8 @@ func groupByKeys(entries interface{}, key string) ([]string, error) {
}

// groupByLabel is the same as groupBy but over a given label
func groupByLabel(entries interface{}, label string) (map[string][]interface{}, error) {
getLabel := func(v interface{}) (interface{}, error) {
func groupByLabel(entries any, label string) (map[string][]any, error) {
getLabel := func(v any) (any, error) {
if container, ok := v.(*context.RuntimeContainer); ok {
if value, ok := container.Labels[label]; ok {
return value, nil
Expand All @@ -94,14 +94,14 @@ func groupByLabel(entries interface{}, label string) (map[string][]interface{},
}
return nil, fmt.Errorf("must pass an array or slice of *RuntimeContainer to 'groupByLabel'; received %v", v)
}
return generalizedGroupBy("groupByLabel", entries, getLabel, func(groups map[string][]interface{}, value interface{}, v interface{}) {
return generalizedGroupBy("groupByLabel", entries, getLabel, func(groups map[string][]any, value any, v any) {
groups[value.(string)] = append(groups[value.(string)], v)
})
}

// groupByLabelWithDefault is the same as groupByLabel but allows a default value to be set
func groupByLabelWithDefault(entries interface{}, label string, defaultValue string) (map[string][]interface{}, error) {
getLabel := func(v interface{}) (interface{}, error) {
func groupByLabelWithDefault(entries any, label string, defaultValue string) (map[string][]any, error) {
getLabel := func(v any) (any, error) {
if container, ok := v.(*context.RuntimeContainer); ok {
if value, ok := container.Labels[label]; ok {
return value, nil
Expand All @@ -110,7 +110,7 @@ func groupByLabelWithDefault(entries interface{}, label string, defaultValue str
}
return nil, fmt.Errorf("must pass an array or slice of *RuntimeContainer to 'groupByLabel'; received %v", v)
}
return generalizedGroupBy("groupByLabelWithDefault", entries, getLabel, func(groups map[string][]interface{}, value interface{}, v interface{}) {
return generalizedGroupBy("groupByLabelWithDefault", entries, getLabel, func(groups map[string][]any, value any, v any) {
groups[value.(string)] = append(groups[value.(string)], v)
})
}
4 changes: 2 additions & 2 deletions internal/template/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func parseAllocateInt(desired string) (int, error) {
return math.MaxInt32, nil
}

func deepGetImpl(v reflect.Value, path []string) interface{} {
func deepGetImpl(v reflect.Value, path []string) any {
if !v.IsValid() {
return nil
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func deepGetImpl(v reflect.Value, path []string) interface{} {
}
}

func deepGet(item interface{}, path string) interface{} {
func deepGet(item any, path string) any {
var parts []string
if path != "" {
parts = strings.Split(strings.TrimPrefix(path, "."), ".")
Expand Down
4 changes: 2 additions & 2 deletions internal/template/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func TestDeepGet(t *testing.T) {

for _, tc := range []struct {
desc string
item interface{}
item any
path string
want interface{}
want any
}{
{
"map of string",
Expand Down
20 changes: 10 additions & 10 deletions internal/template/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func sortStringsDesc(values []string) []string {

type sortable interface {
sort.Interface
set(string, interface{}) error
get() []interface{}
set(string, any) error
get() []any
}

type sortableData struct {
data []interface{}
data []any
}

func (s sortableData) get() []interface{} {
func (s sortableData) get() []any {
return s.data
}

Expand All @@ -42,19 +42,19 @@ type sortableByKey struct {
key string
}

func (s *sortableByKey) set(funcName string, entries interface{}) (err error) {
func (s *sortableByKey) set(funcName string, entries any) (err error) {
entriesVal, err := getArrayValues(funcName, entries)
if err != nil {
return
}
s.data = make([]interface{}, entriesVal.Len())
s.data = make([]any, entriesVal.Len())
for i := 0; i < entriesVal.Len(); i++ {
s.data[i] = entriesVal.Index(i).Interface()
}
return
}

func getFieldAsString(item interface{}, path string) string {
func getFieldAsString(item any, path string) string {
// Mostly inspired by https://stackoverflow.com/a/47739620
e := deepGet(item, path)
r := reflect.ValueOf(e)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s sortableByKey) Less(i, j int) bool {
}

// Generalized SortBy function
func generalizedSortBy(funcName string, entries interface{}, s sortable, reverse bool) (sorted []interface{}, err error) {
func generalizedSortBy(funcName string, entries any, s sortable, reverse bool) (sorted []any, err error) {
err = s.set(funcName, entries)
if err != nil {
return nil, err
Expand All @@ -116,13 +116,13 @@ func generalizedSortBy(funcName string, entries interface{}, s sortable, reverse
}

// sortObjectsByKeysAsc returns a sorted array of objects, sorted by object's key field in ascending order
func sortObjectsByKeysAsc(objs interface{}, key string) ([]interface{}, error) {
func sortObjectsByKeysAsc(objs any, key string) ([]any, error) {
s := &sortableByKey{key: key}
return generalizedSortBy("sortObjsByKeys", objs, s, false)
}

// sortObjectsByKeysDesc returns a sorted array of objects, sorted by object's key field in descending order
func sortObjectsByKeysDesc(objs interface{}, key string) ([]interface{}, error) {
func sortObjectsByKeysDesc(objs any, key string) ([]any, error) {
s := &sortableByKey{key: key}
return generalizedSortBy("sortObjsByKey", objs, s, true)
}
20 changes: 10 additions & 10 deletions internal/template/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,18 @@ func TestSortObjectsByKeys(t *testing.T) {

for _, tc := range []struct {
desc string
fn func(interface{}, string) ([]interface{}, error)
fn func(any, string) ([]any, error)
key string
want []interface{}
want []any
}{
{"Asc simple", sortObjectsByKeysAsc, "ID", []interface{}{o1, o2, o3, o0}},
{"Desc simple", sortObjectsByKeysDesc, "ID", []interface{}{o0, o3, o2, o1}},
{"Asc complex", sortObjectsByKeysAsc, "Env.VIRTUAL_HOST", []interface{}{o3, o0, o2, o1}},
{"Desc complex", sortObjectsByKeysDesc, "Env.VIRTUAL_HOST", []interface{}{o1, o2, o0, o3}},
{"Asc complex w/ dots in key name", sortObjectsByKeysAsc, "Labels.com.docker.compose.container_number", []interface{}{o2, o0, o3, o1}},
{"Desc complex w/ dots in key name", sortObjectsByKeysDesc, "Labels.com.docker.compose.container_number", []interface{}{o1, o3, o0, o2}},
{"Asc time", sortObjectsByKeysAsc, "Created", []interface{}{o3, o0, o2, o1}},
{"Desc time", sortObjectsByKeysDesc, "Created", []interface{}{o1, o2, o0, o3}},
{"Asc simple", sortObjectsByKeysAsc, "ID", []any{o1, o2, o3, o0}},
{"Desc simple", sortObjectsByKeysDesc, "ID", []any{o0, o3, o2, o1}},
{"Asc complex", sortObjectsByKeysAsc, "Env.VIRTUAL_HOST", []any{o3, o0, o2, o1}},
{"Desc complex", sortObjectsByKeysDesc, "Env.VIRTUAL_HOST", []any{o1, o2, o0, o3}},
{"Asc complex w/ dots in key name", sortObjectsByKeysAsc, "Labels.com.docker.compose.container_number", []any{o2, o0, o3, o1}},
{"Desc complex w/ dots in key name", sortObjectsByKeysDesc, "Labels.com.docker.compose.container_number", []any{o1, o3, o0, o2}},
{"Asc time", sortObjectsByKeysAsc, "Created", []any{o3, o0, o2, o1}},
{"Desc time", sortObjectsByKeysDesc, "Created", []any{o1, o2, o0, o3}},
} {
t.Run(tc.desc, func(t *testing.T) {
got, err := tc.fn(containers, tc.key)
Expand Down
4 changes: 2 additions & 2 deletions internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
"github.com/nginx-proxy/docker-gen/internal/utils"
)

func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error) {
func getArrayValues(funcName string, entries any) (*reflect.Value, error) {
entriesVal := reflect.ValueOf(entries)

kind := entriesVal.Kind()

if kind == reflect.Ptr {
if kind == reflect.Pointer {
entriesVal = entriesVal.Elem()
kind = entriesVal.Kind()
}
Expand Down
5 changes: 2 additions & 3 deletions internal/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (

type templateTestList []struct {
tmpl string
context interface{}
expected interface{}
context any
expected any
}

func (tests templateTestList) run(t *testing.T) {
for n, test := range tests {
test := test
t.Run(strconv.Itoa(n), func(t *testing.T) {
t.Parallel()
wantErr, _ := test.expected.(error)
Expand Down
Loading