2024-03-20 19:23:31 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"flag"
|
|
|
|
"log"
|
2024-04-03 18:47:55 +00:00
|
|
|
"log/slog"
|
|
|
|
"fmt"
|
2024-03-20 19:23:31 +00:00
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
"decl/internal/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-04-03 18:47:55 +00:00
|
|
|
fmt.Print("debugging\n")
|
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
|
|
|
|
2024-04-03 18:47:55 +00:00
|
|
|
fmt.Print("debugging: flags\n")
|
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-03-20 19:23:31 +00:00
|
|
|
d := resource.NewDocument()
|
2024-04-03 18:47:55 +00:00
|
|
|
slog.Info("loading resource document", "file", resourceFile)
|
|
|
|
if *file != "" {
|
|
|
|
if e := d.Load(resourceFile); e != nil {
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
|
|
|
if applyErr := d.Apply(); applyErr != nil {
|
|
|
|
log.Fatal(applyErr)
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:23:31 +00:00
|
|
|
if *resourceUri != "" {
|
2024-04-03 18:47:55 +00:00
|
|
|
slog.Info("importing resource: %s\n", *resourceUri)
|
2024-03-20 19:23:31 +00:00
|
|
|
d.AddResource(*resourceUri)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Generate(os.Stdout)
|
|
|
|
}
|