add support for multiple document definitions
This commit is contained in:
parent
adea95972d
commit
0a6d1a93ee
@ -1,19 +1,22 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"flag"
|
||||
"log"
|
||||
"log/slog"
|
||||
"fmt"
|
||||
"errors"
|
||||
_ "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)
|
||||
@ -23,7 +26,6 @@ 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")
|
||||
|
||||
@ -49,27 +51,49 @@ func main() {
|
||||
log.Fatal(inputFileErr)
|
||||
}
|
||||
|
||||
d := resource.NewDocument()
|
||||
documents := make([]*resource.Document, 0, 100)
|
||||
decoder := resource.NewYAMLDecoder(resourceFile)
|
||||
encoder := resource.NewYAMLEncoder(os.Stdout)
|
||||
index := 0
|
||||
|
||||
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)
|
||||
|
||||
documents = append(documents, resource.NewDocument())
|
||||
if resourceFile != nil {
|
||||
for {
|
||||
d := documents[index]
|
||||
e := decoder.Decode(d)
|
||||
if errors.Is(e, io.EOF) {
|
||||
if len(documents) > 1 {
|
||||
documents[index] = nil
|
||||
}
|
||||
break
|
||||
}
|
||||
if e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
if validationErr := d.Validate(); validationErr != nil {
|
||||
log.Fatal(validationErr)
|
||||
}
|
||||
if applyErr := d.Apply(); applyErr != nil {
|
||||
log.Fatal(applyErr)
|
||||
}
|
||||
documents = append(documents, resource.NewDocument())
|
||||
index++
|
||||
}
|
||||
}
|
||||
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)
|
||||
for _,document := range documents {
|
||||
if document != nil {
|
||||
if *resourceUri != "" {
|
||||
slog.Info("importing resource", "resource", *resourceUri)
|
||||
if addResourceErr := document.AddResource(*resourceUri); addResourceErr != nil {
|
||||
log.Fatal(addResourceErr)
|
||||
}
|
||||
}
|
||||
if documentGenerateErr := encoder.Encode(document); documentGenerateErr != nil {
|
||||
log.Fatal(documentGenerateErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (d *Declaration) Resource() Resource {
|
||||
}
|
||||
|
||||
func (d *Declaration) SetURI(uri string) error {
|
||||
slog.Info("SetURI()", "uri", uri)
|
||||
slog.Info("Declaration.SetURI()", "uri", uri, "declaration", d)
|
||||
d.Attributes = NewResource(uri)
|
||||
if d.Attributes == nil {
|
||||
panic("unknown resource")
|
||||
|
@ -1,4 +1,5 @@
|
||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||
|
||||
package resource
|
||||
|
||||
import (
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io"
|
||||
"net/url"
|
||||
@ -41,7 +42,6 @@ func init() {
|
||||
|
||||
// Manage the state of file system objects
|
||||
type File struct {
|
||||
loader YamlLoader
|
||||
Path string `json:"path" yaml:"path"`
|
||||
Owner string `json:"owner" yaml:"owner"`
|
||||
Group string `json:"group" yaml:"group"`
|
||||
@ -60,7 +60,9 @@ type File struct {
|
||||
func NewFile() *File {
|
||||
currentUser, _ := user.Current()
|
||||
group, _ := user.LookupGroupId(currentUser.Gid)
|
||||
return &File{loader: YamlLoadDecl, Owner: currentUser.Username, Group: group.Name, Mode: "0666", FileType: RegularFile}
|
||||
f := &File{Owner: currentUser.Username, Group: group.Name, Mode: "0666", FileType: RegularFile}
|
||||
slog.Info("NewFile()", "file", f)
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *File) URI() string {
|
||||
@ -152,8 +154,9 @@ func (f *File) Apply() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *File) LoadDecl(yamlFileResourceDeclaration string) error {
|
||||
return f.loader(yamlFileResourceDeclaration, f)
|
||||
func (f *File) LoadDecl(yamlResourceDeclaration string) error {
|
||||
d := NewYAMLStringDecoder(yamlResourceDeclaration)
|
||||
return d.Decode(f)
|
||||
}
|
||||
|
||||
func (f *File) ResolveId(ctx context.Context) string {
|
||||
@ -193,7 +196,7 @@ func (f *File) ReadStat() error {
|
||||
//panic(userErr)
|
||||
f.Owner = userId
|
||||
} else {
|
||||
f.Owner = fileUser.Name
|
||||
f.Owner = fileUser.Username
|
||||
}
|
||||
fileGroup, groupErr := user.LookupGroupId(groupId)
|
||||
if groupErr != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user