inits/pkg/cmd/applyconfig/applyconfig.go
2024-01-20 14:07:11 +01:00

62 lines
1.2 KiB
Go

package applyconfig
import (
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"novit.tech/direktil/pkg/config"
"novit.tech/direktil/pkg/config/apply"
)
var (
filters []string
pathPrefix string
)
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,
}
flag := c.Flags()
flag.StringArrayVarP(&filters, "filter", "F", []string{}, "glob filter to select files to apply")
flag.StringVarP(&pathPrefix, "prefix", "P", "", "path prefix")
return c
}
func run(_ *cobra.Command, args []string) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
configPath := args[0]
var (
cfg *config.Config
err error
)
if configPath == "-" {
log.Info().Str("from", "stdin").Msg("loading config")
cfg, err = config.Read(os.Stdin)
} else {
log.Info().Str("from", configPath).Msg("loading config")
cfg, err = config.Load(configPath)
}
if err != nil {
log.Fatal().Err(err).Msg("failed to load config")
}
if err = apply.Files(cfg, pathPrefix, filters...); err != nil {
log.Fatal().Err(err).Msg("failed to apply files")
}
}