2024-03-20 19:23:31 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-03-20 19:23:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-12 16:08:08 +00:00
|
|
|
"io"
|
2024-03-20 19:23:31 +00:00
|
|
|
"os"
|
|
|
|
"flag"
|
|
|
|
"log"
|
2024-04-03 18:47:55 +00:00
|
|
|
"log/slog"
|
2024-04-12 16:08:08 +00:00
|
|
|
"errors"
|
|
|
|
_ "fmt"
|
2024-03-20 19:23:31 +00:00
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
"decl/internal/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-03-27 21:13:33 +00:00
|
|
|
var programLevel = new(slog.LevelVar)
|
|
|
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel}))
|
|
|
|
slog.SetDefault(logger)
|
|
|
|
if debugLogging,ok := os.LookupEnv("DECL_DEBUG"); ok && debugLogging != "" {
|
|
|
|
programLevel.Set(slog.LevelDebug)
|
|
|
|
} else {
|
|
|
|
programLevel.Set(slog.LevelError)
|
|
|
|
}
|
2024-03-20 19:23:31 +00:00
|
|
|
|
|
|
|
file := flag.String("resource-file", "", "Resource file path")
|
|
|
|
resourceUri := flag.String("import-resource", "", "Add an existing resource")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
var resourceFile *os.File
|
|
|
|
var inputFileErr error
|
|
|
|
|
2024-04-03 18:47:55 +00:00
|
|
|
slog.Info("args", "resource-file", *file, "import-resource", *resourceUri)
|
|
|
|
if *file == "-" {
|
2024-03-20 19:23:31 +00:00
|
|
|
if stdinInfo, stdinErr := os.Stdin.Stat(); stdinErr == nil {
|
|
|
|
if (stdinInfo.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
resourceFile = os.Stdin
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-03 18:47:55 +00:00
|
|
|
inputFileErr = stdinErr
|
2024-03-20 19:23:31 +00:00
|
|
|
}
|
2024-04-03 18:47:55 +00:00
|
|
|
} else if *file != "" {
|
|
|
|
resourceFile,inputFileErr = os.Open(*file)
|
2024-03-20 19:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if inputFileErr != nil {
|
|
|
|
log.Fatal(inputFileErr)
|
|
|
|
}
|
2024-04-03 18:47:55 +00:00
|
|
|
|
2024-04-12 16:08:08 +00:00
|
|
|
documents := make([]*resource.Document, 0, 100)
|
|
|
|
decoder := resource.NewYAMLDecoder(resourceFile)
|
|
|
|
encoder := resource.NewYAMLEncoder(os.Stdout)
|
|
|
|
index := 0
|
|
|
|
|
2024-04-03 18:47:55 +00:00
|
|
|
slog.Info("loading resource document", "file", resourceFile)
|
2024-04-12 16:08:08 +00:00
|
|
|
|
|
|
|
documents = append(documents, resource.NewDocument())
|
|
|
|
if resourceFile != nil {
|
|
|
|
for {
|
|
|
|
d := documents[index]
|
|
|
|
e := decoder.Decode(d)
|
|
|
|
if errors.Is(e, io.EOF) {
|
|
|
|
if len(documents) > 1 {
|
|
|
|
documents[index] = nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
|
|
|
if validationErr := d.Validate(); validationErr != nil {
|
|
|
|
log.Fatal(validationErr)
|
|
|
|
}
|
|
|
|
if applyErr := d.Apply(); applyErr != nil {
|
|
|
|
log.Fatal(applyErr)
|
|
|
|
}
|
|
|
|
documents = append(documents, resource.NewDocument())
|
|
|
|
index++
|
2024-04-03 18:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:23:31 +00:00
|
|
|
|
2024-04-12 16:08:08 +00:00
|
|
|
for _,document := range documents {
|
|
|
|
if document != nil {
|
|
|
|
if *resourceUri != "" {
|
|
|
|
slog.Info("importing resource", "resource", *resourceUri)
|
|
|
|
if addResourceErr := document.AddResource(*resourceUri); addResourceErr != nil {
|
|
|
|
log.Fatal(addResourceErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if documentGenerateErr := encoder.Encode(document); documentGenerateErr != nil {
|
|
|
|
log.Fatal(documentGenerateErr)
|
|
|
|
}
|
|
|
|
}
|
2024-04-03 19:27:16 +00:00
|
|
|
}
|
2024-03-20 19:23:31 +00:00
|
|
|
}
|