From 7525654ad3b1ed5cb6b81a79cfe2d1c826b9d6b9 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Sun, 3 Sep 2023 18:28:42 +0200 Subject: [PATCH] feat: autoseal file in current dir --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index c048119..884eeb2 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,51 @@ package main -import "fmt" +import ( + "fmt" + "log" + "os" + "os/exec" + "path/filepath" + "strings" +) func main() { - fmt.Println("hello world") + entries, err := os.ReadDir("./") + if err != nil { + log.Fatal("unable to read current directory:", err) + } + + for _, e := range entries { + name := e.Name() + ext := filepath.Ext(name) + nameNoExt := name[0 : len(name)-len(ext)] + if !strings.HasSuffix(nameNoExt, ".sealme") { + continue + } + naked := nameNoExt[0 : len(nameNoExt)-len(".sealme")] + + out := naked + ext + fmt.Println("sealing", name, "to", out) + cmd := exec.Command("kubeseal", "-o", "yaml", "--controller-namespace=flux-system") + + inf, err := os.Open(name) + if err != nil { + log.Println("failed to open", name+":", err) + continue + } + defer inf.Close() + cmd.Stdin = inf + + outf, err := os.Create(out) + if err != nil { + log.Println("failed to create", name+":", err) + continue + } + defer outf.Close() + cmd.Stdout = outf + + if err = cmd.Run(); err != nil { + log.Fatal(err) + } + } }