mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 03:27:02 -07:00
Merge pull request #14 from OpenCHAMI/minor-refactor
Minor refactoring and improvements
This commit is contained in:
commit
6cc8a873bf
10 changed files with 289 additions and 151 deletions
|
|
@ -13,12 +13,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
accessToken string
|
||||
remoteHost string
|
||||
remotePort int
|
||||
)
|
||||
|
||||
var fetchCmd = &cobra.Command{
|
||||
Use: "fetch",
|
||||
Short: "Fetch a config file from a remote instance of configurator",
|
||||
|
|
|
|||
|
|
@ -12,12 +12,14 @@ import (
|
|||
configurator "github.com/OpenCHAMI/configurator/pkg"
|
||||
"github.com/OpenCHAMI/configurator/pkg/generator"
|
||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
tokenFetchRetries int
|
||||
pluginPaths []string
|
||||
templatePaths []string
|
||||
pluginPath string
|
||||
cacertPath string
|
||||
)
|
||||
|
||||
|
|
@ -27,8 +29,6 @@ var generateCmd = &cobra.Command{
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// make sure that we have a token present before trying to make request
|
||||
if config.AccessToken == "" {
|
||||
// TODO: make request to check if request will need token
|
||||
|
||||
// check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
|
||||
accessToken := os.Getenv("ACCESS_TOKEN")
|
||||
if accessToken != "" {
|
||||
|
|
@ -42,16 +42,10 @@ var generateCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
// use cert path from cobra if empty
|
||||
// TODO: this needs to be checked for the correct desired behavior
|
||||
if config.CertPath == "" {
|
||||
config.CertPath = cacertPath
|
||||
}
|
||||
|
||||
// use config plugins if none supplied via CLI
|
||||
if len(pluginPaths) <= 0 {
|
||||
pluginPaths = append(pluginPaths, config.PluginDirs...)
|
||||
}
|
||||
|
||||
// show config as JSON and generators if verbose
|
||||
if verbose {
|
||||
b, err := json.MarshalIndent(config, "", " ")
|
||||
|
|
@ -61,8 +55,22 @@ var generateCmd = &cobra.Command{
|
|||
fmt.Printf("%v\n", string(b))
|
||||
}
|
||||
|
||||
RunTargets(&config, args, targets...)
|
||||
// run all of the target recursively until completion if provided
|
||||
if len(targets) > 0 {
|
||||
RunTargets(&config, args, targets...)
|
||||
} else {
|
||||
if pluginPath == "" {
|
||||
fmt.Printf("no plugin path specified")
|
||||
return
|
||||
}
|
||||
|
||||
// run generator.Generate() with just plugin path and templates provided
|
||||
generator.Generate(&config, generator.Params{
|
||||
PluginPath: pluginPath,
|
||||
TemplatePaths: templatePaths,
|
||||
})
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -75,22 +83,20 @@ var generateCmd = &cobra.Command{
|
|||
func RunTargets(config *configurator.Config, args []string, targets ...string) {
|
||||
// generate config with each supplied target
|
||||
for _, target := range targets {
|
||||
params := generator.Params{
|
||||
Args: args,
|
||||
PluginPaths: pluginPaths,
|
||||
Target: target,
|
||||
Verbose: verbose,
|
||||
}
|
||||
outputBytes, err := generator.GenerateWithTarget(config, params)
|
||||
outputBytes, err := generator.GenerateWithTarget(config, generator.Params{
|
||||
Args: args,
|
||||
PluginPath: pluginPath,
|
||||
Target: target,
|
||||
Verbose: verbose,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("failed to generate config: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to generate config")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputMap := generator.ConvertContentsToString(outputBytes)
|
||||
|
||||
// if we have more than one target and output is set, create configs in directory
|
||||
var (
|
||||
outputMap = generator.ConvertContentsToString(outputBytes)
|
||||
targetCount = len(targets)
|
||||
templateCount = len(outputMap)
|
||||
)
|
||||
|
|
@ -110,16 +116,16 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) {
|
|||
for _, contents := range outputBytes {
|
||||
err := os.WriteFile(outputPath, contents, 0o644)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to write config to file: %v", err)
|
||||
log.Error().Err(err).Msg("failed to write config to file")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("wrote file to '%s'\n", outputPath)
|
||||
log.Info().Msgf("wrote file to '%s'\n", outputPath)
|
||||
}
|
||||
} else if outputPath != "" && targetCount > 1 || templateCount > 1 {
|
||||
// write multiple files in directory using template name
|
||||
err := os.MkdirAll(filepath.Clean(outputPath), 0o755)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to make output directory: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to make output directory")
|
||||
os.Exit(1)
|
||||
}
|
||||
for path, contents := range outputBytes {
|
||||
|
|
@ -127,15 +133,17 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) {
|
|||
cleanPath := fmt.Sprintf("%s/%s", filepath.Clean(outputPath), filename)
|
||||
err := os.WriteFile(cleanPath, contents, 0o755)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to write config to file: %v\n", err)
|
||||
log.Error().Err(err).Msg("failed to write config to file")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("wrote file to '%s'\n", cleanPath)
|
||||
log.Info().Msgf("wrote file to '%s'\n", cleanPath)
|
||||
}
|
||||
}
|
||||
|
||||
// remove any targets that are the same as current to prevent infinite loop
|
||||
nextTargets := util.CopyIf(config.Targets[target].RunTargets, func(t string) bool { return t != target })
|
||||
nextTargets := util.CopyIf(config.Targets[target].RunTargets, func(nextTarget string) bool {
|
||||
return nextTarget != target
|
||||
})
|
||||
|
||||
// ...then, run any other targets that the current target has
|
||||
RunTargets(config, args, nextTargets...)
|
||||
|
|
@ -143,11 +151,20 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) {
|
|||
}
|
||||
|
||||
func init() {
|
||||
generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the target configs to make")
|
||||
generateCmd.Flags().StringSliceVar(&pluginPaths, "plugins", []string{}, "set the generator plugins directory path")
|
||||
generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the targets to run pre-defined config")
|
||||
generateCmd.Flags().StringSliceVar(&templatePaths, "template", []string{}, "set the paths for the Jinja 2 templates to use")
|
||||
generateCmd.Flags().StringVar(&pluginPath, "plugin", "", "set the generator plugin path")
|
||||
generateCmd.Flags().StringVarP(&outputPath, "output", "o", "", "set the output path for config targets")
|
||||
generateCmd.Flags().StringVar(&cacertPath, "ca-cert", "", "path to CA cert. (defaults to system CAs)")
|
||||
generateCmd.Flags().StringVar(&cacertPath, "cacert", "", "path to CA cert. (defaults to system CAs)")
|
||||
generateCmd.Flags().IntVar(&tokenFetchRetries, "fetch-retries", 5, "set the number of retries to fetch an access token")
|
||||
generateCmd.Flags().StringVar(&remoteHost, "host", "http://localhost", "set the remote host")
|
||||
generateCmd.Flags().IntVar(&remotePort, "port", 80, "set the remote port")
|
||||
|
||||
// requires either 'target' by itself or 'plugin' and 'templates' together
|
||||
// generateCmd.MarkFlagsOneRequired("target", "plugin")
|
||||
generateCmd.MarkFlagsMutuallyExclusive("target", "plugin")
|
||||
generateCmd.MarkFlagsMutuallyExclusive("target", "template")
|
||||
generateCmd.MarkFlagsRequiredTogether("plugin", "template")
|
||||
|
||||
rootCmd.AddCommand(generateCmd)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/OpenCHAMI/configurator/pkg/generator"
|
||||
"github.com/OpenCHAMI/configurator/pkg/util"
|
||||
"github.com/rodaine/table"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
|
@ -26,11 +27,16 @@ var inspectCmd = &cobra.Command{
|
|||
return strings.ToUpper(fmt.Sprintf(format, vals...))
|
||||
}
|
||||
|
||||
// TODO: remove duplicate args from CLI
|
||||
// remove duplicate clean paths from CLI
|
||||
paths := make([]string, len(args))
|
||||
for _, path := range args {
|
||||
paths = append(paths, filepath.Clean(path))
|
||||
}
|
||||
paths = util.RemoveDuplicates(paths)
|
||||
|
||||
// load specific plugins from positional args
|
||||
var generators = make(map[string]generator.Generator)
|
||||
for _, path := range args {
|
||||
for _, path := range paths {
|
||||
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
13
cmd/root.go
13
cmd/root.go
|
|
@ -10,11 +10,14 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
configPath string
|
||||
config configurator.Config
|
||||
verbose bool
|
||||
targets []string
|
||||
outputPath string
|
||||
configPath string
|
||||
config configurator.Config
|
||||
verbose bool
|
||||
targets []string
|
||||
outputPath string
|
||||
accessToken string
|
||||
remoteHost string
|
||||
remotePort int
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
|
|
|
|||
17
cmd/serve.go
17
cmd/serve.go
|
|
@ -35,11 +35,6 @@ var serveCmd = &cobra.Command{
|
|||
}
|
||||
}
|
||||
|
||||
// use config plugins if none supplied via CLI
|
||||
if len(pluginPaths) <= 0 {
|
||||
pluginPaths = append(pluginPaths, config.PluginDirs...)
|
||||
}
|
||||
|
||||
// show config as JSON and generators if verbose
|
||||
if verbose {
|
||||
b, err := json.MarshalIndent(config, "", " ")
|
||||
|
|
@ -60,15 +55,19 @@ var serveCmd = &cobra.Command{
|
|||
Retries: config.Server.Jwks.Retries,
|
||||
},
|
||||
GeneratorParams: generator.Params{
|
||||
Args: args,
|
||||
PluginPaths: pluginPaths,
|
||||
Args: args,
|
||||
PluginPath: pluginPath,
|
||||
// Target: target, // NOTE: targets are set via HTTP requests (ex: curl http://configurator:3334/generate?target=dnsmasq)
|
||||
Verbose: verbose,
|
||||
},
|
||||
}
|
||||
|
||||
// start listening with the server
|
||||
err := server.Serve()
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
fmt.Printf("Server closed.")
|
||||
if verbose {
|
||||
fmt.Printf("Server closed.")
|
||||
}
|
||||
} else if err != nil {
|
||||
fmt.Errorf("failed to start server: %v", err)
|
||||
os.Exit(1)
|
||||
|
|
@ -79,7 +78,7 @@ var serveCmd = &cobra.Command{
|
|||
func init() {
|
||||
serveCmd.Flags().StringVar(&config.Server.Host, "host", config.Server.Host, "set the server host")
|
||||
serveCmd.Flags().IntVar(&config.Server.Port, "port", config.Server.Port, "set the server port")
|
||||
serveCmd.Flags().StringSliceVar(&pluginPaths, "plugins", nil, "set the generator plugins directory path")
|
||||
serveCmd.Flags().StringVar(&pluginPath, "plugin", "", "set the generator plugins directory path")
|
||||
serveCmd.Flags().StringVar(&config.Server.Jwks.Uri, "jwks-uri", config.Server.Jwks.Uri, "set the JWKS url to fetch public key")
|
||||
serveCmd.Flags().IntVar(&config.Server.Jwks.Retries, "jwks-fetch-retries", config.Server.Jwks.Retries, "set the JWKS fetch retry count")
|
||||
rootCmd.AddCommand(serveCmd)
|
||||
|
|
|
|||
23
pkg/auth.go
23
pkg/auth.go
|
|
@ -8,26 +8,9 @@ import (
|
|||
"slices"
|
||||
|
||||
"github.com/OpenCHAMI/jwtauth/v5"
|
||||
"github.com/lestrrat-go/jwx/jwk"
|
||||
"github.com/lestrrat-go/jwx/v2/jwk"
|
||||
)
|
||||
|
||||
func VerifyClaims(testClaims []string, r *http.Request) (bool, error) {
|
||||
// extract claims from JWT
|
||||
_, claims, err := jwtauth.FromContext(r.Context())
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to get claims(s) from token: %v", err)
|
||||
}
|
||||
|
||||
// verify that each one of the test claims are included
|
||||
for _, testClaim := range testClaims {
|
||||
_, ok := claims[testClaim]
|
||||
if !ok {
|
||||
return false, fmt.Errorf("failed to verify claim(s) from token: %s", testClaim)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func VerifyScope(testScopes []string, r *http.Request) (bool, error) {
|
||||
// extract the scopes from JWT
|
||||
var scopes []string
|
||||
|
|
@ -112,3 +95,7 @@ func FetchPublicKeyFromURL(url string) (*jwtauth.JWTAuth, error) {
|
|||
|
||||
return tokenAuth, nil
|
||||
}
|
||||
|
||||
func LoadAccessToken() {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package generator
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"maps"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
|
|
@ -17,6 +17,7 @@ import (
|
|||
type Mappings map[string]any
|
||||
type FileMap map[string][]byte
|
||||
type FileList [][]byte
|
||||
type Template []byte
|
||||
|
||||
// Generator interface used to define how files are created. Plugins can
|
||||
// be created entirely independent of the main driver program.
|
||||
|
|
@ -29,11 +30,12 @@ type Generator interface {
|
|||
|
||||
// Params defined and used by the "generate" subcommand.
|
||||
type Params struct {
|
||||
Args []string
|
||||
PluginPaths []string
|
||||
Generators map[string]Generator
|
||||
Target string
|
||||
Verbose bool
|
||||
Args []string
|
||||
Generators map[string]Generator
|
||||
TemplatePaths []string
|
||||
PluginPath string
|
||||
Target string
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// Converts the file outputs from map[string][]byte to map[string]string.
|
||||
|
|
@ -51,13 +53,13 @@ func LoadFiles(paths ...string) (FileMap, error) {
|
|||
for _, path := range paths {
|
||||
expandedPaths, err := filepath.Glob(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to glob path: %v", err)
|
||||
return nil, fmt.Errorf("failed to glob path: %w", err)
|
||||
}
|
||||
for _, expandedPath := range expandedPaths {
|
||||
info, err := os.Stat(expandedPath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, fmt.Errorf("failed to stat file or directory: %v", err)
|
||||
return nil, fmt.Errorf("failed to stat file or directory: %w", err)
|
||||
}
|
||||
// skip any directories found
|
||||
if info.IsDir() {
|
||||
|
|
@ -65,7 +67,7 @@ func LoadFiles(paths ...string) (FileMap, error) {
|
|||
}
|
||||
b, err := os.ReadFile(expandedPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file: %v", err)
|
||||
return nil, fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
outputs[expandedPath] = b
|
||||
|
|
@ -81,19 +83,19 @@ func LoadPlugin(path string) (Generator, error) {
|
|||
if isDir, err := util.IsDirectory(path); err == nil && isDir {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("failed to test if path is directory: %v", err)
|
||||
return nil, fmt.Errorf("failed to test if plugin path is directory: %w", err)
|
||||
}
|
||||
|
||||
// try and open the plugin
|
||||
p, err := plugin.Open(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open plugin: %v", err)
|
||||
return nil, fmt.Errorf("failed to open plugin: %w", err)
|
||||
}
|
||||
|
||||
// load the "Generator" symbol from plugin
|
||||
symbol, err := p.Lookup("Generator")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to look up symbol at path '%s': %v", path, err)
|
||||
return nil, fmt.Errorf("failed to look up symbol at path '%s': %w", path, err)
|
||||
}
|
||||
|
||||
// assert that the plugin loaded has a valid generator
|
||||
|
|
@ -111,45 +113,123 @@ func LoadPlugin(path string) (Generator, error) {
|
|||
func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) {
|
||||
// check if verbose option is supplied
|
||||
var (
|
||||
gens = make(map[string]Generator)
|
||||
params = util.GetParams(opts...)
|
||||
generators = make(map[string]Generator)
|
||||
params = util.ToDict(opts...)
|
||||
)
|
||||
|
||||
items, _ := os.ReadDir(dirpath)
|
||||
for _, item := range items {
|
||||
if item.IsDir() {
|
||||
subitems, _ := os.ReadDir(item.Name())
|
||||
for _, subitem := range subitems {
|
||||
if !subitem.IsDir() {
|
||||
gen, err := LoadPlugin(subitem.Name())
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err)
|
||||
continue
|
||||
}
|
||||
if verbose, ok := params["verbose"].(bool); ok {
|
||||
if verbose {
|
||||
fmt.Printf("-- found plugin '%s'\n", item.Name())
|
||||
}
|
||||
}
|
||||
gens[gen.GetName()] = gen
|
||||
}
|
||||
//
|
||||
err := filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error {
|
||||
// skip trying to load generator plugin if directory or error
|
||||
if info.IsDir() || err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// load the generator plugin from current path
|
||||
gen, err := LoadPlugin(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
|
||||
}
|
||||
|
||||
// show the plugins found if verbose flag is set
|
||||
if params.GetVerbose() {
|
||||
fmt.Printf("-- found plugin '%s'\n", gen.GetName())
|
||||
}
|
||||
|
||||
// map each generator plugin by name for lookup
|
||||
generators[gen.GetName()] = gen
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to walk directory: %w", err)
|
||||
}
|
||||
|
||||
// items, _ := os.ReadDir(dirpath)
|
||||
// for _, item := range items {
|
||||
// if item.IsDir() {
|
||||
// subitems, _ := os.ReadDir(item.Name())
|
||||
// for _, subitem := range subitems {
|
||||
// if !subitem.IsDir() {
|
||||
// gen, err := LoadPlugin(subitem.Name())
|
||||
// if err != nil {
|
||||
// fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err)
|
||||
// continue
|
||||
// }
|
||||
// if verbose, ok := params["verbose"].(bool); ok {
|
||||
// if verbose {
|
||||
// fmt.Printf("-- found plugin '%s'\n", item.Name())
|
||||
// }
|
||||
// }
|
||||
// gens[gen.GetName()] = gen
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// gen, err := LoadPlugin(dirpath + item.Name())
|
||||
// if err != nil {
|
||||
// fmt.Printf("failed to load plugin: %v\n", err)
|
||||
// continue
|
||||
// }
|
||||
// if verbose, ok := params["verbose"].(bool); ok {
|
||||
// if verbose {
|
||||
// fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name())
|
||||
// }
|
||||
// }
|
||||
// gens[gen.GetName()] = gen
|
||||
// }
|
||||
// }
|
||||
|
||||
return generators, nil
|
||||
}
|
||||
|
||||
func LoadTemplate(path string) (Template, error) {
|
||||
// skip loading template if path is a directory with no error
|
||||
if isDir, err := util.IsDirectory(path); err == nil && isDir {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("failed to test if template path is directory: %w", err)
|
||||
}
|
||||
|
||||
// try and read the contents of the file
|
||||
// NOTE: we don't care if this is actually a Jinja template
|
||||
// or not...at least for now.
|
||||
return os.ReadFile(path)
|
||||
}
|
||||
|
||||
func LoadTemplates(paths []string, opts ...util.Option) (map[string]Template, error) {
|
||||
var (
|
||||
templates = make(map[string]Template)
|
||||
params = util.ToDict(opts...)
|
||||
)
|
||||
|
||||
for _, path := range paths {
|
||||
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
|
||||
// skip trying to load generator plugin if directory or error
|
||||
if info.IsDir() || err != nil {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
gen, err := LoadPlugin(dirpath + item.Name())
|
||||
|
||||
// load the contents of the template
|
||||
template, err := LoadTemplate(path)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load plugin: %v\n", err)
|
||||
continue
|
||||
return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
|
||||
}
|
||||
if verbose, ok := params["verbose"].(bool); ok {
|
||||
if verbose {
|
||||
fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name())
|
||||
}
|
||||
|
||||
// show the templates loaded if verbose flag is set
|
||||
if params.GetVerbose() {
|
||||
fmt.Printf("-- loaded tempalte '%s'\n", path)
|
||||
}
|
||||
gens[gen.GetName()] = gen
|
||||
|
||||
// map each template by the path it was loaded from
|
||||
templates[path] = template
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to walk directory: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return gens, nil
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
// Option to specify "target" in parameter map. This is used to set which generator
|
||||
|
|
@ -171,6 +251,31 @@ func WithType(_type string) util.Option {
|
|||
}
|
||||
}
|
||||
|
||||
// Option to the plugin to load
|
||||
func WithPlugin(path string) util.Option {
|
||||
return func(p util.Params) {
|
||||
if p != nil {
|
||||
plugin, err := LoadPlugin(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p["plugin"] = plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithTemplates(paths []string) util.Option {
|
||||
return func(p util.Params) {
|
||||
if p != nil {
|
||||
templates, err := LoadTemplates(paths)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
p["templates"] = templates
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Option to a specific client to include in implementing plugin generator.Generate().
|
||||
//
|
||||
// NOTE: This may be changed to pass some kind of client interface as an argument in
|
||||
|
|
@ -217,13 +322,13 @@ func ApplyTemplates(mappings Mappings, contents ...[]byte) (FileList, error) {
|
|||
// load jinja template from file
|
||||
t, err := gonja.FromBytes(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read template from file: %v", err)
|
||||
return nil, fmt.Errorf("failed to read template from file: %w", err)
|
||||
}
|
||||
|
||||
// execute/render jinja template
|
||||
b := bytes.Buffer{}
|
||||
if err = t.Execute(&b, data); err != nil {
|
||||
return nil, fmt.Errorf("failed to execute: %v", err)
|
||||
return nil, fmt.Errorf("failed to execute: %w", err)
|
||||
}
|
||||
outputs = append(outputs, b.Bytes())
|
||||
}
|
||||
|
|
@ -243,13 +348,13 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
|
|||
// load jinja template from file
|
||||
t, err := gonja.FromFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read template from file: %v", err)
|
||||
return nil, fmt.Errorf("failed to read template from file: %w", err)
|
||||
}
|
||||
|
||||
// execute/render jinja template
|
||||
b := bytes.Buffer{}
|
||||
if err = t.Execute(&b, data); err != nil {
|
||||
return nil, fmt.Errorf("failed to execute: %v", err)
|
||||
return nil, fmt.Errorf("failed to execute: %w", err)
|
||||
}
|
||||
outputs[path] = b.Bytes()
|
||||
}
|
||||
|
|
@ -257,6 +362,23 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
|
|||
return outputs, nil
|
||||
}
|
||||
|
||||
// Generate() is the main function to generate a collection of files and returns them as a map.
|
||||
// This function only expects a path to a plugin and paths to a collection of templates to
|
||||
// be used. This function will only load the plugin on-demand and fetch resources as needed.
|
||||
func Generate(config *configurator.Config, params Params) (FileMap, error) {
|
||||
var (
|
||||
gen Generator
|
||||
client = configurator.NewSmdClient()
|
||||
)
|
||||
|
||||
return gen.Generate(
|
||||
config,
|
||||
WithPlugin(params.PluginPath),
|
||||
WithTemplates(params.TemplatePaths),
|
||||
WithClient(client),
|
||||
)
|
||||
}
|
||||
|
||||
// Main function to generate a collection of files as a map with the path as the key and
|
||||
// the contents of the file as the value. This function currently expects a list of plugin
|
||||
// paths to load all plugins within a directory. Then, each plugin's generator.GenerateWithTarget()
|
||||
|
|
@ -269,8 +391,7 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
|
|||
func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
|
||||
// load generator plugins to generate configs or to print
|
||||
var (
|
||||
generators = make(map[string]Generator)
|
||||
client = configurator.NewSmdClient(
|
||||
client = configurator.NewSmdClient(
|
||||
configurator.WithHost(config.SmdClient.Host),
|
||||
configurator.WithPort(config.SmdClient.Port),
|
||||
configurator.WithAccessToken(config.AccessToken),
|
||||
|
|
@ -278,41 +399,32 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
|
|||
)
|
||||
)
|
||||
|
||||
// load all plugins from supplied arguments
|
||||
for _, path := range params.PluginPaths {
|
||||
if params.Verbose {
|
||||
fmt.Printf("loading plugins from '%s'\n", path)
|
||||
}
|
||||
plugins, err := LoadPlugins(path)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load plugins: %v\n", err)
|
||||
err = nil
|
||||
continue
|
||||
}
|
||||
|
||||
// add loaded generator plugins to set
|
||||
maps.Copy(generators, plugins)
|
||||
// check if a target is supplied
|
||||
if len(params.Args) == 0 && params.Target == "" {
|
||||
return nil, fmt.Errorf("must specify a target")
|
||||
}
|
||||
|
||||
// copy all generators supplied from arguments
|
||||
maps.Copy(generators, params.Generators)
|
||||
// load target information from config
|
||||
target, ok := config.Targets[params.Target]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("target not found in config")
|
||||
}
|
||||
|
||||
// show available targets then exit
|
||||
if len(params.Args) == 0 && params.Target == "" {
|
||||
for g := range generators {
|
||||
fmt.Printf("-- found generator plugin \"%s\"\n", g)
|
||||
}
|
||||
return nil, nil
|
||||
// if plugin path specified from CLI, use that instead
|
||||
if params.PluginPath != "" {
|
||||
target.PluginPath = params.PluginPath
|
||||
}
|
||||
|
||||
// only load the plugin needed for this target
|
||||
generator, err := LoadPlugin(target.PluginPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load plugin: %w", err)
|
||||
}
|
||||
|
||||
// run the generator plugin from target passed
|
||||
gen := generators[params.Target]
|
||||
if gen == nil {
|
||||
return nil, fmt.Errorf("invalid generator target (%s)", params.Target)
|
||||
}
|
||||
return gen.Generate(
|
||||
return generator.Generate(
|
||||
config,
|
||||
WithTarget(gen.GetName()),
|
||||
WithTarget(generator.GetName()),
|
||||
WithClient(client),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
|
|||
if client != nil {
|
||||
eths, err = client.FetchEthernetInterfaces(opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %v", err)
|
||||
return nil, fmt.Errorf("failed to fetch ethernet interfaces with client: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ type Params map[string]any
|
|||
type Option func(Params)
|
||||
|
||||
// Extract all parameters from the options passed as map[string]any.
|
||||
func GetParams(opts ...Option) Params {
|
||||
func ToDict(opts ...Option) Params {
|
||||
params := Params{}
|
||||
for _, opt := range opts {
|
||||
opt(params)
|
||||
|
|
@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option {
|
|||
}
|
||||
}
|
||||
|
||||
// Syntactic sugar generic function to get parameter from util.Params.
|
||||
func Get[T any](params Params, key string, opts ...Option) *T {
|
||||
// Sugary generic function to get parameter from util.Params.
|
||||
func Get[T any](params Params, key string) *T {
|
||||
if v, ok := params[key].(T); ok {
|
||||
return &v
|
||||
}
|
||||
|
|
@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOpt[T any](opts []Option, key string) *T {
|
||||
return Get[T](ToDict(opts...), "required_claims")
|
||||
}
|
||||
|
||||
func (p Params) GetVerbose() bool {
|
||||
if verbose, ok := p["verbose"].(bool); ok {
|
||||
return verbose
|
||||
}
|
||||
|
||||
// default setting
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,14 @@ package util
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -28,7 +30,7 @@ func IsDirectory(path string) (bool, error) {
|
|||
// This returns an *os.FileInfo type
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to stat path: %v", err)
|
||||
return false, fmt.Errorf("failed to stat path (%s): %v", path, err)
|
||||
}
|
||||
|
||||
// IsDir is short for fileInfo.Mode().IsDir()
|
||||
|
|
@ -63,7 +65,7 @@ func MakeRequest(url string, httpMethod string, body []byte, headers map[string]
|
|||
// NOTE: This currently requires git to be installed.
|
||||
// TODO: Change how this is done to not require executing a command.
|
||||
func GitCommit() string {
|
||||
c := exec.Command("git", "rev-parse", "HEAD")
|
||||
c := exec.Command("git", "rev-parse", "--short=8", "HEAD")
|
||||
stdout, err := c.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
|
|
@ -80,6 +82,11 @@ func RemoveIndex[T comparable](s []T, index int) []T {
|
|||
return append(ret, s[index+1:]...)
|
||||
}
|
||||
|
||||
func RemoveDuplicates[T cmp.Ordered](s []T) []T {
|
||||
slices.Sort(s)
|
||||
return slices.Compact(s)
|
||||
}
|
||||
|
||||
// General function to copy elements from slice if condition is true.
|
||||
func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
|
||||
var f = make([]T, 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue