Minor refactor to run additional targets recursively

This commit is contained in:
David Allen 2024-06-27 11:19:16 -06:00
parent 2154657d34
commit cccbb1ad25
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
2 changed files with 81 additions and 52 deletions

View file

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
configurator "github.com/OpenCHAMI/configurator/internal"
"github.com/OpenCHAMI/configurator/internal/generator"
"github.com/OpenCHAMI/configurator/internal/util"
"github.com/spf13/cobra"
@ -60,6 +61,12 @@ var generateCmd = &cobra.Command{
fmt.Printf("%v\n", string(b))
}
RunTargets(targets...)
},
}
func RunTargets(config *configurator.Config, targets ...string) {
// generate config with each supplied target
for _, target := range targets {
params := generator.Params{
@ -120,9 +127,13 @@ var generateCmd = &cobra.Command{
fmt.Printf("wrote file to '%s'\n", cleanPath)
}
}
}
},
// remove any targets that are the same as current to prevent infinite loop
nextTargets := util.CopyIf(config.Targets[targets].Targets, func(t T) bool { return t != target })
// ...then, run any other targets that the current target has
RunTargets(config, nextTargets...)
}
}
func init() {

View file

@ -61,3 +61,21 @@ func GitCommit() string {
return strings.TrimRight(string(stdout), "\n")
}
// NOTE: would it be better to use slices.DeleteFunc instead
func RemoveIndex[T comparable](s []T, index int) []T {
ret := make([]T, 0)
ret = append(ret, s[:index]...)
return append(ret, s[index+1:]...)
}
func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
var f = make([]T, 0)
for _, e := range s {
if condition(e) {
f = append(f, e)
}
}
return f
}