add cli test
Some checks failed
Lint / golangci-lint (push) Failing after 10m2s
Declarative Tests / test (push) Failing after 41s

This commit is contained in:
Matthew Rich 2024-04-03 11:47:55 -07:00
parent 5a49359722
commit f2e451e668
2 changed files with 40 additions and 14 deletions

20
cli_test.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package main
import (
"github.com/stretchr/testify/assert"
"os"
"os/exec"
"testing"
"errors"
)
func TestCli(t *testing.T) {
if _, e := os.Stat("./decl"); errors.Is(e, os.ErrNotExist) {
t.Skip("cli not built")
}
yaml, cliErr := exec.Command("./decl", "-import-resource", "file://decl").Output()
assert.Nil(t, cliErr)
assert.Greater(t, len(yaml), 0)
}

View File

@ -6,13 +6,14 @@ import (
"flag"
"log"
"log/slog"
_ "fmt"
"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)
@ -22,6 +23,7 @@ func main() {
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")
@ -30,16 +32,17 @@ func main() {
var resourceFile *os.File
var inputFileErr error
if *file != "" {
resourceFile,inputFileErr = os.Open(*file)
} else {
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 {
return
inputFileErr = stdinErr
}
} else if *file != "" {
resourceFile,inputFileErr = os.Open(*file)
}
if inputFileErr != nil {
@ -47,14 +50,17 @@ func main() {
}
d := resource.NewDocument()
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)
}
}
if *resourceUri != "" {
slog.Info("importing resource: %s\n", *resourceUri)
d.AddResource(*resourceUri)
}