add cli test
This commit is contained in:
parent
5a49359722
commit
f2e451e668
20
cli_test.go
Normal file
20
cli_test.go
Normal 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)
|
||||||
|
}
|
@ -5,14 +5,15 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
_ "fmt"
|
"fmt"
|
||||||
_ "gopkg.in/yaml.v3"
|
_ "gopkg.in/yaml.v3"
|
||||||
"decl/internal/resource"
|
"decl/internal/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Print("debugging\n")
|
||||||
var programLevel = new(slog.LevelVar)
|
var programLevel = new(slog.LevelVar)
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel}))
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel}))
|
||||||
slog.SetDefault(logger)
|
slog.SetDefault(logger)
|
||||||
@ -22,6 +23,7 @@ func main() {
|
|||||||
programLevel.Set(slog.LevelError)
|
programLevel.Set(slog.LevelError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Print("debugging: flags\n")
|
||||||
file := flag.String("resource-file", "", "Resource file path")
|
file := flag.String("resource-file", "", "Resource file path")
|
||||||
resourceUri := flag.String("import-resource", "", "Add an existing resource")
|
resourceUri := flag.String("import-resource", "", "Add an existing resource")
|
||||||
|
|
||||||
@ -30,31 +32,35 @@ func main() {
|
|||||||
var resourceFile *os.File
|
var resourceFile *os.File
|
||||||
var inputFileErr error
|
var inputFileErr error
|
||||||
|
|
||||||
if *file != "" {
|
slog.Info("args", "resource-file", *file, "import-resource", *resourceUri)
|
||||||
resourceFile,inputFileErr = os.Open(*file)
|
if *file == "-" {
|
||||||
} else {
|
|
||||||
if stdinInfo, stdinErr := os.Stdin.Stat(); stdinErr == nil {
|
if stdinInfo, stdinErr := os.Stdin.Stat(); stdinErr == nil {
|
||||||
if (stdinInfo.Mode() & os.ModeCharDevice) == 0 {
|
if (stdinInfo.Mode() & os.ModeCharDevice) == 0 {
|
||||||
resourceFile = os.Stdin
|
resourceFile = os.Stdin
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return
|
inputFileErr = stdinErr
|
||||||
}
|
}
|
||||||
|
} else if *file != "" {
|
||||||
|
resourceFile,inputFileErr = os.Open(*file)
|
||||||
}
|
}
|
||||||
|
|
||||||
if inputFileErr != nil {
|
if inputFileErr != nil {
|
||||||
log.Fatal(inputFileErr)
|
log.Fatal(inputFileErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
d := resource.NewDocument()
|
d := resource.NewDocument()
|
||||||
if e := d.Load(resourceFile); e != nil {
|
slog.Info("loading resource document", "file", resourceFile)
|
||||||
log.Fatal(e)
|
if *file != "" {
|
||||||
}
|
if e := d.Load(resourceFile); e != nil {
|
||||||
if applyErr := d.Apply(); applyErr != nil {
|
log.Fatal(e)
|
||||||
log.Fatal(applyErr)
|
}
|
||||||
}
|
if applyErr := d.Apply(); applyErr != nil {
|
||||||
|
log.Fatal(applyErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
if *resourceUri != "" {
|
if *resourceUri != "" {
|
||||||
|
slog.Info("importing resource: %s\n", *resourceUri)
|
||||||
d.AddResource(*resourceUri)
|
d.AddResource(*resourceUri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user