jx/cmd/cli/main.go

76 lines
1.9 KiB
Go
Raw Normal View History

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)
}
2024-04-09 19:30:05 +00:00
if validationErr := d.Validate(); validationErr != nil {
log.Fatal(validationErr)
}
2024-04-03 18:47:55 +00:00
if applyErr := d.Apply(); applyErr != nil {
log.Fatal(applyErr)
}
}
2024-03-20 19:23:31 +00:00
if *resourceUri != "" {
2024-04-03 19:27:16 +00:00
slog.Info("importing resource", "resource", *resourceUri)
if addResourceErr := d.AddResource(*resourceUri); addResourceErr != nil {
log.Fatal(addResourceErr)
}
2024-03-20 19:23:31 +00:00
}
2024-04-03 19:27:16 +00:00
if documentGenerateErr := d.Generate(os.Stdout); documentGenerateErr != nil {
log.Fatal(documentGenerateErr)
}
2024-03-20 19:23:31 +00:00
}