feat: autoseal file in current dir

This commit is contained in:
Bastien Riviere 2023-09-03 18:28:42 +02:00
parent 1ac25d1792
commit 7525654ad3
Signed by: babariviere
GPG key ID: 4E5F0839249F162E

48
main.go
View file

@ -1,7 +1,51 @@
package main package main
import "fmt" import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() { 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)
}
}
} }