From 0a6d1a93eec0bbe9ff1b68df354407b4a428df51 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Fri, 12 Apr 2024 09:08:08 -0700 Subject: [PATCH] add support for multiple document definitions --- cmd/cli/main.go | 66 ++++++++++++++++++++---------- internal/resource/declaration.go | 2 +- internal/resource/document_test.go | 1 + internal/resource/file.go | 13 +++--- 4 files changed, 55 insertions(+), 27 deletions(-) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 1b1b4a0..03bf693 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -1,19 +1,22 @@ // Copyright 2024 Matthew Rich . 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) + } + } } } diff --git a/internal/resource/declaration.go b/internal/resource/declaration.go index 27e4de2..a3b7da3 100644 --- a/internal/resource/declaration.go +++ b/internal/resource/declaration.go @@ -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") diff --git a/internal/resource/document_test.go b/internal/resource/document_test.go index 24de434..32103dd 100644 --- a/internal/resource/document_test.go +++ b/internal/resource/document_test.go @@ -1,4 +1,5 @@ // Copyright 2024 Matthew Rich . All rights reserved. + package resource import ( diff --git a/internal/resource/file.go b/internal/resource/file.go index 2e473d9..3f1d5be 100644 --- a/internal/resource/file.go +++ b/internal/resource/file.go @@ -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 {