jx/cmd/cli/main.go
Matthew Rich 33dd463180
Some checks failed
Lint / golangci-lint (push) Failing after 9m53s
Declarative Tests / test (push) Failing after 58s
update resource schemas
2024-04-09 12:30:05 -07:00

76 lines
1.9 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package main
import (
"os"
"flag"
"log"
"log/slog"
"fmt"
_ "gopkg.in/yaml.v3"
"decl/internal/resource"
)
func main() {
fmt.Print("debugging\n")
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)
}
fmt.Print("debugging: flags\n")
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
slog.Info("args", "resource-file", *file, "import-resource", *resourceUri)
if *file == "-" {
if stdinInfo, stdinErr := os.Stdin.Stat(); stdinErr == nil {
if (stdinInfo.Mode() & os.ModeCharDevice) == 0 {
resourceFile = os.Stdin
}
} else {
inputFileErr = stdinErr
}
} else if *file != "" {
resourceFile,inputFileErr = os.Open(*file)
}
if inputFileErr != nil {
log.Fatal(inputFileErr)
}
d := resource.NewDocument()
slog.Info("loading resource document", "file", resourceFile)
if *file != "" {
if e := d.Load(resourceFile); e != nil {
log.Fatal(e)
}
if validationErr := d.Validate(); validationErr != nil {
log.Fatal(validationErr)
}
if applyErr := d.Apply(); applyErr != nil {
log.Fatal(applyErr)
}
}
if *resourceUri != "" {
slog.Info("importing resource", "resource", *resourceUri)
if addResourceErr := d.AddResource(*resourceUri); addResourceErr != nil {
log.Fatal(addResourceErr)
}
}
if documentGenerateErr := d.Generate(os.Stdout); documentGenerateErr != nil {
log.Fatal(documentGenerateErr)
}
}