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.
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"fmt"
|
"errors"
|
||||||
|
_ "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)
|
||||||
@ -23,7 +26,6 @@ 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")
|
||||||
|
|
||||||
@ -49,10 +51,25 @@ func main() {
|
|||||||
log.Fatal(inputFileErr)
|
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)
|
slog.Info("loading resource document", "file", resourceFile)
|
||||||
if *file != "" {
|
|
||||||
if e := d.Load(resourceFile); e != nil {
|
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)
|
log.Fatal(e)
|
||||||
}
|
}
|
||||||
if validationErr := d.Validate(); validationErr != nil {
|
if validationErr := d.Validate(); validationErr != nil {
|
||||||
@ -61,15 +78,22 @@ func main() {
|
|||||||
if applyErr := d.Apply(); applyErr != nil {
|
if applyErr := d.Apply(); applyErr != nil {
|
||||||
log.Fatal(applyErr)
|
log.Fatal(applyErr)
|
||||||
}
|
}
|
||||||
}
|
documents = append(documents, resource.NewDocument())
|
||||||
if *resourceUri != "" {
|
index++
|
||||||
slog.Info("importing resource", "resource", *resourceUri)
|
|
||||||
if addResourceErr := d.AddResource(*resourceUri); addResourceErr != nil {
|
|
||||||
log.Fatal(addResourceErr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if documentGenerateErr := d.Generate(os.Stdout); documentGenerateErr != nil {
|
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)
|
log.Fatal(documentGenerateErr)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (d *Declaration) Resource() Resource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Declaration) SetURI(uri string) error {
|
func (d *Declaration) SetURI(uri string) error {
|
||||||
slog.Info("SetURI()", "uri", uri)
|
slog.Info("Declaration.SetURI()", "uri", uri, "declaration", d)
|
||||||
d.Attributes = NewResource(uri)
|
d.Attributes = NewResource(uri)
|
||||||
if d.Attributes == nil {
|
if d.Attributes == nil {
|
||||||
panic("unknown resource")
|
panic("unknown resource")
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||||||
|
|
||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -41,7 +42,6 @@ func init() {
|
|||||||
|
|
||||||
// Manage the state of file system objects
|
// Manage the state of file system objects
|
||||||
type File struct {
|
type File struct {
|
||||||
loader YamlLoader
|
|
||||||
Path string `json:"path" yaml:"path"`
|
Path string `json:"path" yaml:"path"`
|
||||||
Owner string `json:"owner" yaml:"owner"`
|
Owner string `json:"owner" yaml:"owner"`
|
||||||
Group string `json:"group" yaml:"group"`
|
Group string `json:"group" yaml:"group"`
|
||||||
@ -60,7 +60,9 @@ type File struct {
|
|||||||
func NewFile() *File {
|
func NewFile() *File {
|
||||||
currentUser, _ := user.Current()
|
currentUser, _ := user.Current()
|
||||||
group, _ := user.LookupGroupId(currentUser.Gid)
|
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 {
|
func (f *File) URI() string {
|
||||||
@ -152,8 +154,9 @@ func (f *File) Apply() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) LoadDecl(yamlFileResourceDeclaration string) error {
|
func (f *File) LoadDecl(yamlResourceDeclaration string) error {
|
||||||
return f.loader(yamlFileResourceDeclaration, f)
|
d := NewYAMLStringDecoder(yamlResourceDeclaration)
|
||||||
|
return d.Decode(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *File) ResolveId(ctx context.Context) string {
|
func (f *File) ResolveId(ctx context.Context) string {
|
||||||
@ -193,7 +196,7 @@ func (f *File) ReadStat() error {
|
|||||||
//panic(userErr)
|
//panic(userErr)
|
||||||
f.Owner = userId
|
f.Owner = userId
|
||||||
} else {
|
} else {
|
||||||
f.Owner = fileUser.Name
|
f.Owner = fileUser.Username
|
||||||
}
|
}
|
||||||
fileGroup, groupErr := user.LookupGroupId(groupId)
|
fileGroup, groupErr := user.LookupGroupId(groupId)
|
||||||
if groupErr != nil {
|
if groupErr != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user