Merge pull request #14 from OpenCHAMI/minor-refactor

Minor refactoring and improvements
This commit is contained in:
David Allen 2024-10-02 11:03:24 -06:00 committed by GitHub
commit 6cc8a873bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 289 additions and 151 deletions

View file

@ -13,12 +13,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
accessToken string
remoteHost string
remotePort int
)
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
Use: "fetch", Use: "fetch",
Short: "Fetch a config file from a remote instance of configurator", Short: "Fetch a config file from a remote instance of configurator",

View file

@ -12,12 +12,14 @@ import (
configurator "github.com/OpenCHAMI/configurator/pkg" configurator "github.com/OpenCHAMI/configurator/pkg"
"github.com/OpenCHAMI/configurator/pkg/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util" "github.com/OpenCHAMI/configurator/pkg/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
tokenFetchRetries int tokenFetchRetries int
pluginPaths []string templatePaths []string
pluginPath string
cacertPath string cacertPath string
) )
@ -27,8 +29,6 @@ var generateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// make sure that we have a token present before trying to make request // make sure that we have a token present before trying to make request
if config.AccessToken == "" { 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 // check if OCHAMI_ACCESS_TOKEN env var is set if no access token is provided and use that instead
accessToken := os.Getenv("ACCESS_TOKEN") accessToken := os.Getenv("ACCESS_TOKEN")
if accessToken != "" { if accessToken != "" {
@ -42,16 +42,10 @@ var generateCmd = &cobra.Command{
} }
// use cert path from cobra if empty // use cert path from cobra if empty
// TODO: this needs to be checked for the correct desired behavior
if config.CertPath == "" { if config.CertPath == "" {
config.CertPath = cacertPath 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 // show config as JSON and generators if verbose
if verbose { if verbose {
b, err := json.MarshalIndent(config, "", " ") b, err := json.MarshalIndent(config, "", " ")
@ -61,8 +55,22 @@ var generateCmd = &cobra.Command{
fmt.Printf("%v\n", string(b)) 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) { func RunTargets(config *configurator.Config, args []string, targets ...string) {
// generate config with each supplied target // generate config with each supplied target
for _, target := range targets { for _, target := range targets {
params := generator.Params{ outputBytes, err := generator.GenerateWithTarget(config, generator.Params{
Args: args, Args: args,
PluginPaths: pluginPaths, PluginPath: pluginPath,
Target: target, Target: target,
Verbose: verbose, Verbose: verbose,
} })
outputBytes, err := generator.GenerateWithTarget(config, params)
if err != nil { if err != nil {
fmt.Printf("failed to generate config: %v\n", err) log.Error().Err(err).Msg("failed to generate config")
os.Exit(1) os.Exit(1)
} }
outputMap := generator.ConvertContentsToString(outputBytes)
// if we have more than one target and output is set, create configs in directory // if we have more than one target and output is set, create configs in directory
var ( var (
outputMap = generator.ConvertContentsToString(outputBytes)
targetCount = len(targets) targetCount = len(targets)
templateCount = len(outputMap) templateCount = len(outputMap)
) )
@ -110,16 +116,16 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) {
for _, contents := range outputBytes { for _, contents := range outputBytes {
err := os.WriteFile(outputPath, contents, 0o644) err := os.WriteFile(outputPath, contents, 0o644)
if err != nil { 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) 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 { } else if outputPath != "" && targetCount > 1 || templateCount > 1 {
// write multiple files in directory using template name // write multiple files in directory using template name
err := os.MkdirAll(filepath.Clean(outputPath), 0o755) err := os.MkdirAll(filepath.Clean(outputPath), 0o755)
if err != nil { 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) os.Exit(1)
} }
for path, contents := range outputBytes { 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) cleanPath := fmt.Sprintf("%s/%s", filepath.Clean(outputPath), filename)
err := os.WriteFile(cleanPath, contents, 0o755) err := os.WriteFile(cleanPath, contents, 0o755)
if err != nil { 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) 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 // 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 // ...then, run any other targets that the current target has
RunTargets(config, args, nextTargets...) RunTargets(config, args, nextTargets...)
@ -143,11 +151,20 @@ func RunTargets(config *configurator.Config, args []string, targets ...string) {
} }
func init() { func init() {
generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the target configs to make") generateCmd.Flags().StringSliceVar(&targets, "target", []string{}, "set the targets to run pre-defined config")
generateCmd.Flags().StringSliceVar(&pluginPaths, "plugins", []string{}, "set the generator plugins directory path") 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().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().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) rootCmd.AddCommand(generateCmd)
} }

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/OpenCHAMI/configurator/pkg/generator" "github.com/OpenCHAMI/configurator/pkg/generator"
"github.com/OpenCHAMI/configurator/pkg/util"
"github.com/rodaine/table" "github.com/rodaine/table"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -26,11 +27,16 @@ var inspectCmd = &cobra.Command{
return strings.ToUpper(fmt.Sprintf(format, vals...)) 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 // load specific plugins from positional args
var generators = make(map[string]generator.Generator) 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 { err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err

View file

@ -10,11 +10,14 @@ import (
) )
var ( var (
configPath string configPath string
config configurator.Config config configurator.Config
verbose bool verbose bool
targets []string targets []string
outputPath string outputPath string
accessToken string
remoteHost string
remotePort int
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{

View file

@ -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 // show config as JSON and generators if verbose
if verbose { if verbose {
b, err := json.MarshalIndent(config, "", " ") b, err := json.MarshalIndent(config, "", " ")
@ -60,15 +55,19 @@ var serveCmd = &cobra.Command{
Retries: config.Server.Jwks.Retries, Retries: config.Server.Jwks.Retries,
}, },
GeneratorParams: generator.Params{ GeneratorParams: generator.Params{
Args: args, Args: args,
PluginPaths: pluginPaths, PluginPath: pluginPath,
// Target: target, // NOTE: targets are set via HTTP requests (ex: curl http://configurator:3334/generate?target=dnsmasq) // Target: target, // NOTE: targets are set via HTTP requests (ex: curl http://configurator:3334/generate?target=dnsmasq)
Verbose: verbose, Verbose: verbose,
}, },
} }
// start listening with the server
err := server.Serve() err := server.Serve()
if errors.Is(err, http.ErrServerClosed) { if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server closed.") if verbose {
fmt.Printf("Server closed.")
}
} else if err != nil { } else if err != nil {
fmt.Errorf("failed to start server: %v", err) fmt.Errorf("failed to start server: %v", err)
os.Exit(1) os.Exit(1)
@ -79,7 +78,7 @@ var serveCmd = &cobra.Command{
func init() { func init() {
serveCmd.Flags().StringVar(&config.Server.Host, "host", config.Server.Host, "set the server host") 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().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().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") serveCmd.Flags().IntVar(&config.Server.Jwks.Retries, "jwks-fetch-retries", config.Server.Jwks.Retries, "set the JWKS fetch retry count")
rootCmd.AddCommand(serveCmd) rootCmd.AddCommand(serveCmd)

View file

@ -8,26 +8,9 @@ import (
"slices" "slices"
"github.com/OpenCHAMI/jwtauth/v5" "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) { func VerifyScope(testScopes []string, r *http.Request) (bool, error) {
// extract the scopes from JWT // extract the scopes from JWT
var scopes []string var scopes []string
@ -112,3 +95,7 @@ func FetchPublicKeyFromURL(url string) (*jwtauth.JWTAuth, error) {
return tokenAuth, nil return tokenAuth, nil
} }
func LoadAccessToken() {
}

View file

@ -3,7 +3,7 @@ package generator
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"maps" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"plugin" "plugin"
@ -17,6 +17,7 @@ import (
type Mappings map[string]any type Mappings map[string]any
type FileMap map[string][]byte type FileMap map[string][]byte
type FileList [][]byte type FileList [][]byte
type Template []byte
// Generator interface used to define how files are created. Plugins can // Generator interface used to define how files are created. Plugins can
// be created entirely independent of the main driver program. // be created entirely independent of the main driver program.
@ -29,11 +30,12 @@ type Generator interface {
// Params defined and used by the "generate" subcommand. // Params defined and used by the "generate" subcommand.
type Params struct { type Params struct {
Args []string Args []string
PluginPaths []string Generators map[string]Generator
Generators map[string]Generator TemplatePaths []string
Target string PluginPath string
Verbose bool Target string
Verbose bool
} }
// Converts the file outputs from map[string][]byte to map[string]string. // 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 { for _, path := range paths {
expandedPaths, err := filepath.Glob(path) expandedPaths, err := filepath.Glob(path)
if err != nil { 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 { for _, expandedPath := range expandedPaths {
info, err := os.Stat(expandedPath) info, err := os.Stat(expandedPath)
if err != nil { if err != nil {
fmt.Println(err) 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 // skip any directories found
if info.IsDir() { if info.IsDir() {
@ -65,7 +67,7 @@ func LoadFiles(paths ...string) (FileMap, error) {
} }
b, err := os.ReadFile(expandedPath) b, err := os.ReadFile(expandedPath)
if err != nil { 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 outputs[expandedPath] = b
@ -81,19 +83,19 @@ func LoadPlugin(path string) (Generator, error) {
if isDir, err := util.IsDirectory(path); err == nil && isDir { if isDir, err := util.IsDirectory(path); err == nil && isDir {
return nil, nil return nil, nil
} else if err != 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 // try and open the plugin
p, err := plugin.Open(path) p, err := plugin.Open(path)
if err != nil { 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 // load the "Generator" symbol from plugin
symbol, err := p.Lookup("Generator") symbol, err := p.Lookup("Generator")
if err != nil { 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 // 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) { func LoadPlugins(dirpath string, opts ...util.Option) (map[string]Generator, error) {
// check if verbose option is supplied // check if verbose option is supplied
var ( var (
gens = make(map[string]Generator) generators = make(map[string]Generator)
params = util.GetParams(opts...) params = util.ToDict(opts...)
) )
items, _ := os.ReadDir(dirpath) //
for _, item := range items { err := filepath.Walk(dirpath, func(path string, info fs.FileInfo, err error) error {
if item.IsDir() { // skip trying to load generator plugin if directory or error
subitems, _ := os.ReadDir(item.Name()) if info.IsDir() || err != nil {
for _, subitem := range subitems { return nil
if !subitem.IsDir() { }
gen, err := LoadPlugin(subitem.Name())
if err != nil { // load the generator plugin from current path
fmt.Printf("failed to load generator in directory '%s': %v\n", item.Name(), err) gen, err := LoadPlugin(path)
continue if err != nil {
} 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", item.Name()) // show the plugins found if verbose flag is set
} if params.GetVerbose() {
} fmt.Printf("-- found plugin '%s'\n", gen.GetName())
gens[gen.GetName()] = gen }
}
// 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 { if err != nil {
fmt.Printf("failed to load plugin: %v\n", err) return fmt.Errorf("failed to load generator in directory '%s': %w", path, err)
continue
} }
if verbose, ok := params["verbose"].(bool); ok {
if verbose { // show the templates loaded if verbose flag is set
fmt.Printf("-- found plugin '%s'\n", dirpath+item.Name()) 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 // 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(). // 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 // 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 // load jinja template from file
t, err := gonja.FromBytes(b) t, err := gonja.FromBytes(b)
if err != nil { 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 // execute/render jinja template
b := bytes.Buffer{} b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil { 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()) outputs = append(outputs, b.Bytes())
} }
@ -243,13 +348,13 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
// load jinja template from file // load jinja template from file
t, err := gonja.FromFile(path) t, err := gonja.FromFile(path)
if err != nil { 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 // execute/render jinja template
b := bytes.Buffer{} b := bytes.Buffer{}
if err = t.Execute(&b, data); err != nil { 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() outputs[path] = b.Bytes()
} }
@ -257,6 +362,23 @@ func ApplyTemplateFromFiles(mappings Mappings, paths ...string) (FileMap, error)
return outputs, nil 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 // 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 // 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() // 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) { func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, error) {
// load generator plugins to generate configs or to print // load generator plugins to generate configs or to print
var ( var (
generators = make(map[string]Generator) client = configurator.NewSmdClient(
client = configurator.NewSmdClient(
configurator.WithHost(config.SmdClient.Host), configurator.WithHost(config.SmdClient.Host),
configurator.WithPort(config.SmdClient.Port), configurator.WithPort(config.SmdClient.Port),
configurator.WithAccessToken(config.AccessToken), configurator.WithAccessToken(config.AccessToken),
@ -278,41 +399,32 @@ func GenerateWithTarget(config *configurator.Config, params Params) (FileMap, er
) )
) )
// load all plugins from supplied arguments // check if a target is supplied
for _, path := range params.PluginPaths { if len(params.Args) == 0 && params.Target == "" {
if params.Verbose { return nil, fmt.Errorf("must specify a target")
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)
} }
// copy all generators supplied from arguments // load target information from config
maps.Copy(generators, params.Generators) target, ok := config.Targets[params.Target]
if !ok {
return nil, fmt.Errorf("target not found in config")
}
// show available targets then exit // if plugin path specified from CLI, use that instead
if len(params.Args) == 0 && params.Target == "" { if params.PluginPath != "" {
for g := range generators { target.PluginPath = params.PluginPath
fmt.Printf("-- found generator plugin \"%s\"\n", g) }
}
return nil, nil // 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 // run the generator plugin from target passed
gen := generators[params.Target] return generator.Generate(
if gen == nil {
return nil, fmt.Errorf("invalid generator target (%s)", params.Target)
}
return gen.Generate(
config, config,
WithTarget(gen.GetName()), WithTarget(generator.GetName()),
WithClient(client), WithClient(client),
) )
} }

View file

@ -37,7 +37,7 @@ func (g *Dhcpd) Generate(config *configurator.Config, opts ...util.Option) (gene
if client != nil { if client != nil {
eths, err = client.FetchEthernetInterfaces(opts...) eths, err = client.FetchEthernetInterfaces(opts...)
if err != nil { 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)
} }
} }

View file

@ -11,7 +11,7 @@ type Params map[string]any
type Option func(Params) type Option func(Params)
// Extract all parameters from the options passed as map[string]any. // Extract all parameters from the options passed as map[string]any.
func GetParams(opts ...Option) Params { func ToDict(opts ...Option) Params {
params := Params{} params := Params{}
for _, opt := range opts { for _, opt := range opts {
opt(params) opt(params)
@ -45,8 +45,8 @@ func WithDefault[T any](v T) Option {
} }
} }
// Syntactic sugar generic function to get parameter from util.Params. // Sugary generic function to get parameter from util.Params.
func Get[T any](params Params, key string, opts ...Option) *T { func Get[T any](params Params, key string) *T {
if v, ok := params[key].(T); ok { if v, ok := params[key].(T); ok {
return &v return &v
} }
@ -55,3 +55,16 @@ func Get[T any](params Params, key string, opts ...Option) *T {
} }
return nil 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
}

View file

@ -2,12 +2,14 @@ package util
import ( import (
"bytes" "bytes"
"cmp"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"slices"
"strings" "strings"
) )
@ -28,7 +30,7 @@ func IsDirectory(path string) (bool, error) {
// This returns an *os.FileInfo type // This returns an *os.FileInfo type
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)
if err != nil { 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() // 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. // NOTE: This currently requires git to be installed.
// TODO: Change how this is done to not require executing a command. // TODO: Change how this is done to not require executing a command.
func GitCommit() string { func GitCommit() string {
c := exec.Command("git", "rev-parse", "HEAD") c := exec.Command("git", "rev-parse", "--short=8", "HEAD")
stdout, err := c.Output() stdout, err := c.Output()
if err != nil { if err != nil {
return "" return ""
@ -80,6 +82,11 @@ func RemoveIndex[T comparable](s []T, index int) []T {
return append(ret, s[index+1:]...) 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. // General function to copy elements from slice if condition is true.
func CopyIf[T comparable](s []T, condition func(t T) bool) []T { func CopyIf[T comparable](s []T, condition func(t T) bool) []T {
var f = make([]T, 0) var f = make([]T, 0)