From a00ee23b14b816e2655365354a1cf55722b4182d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Cluseau?= Date: Wed, 20 Jan 2021 15:34:40 +0100 Subject: [PATCH] dynlay --- cmd/dkl-apply-config/main.go | 53 ------------------------- cmd/dkl/main.go | 2 + modd.conf | 2 +- pkg/cmd/applyconfig/applyconfig.go | 63 ++++++++++++++++++++++++++++++ pkg/cmd/dynlay/dynlay.go | 3 +- 5 files changed, 68 insertions(+), 55 deletions(-) delete mode 100644 cmd/dkl-apply-config/main.go create mode 100644 pkg/cmd/applyconfig/applyconfig.go diff --git a/cmd/dkl-apply-config/main.go b/cmd/dkl-apply-config/main.go deleted file mode 100644 index 9c49894..0000000 --- a/cmd/dkl-apply-config/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package main - -import ( - "flag" - "os" - "strings" - - "novit.nc/direktil/inits/pkg/apply" - "novit.nc/direktil/pkg/config" - dlog "novit.nc/direktil/pkg/log" -) - -var ( - log = dlog.Get("dkl-apply-config") -) - -func main() { - configPath := flag.String("config", "config.yaml", "config to load (\"-\" for stdin)") - doFiles := flag.Bool("files", false, "apply files") - filesFilters := flag.String("files-filters", "", "comma-separated filters to select files to apply") - flag.Parse() - - log.SetConsole(os.Stderr) - - var ( - cfg *config.Config - err error - ) - - if *configPath == "-" { - log.Print("loading config from stdin") - cfg, err = config.Read(os.Stdin) - - } else { - log.Print("loading config from ", *configPath) - cfg, err = config.Load(*configPath) - } - - if err != nil { - log.Print("failed to load config: ", err) - } - - if *doFiles { - filters := []string{} - if *filesFilters != "" { - filters = strings.Split(*filesFilters, ",") - } - if err = apply.Files(cfg /*log,*/, filters...); err != nil { - log.Taint(dlog.Fatal, "failed to apply files: ", err) - os.Exit(1) - } - } -} diff --git a/cmd/dkl/main.go b/cmd/dkl/main.go index 6a96c3a..bb1292c 100644 --- a/cmd/dkl/main.go +++ b/cmd/dkl/main.go @@ -5,6 +5,7 @@ import ( "github.com/spf13/cobra" + "novit.nc/direktil/inits/pkg/cmd/applyconfig" cmddynlay "novit.nc/direktil/inits/pkg/cmd/dynlay" cmdinit "novit.nc/direktil/inits/pkg/cmd/init" ) @@ -14,6 +15,7 @@ func main() { root.AddCommand(cmdinit.Command()) root.AddCommand(cmddynlay.Command()) + root.AddCommand(applyconfig.Command()) if err := root.Execute(); err != nil { log.Fatal("error: ", err) diff --git a/modd.conf b/modd.conf index 767e5c8..c8b4b94 100644 --- a/modd.conf +++ b/modd.conf @@ -6,7 +6,7 @@ modd.conf {} prep: CGO_ENABLED=0 go build -o dist -trimpath ./cmd/... prep: rsync -za --stats dist/dkl bw:/usr/local/bin/ - prep: ssh bw dkl dynlay --url-prefix https://dkl.nwrk.info/dist/layers kubernetes v1.19.4_containerd.1.4.1 + prep: ssh bw dkl apply-config config.yaml #prep: ./update-test-data #daemon: ./test-vm 1 diff --git a/pkg/cmd/applyconfig/applyconfig.go b/pkg/cmd/applyconfig/applyconfig.go new file mode 100644 index 0000000..fe7e4f2 --- /dev/null +++ b/pkg/cmd/applyconfig/applyconfig.go @@ -0,0 +1,63 @@ +package applyconfig + +import ( + "flag" + "os" + "strings" + + "github.com/spf13/cobra" + "novit.nc/direktil/inits/pkg/apply" + "novit.nc/direktil/pkg/config" + + dlog "novit.nc/direktil/pkg/log" +) + +var ( + filesFilters string + log = dlog.Get("dkl") +) + +func Command() (c *cobra.Command) { + c = &cobra.Command{ + Use: "apply-config ", + Short: "apply a config to the current system", + Args: cobra.ExactArgs(1), + + Run: run, + } + + flag.StringVar(&filesFilters, "files-filters", "", "comma-separated filters to select files to apply") + + return c +} + +func run(_ *cobra.Command, args []string) { + configPath := args[0] + + var ( + cfg *config.Config + err error + ) + + if configPath == "-" { + log.Print("loading config from stdin") + cfg, err = config.Read(os.Stdin) + + } else { + log.Print("loading config from ", configPath) + cfg, err = config.Load(configPath) + } + + if err != nil { + log.Print("failed to load config: ", err) + } + + filters := []string{} + if filesFilters != "" { + filters = strings.Split(filesFilters, ",") + } + if err = apply.Files(cfg /*log,*/, filters...); err != nil { + log.Taint(dlog.Fatal, "failed to apply files: ", err) + os.Exit(1) + } +} diff --git a/pkg/cmd/dynlay/dynlay.go b/pkg/cmd/dynlay/dynlay.go index 6d354f2..cc76af6 100644 --- a/pkg/cmd/dynlay/dynlay.go +++ b/pkg/cmd/dynlay/dynlay.go @@ -80,9 +80,10 @@ func run(_ *cobra.Command, args []string) { for _, path := range paths { existing[path] = true + target := "/" + path + fmt.Println("linking", path) - target := "/" + path os.Remove(target) os.MkdirAll(filepath.Dir(target), 0755)