2021-01-20 14:34:40 +00:00
|
|
|
package applyconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2024-01-20 13:07:11 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-01-20 14:34:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2024-01-20 13:07:11 +00:00
|
|
|
"novit.tech/direktil/pkg/config"
|
|
|
|
"novit.tech/direktil/pkg/config/apply"
|
2021-01-20 14:34:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-20 13:07:11 +00:00
|
|
|
filters []string
|
|
|
|
pathPrefix string
|
2021-01-20 14:34:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Command() (c *cobra.Command) {
|
|
|
|
c = &cobra.Command{
|
|
|
|
Use: "apply-config <config.yaml>",
|
|
|
|
Short: "apply a config to the current system",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
|
|
|
Run: run,
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:51:44 +00:00
|
|
|
flag := c.Flags()
|
|
|
|
flag.StringArrayVarP(&filters, "filter", "F", []string{}, "glob filter to select files to apply")
|
2024-01-20 13:07:11 +00:00
|
|
|
flag.StringVarP(&pathPrefix, "prefix", "P", "", "path prefix")
|
2021-01-20 14:34:40 +00:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(_ *cobra.Command, args []string) {
|
2024-01-20 13:07:11 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
2021-01-20 14:34:40 +00:00
|
|
|
configPath := args[0]
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg *config.Config
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if configPath == "-" {
|
2024-01-20 13:07:11 +00:00
|
|
|
log.Info().Str("from", "stdin").Msg("loading config")
|
2021-01-20 14:34:40 +00:00
|
|
|
cfg, err = config.Read(os.Stdin)
|
|
|
|
|
|
|
|
} else {
|
2024-01-20 13:07:11 +00:00
|
|
|
log.Info().Str("from", configPath).Msg("loading config")
|
2021-01-20 14:34:40 +00:00
|
|
|
cfg, err = config.Load(configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-20 13:07:11 +00:00
|
|
|
log.Fatal().Err(err).Msg("failed to load config")
|
2021-01-20 14:34:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 13:07:11 +00:00
|
|
|
if err = apply.Files(cfg, pathPrefix, filters...); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("failed to apply files")
|
2021-01-20 14:34:40 +00:00
|
|
|
}
|
|
|
|
}
|