From 6925598bf2ac2d224ed2ff2b85cb216964624aa7 Mon Sep 17 00:00:00 2001 From: Matthew Rich Date: Wed, 9 Oct 2024 22:31:39 +0000 Subject: [PATCH] add Delete method --- cli_test.go | 22 ++++++++++++++++++++-- cmd/cli/main.go | 4 ++-- cmd/cli/main_test.go | 26 ++++++++++++++++++++++++++ examples/file.jx.yaml | 2 +- examples/user.jx.yaml | 2 +- internal/client/client.go | 2 +- internal/config/system.go | 13 +++++++++++++ internal/fan/jx.go | 3 +++ internal/folio/declaration.go | 2 +- internal/folio/document.go | 8 ++++++++ internal/folio/registry.go | 8 +++++--- 11 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 cmd/cli/main_test.go diff --git a/cli_test.go b/cli_test.go index 9957808..89f7636 100644 --- a/cli_test.go +++ b/cli_test.go @@ -184,6 +184,24 @@ func TestResourcesRead(t *testing.T) { t.Skip("cli not built") } + + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + defer req.Body.Close() + assert.Equal(t, req.URL.String(), "/resource/user") + _, err := io.ReadAll(req.Body) + assert.Nil(t, err) + userdecl := []byte(` +type: "user" +attributes: + name: "foo" + gecos: "foo user" +`) + + rw.Write(userdecl) + })) + defer server.Close() + + assert.Nil(t, TempDir.CreateFile("testread", "data")) resources := fmt.Sprintf(` @@ -215,7 +233,7 @@ resources: - type: http transition: read attributes: - endpoint: https://gitea.rosskeen.house + endpoint: %s/resource/user - type: route transition: read attributes: @@ -227,7 +245,7 @@ resources: rtid: all routetype: local metric: 100 -`, TempDir.FilePath("testread")) +`, TempDir.FilePath("testread"), server.URL) assert.Nil(t, TempDir.CreateFile("resources.jx.yaml", resources)) diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 180836b..33b2c6e 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -167,8 +167,8 @@ func main() { for _, subCmd := range jxSubCommands { cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError) - cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx", "Config file path") - cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx", "Config file path") + cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx/conf.d", "Config file path") + cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx/conf.d", "Config file path") GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format") cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)") diff --git a/cmd/cli/main_test.go b/cmd/cli/main_test.go new file mode 100644 index 0000000..ea60e6c --- /dev/null +++ b/cmd/cli/main_test.go @@ -0,0 +1,26 @@ +// Copyright 2024 Matthew Rich . All rights reserved. + +package main + +import ( +_ "github.com/stretchr/testify/assert" + "testing" +_ "decl/internal/folio" +_ "decl/internal/data" +_ "log/slog" +) + +func TestLoadSourceURIConverter(t *testing.T) { +/* + var uri folio.URI = "file://../../examples/file.jx.yaml" + docs, err := LoadSourceURIConverter(uri) + assert.Nil(t, err) + assert.Greater(t, len(docs), 0) + slog.Info("TestLoadSourceURIConverter", "doc", docs[0], "resource", docs[0].(*folio.Document).ResourceDeclarations[0].Attributes) + resDecl := docs[0].(*folio.Document).ResourceDeclarations[0] + assert.Equal(t, "file", resDecl.Attributes.Type()) + v, ok := docs[0].Get("file:///tmp/foo.txt") + assert.True(t, ok) + assert.Equal(t, "/tmp/foo.txt", v.(data.Declaration).Resource().(data.FileResource).FilePath()) +*/ +} diff --git a/examples/file.jx.yaml b/examples/file.jx.yaml index 336327a..03e62b8 100644 --- a/examples/file.jx.yaml +++ b/examples/file.jx.yaml @@ -1,8 +1,8 @@ resources: - type: file + transition: create attributes: path: /tmp/foo.txt owner: nobody - group: nobody mode: 0644 state: present diff --git a/examples/user.jx.yaml b/examples/user.jx.yaml index 14e3a42..21da603 100644 --- a/examples/user.jx.yaml +++ b/examples/user.jx.yaml @@ -1,7 +1,7 @@ resources: - type: user + transition: create attributes: name: "testuser" uid: "12001" home: "/home/testuser" - state: present diff --git a/internal/client/client.go b/internal/client/client.go index d003937..c94a933 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -183,7 +183,7 @@ func (a *App) Apply(ctx context.Context, deleteResources bool) (err error) { continue } - slog.Info("Client.Apply()", "document", d, "state", overrideState, "error", err) + slog.Info("Client.Apply()", "uri", d.GetURI(), "document", d, "state", overrideState, "error", err) if e := d.(*folio.Document).Apply(overrideState); e != nil { slog.Info("Client.Apply() error", "error", e) return e diff --git a/internal/config/system.go b/internal/config/system.go index 4a78016..885c08b 100644 --- a/internal/config/system.go +++ b/internal/config/system.go @@ -10,6 +10,7 @@ import ( "decl/internal/data" "decl/internal/folio" "runtime" + "decl/internal/system" ) // Collects facts about the system @@ -35,9 +36,21 @@ func NewSystem() *System { for k, v := range buildValues { s[k] = v } + s.CurrentUser() return &s } +func (s *System) CurrentUser() { + processUser := system.ProcessUser() + processGroup := system.ProcessGroup(processUser) + (*s)["user"] = processUser.Username + (*s)["gecos"] = processUser.Name + (*s)["home"] = processUser.HomeDir + (*s)["uid"] = processUser.Uid + (*s)["group"] = processGroup.Name + (*s)["gid"] = processUser.Gid +} + func (s *System) URI() string { return fmt.Sprintf("%s://%s", s.Type(), "") } diff --git a/internal/fan/jx.go b/internal/fan/jx.go index 1e4065c..06f3a96 100644 --- a/internal/fan/jx.go +++ b/internal/fan/jx.go @@ -120,8 +120,10 @@ func (j *JxFile) setdecoder(source data.ContentIdentifier) { for _,v := range strings.Split(source.ContentType(), ".") { _ = j.Format.Set(v) } + slog.Info("JxFile.setdecoder()", "type", source.ContentType(), "format", j.Format) j.decoder = codec.NewDecoder(j.reader, j.Format) } + slog.Info("JxFile.setdecoder()", "decoder", j.decoder) } func (j *JxFile) Type() data.TypeName { return "jx" } @@ -179,6 +181,7 @@ func (j *JxFile) ExtractMany(resourceSource data.Resource, filter data.ElementSe } break } + slog.Info("JxFile.ExtractMany() loading", "document", j.index) } slog.Info("JxFile.ExtractMany()", "jxfile", j, "error", err) return diff --git a/internal/folio/declaration.go b/internal/folio/declaration.go index 4248123..99609d9 100644 --- a/internal/folio/declaration.go +++ b/internal/folio/declaration.go @@ -245,7 +245,7 @@ func (d *Declaration) SetConfig(configDoc data.Document) { return } if d.Config != "" { // XXX - panic("failed setting config") + panic(fmt.Sprintf("failed setting config: %s", d.Config)) } } diff --git a/internal/folio/document.go b/internal/folio/document.go index 7dc9638..19fcae1 100644 --- a/internal/folio/document.go +++ b/internal/folio/document.go @@ -99,6 +99,10 @@ func (d *Document) Set(key string, value any) { d.uris.Set(key, value.(data.Declaration)) } +func (d *Document) Delete(key string) { + d.uris.Delete(key) +} + func (d *Document) GetResource(uri string) *Declaration { if decl, ok := d.uris[uri]; ok { return decl.(*Declaration) @@ -323,6 +327,10 @@ func (d *Document) MapResourceURI(uri string, declaration data.Declaration) { d.uris[uri] = declaration } +func (d *Document) UnMapResourceURI(uri string) { + d.uris.Delete(uri) +} + func (d *Document) AddDeclaration(declaration data.Declaration) { uri := declaration.URI() decl := declaration.(*Declaration) diff --git a/internal/folio/registry.go b/internal/folio/registry.go index 145e253..3f92567 100644 --- a/internal/folio/registry.go +++ b/internal/folio/registry.go @@ -88,17 +88,17 @@ func (r *Registry) AppendParsedURI(uri *url.URL, documents []data.Document) (add case data.ManyExtractor: var docs []data.Document docs, err = extractor.ExtractMany(sourceResource, nil) - slog.Info("folio.Registry.Append() - ExtractMany", "uri", uri, "source", sourceResource, "docs", docs, "error", err) + slog.Info("folio.Registry.AppendParsedURI() - ExtractMany", "uri", uri, "source", sourceResource, "docs", docs, "error", err) documents = append(documents, docs...) case data.Extractor: var singleDocument data.Document singleDocument, err = extractor.Extract(sourceResource, nil) - slog.Info("folio.Registry.Append() - Extract", "uri", uri, "source", sourceResource, "doc", singleDocument, "error", err) + slog.Info("folio.Registry.AppendParsedURI() - Extract", "uri", uri, "source", sourceResource, "doc", singleDocument, "error", err) documents = append(documents, singleDocument) } } } - slog.Info("folio.Registry.Append()", "uri", uri, "converter", r.ConverterTypes, "error", err) + slog.Info("folio.Registry.AppendParsedURI()", "uri", uri, "converter", r.ConverterTypes, "error", err) addedDocuments = documents return } @@ -130,6 +130,8 @@ func (r *Registry) Append(uri URI, documents []data.Document) (addedDocuments [] slog.Info("folio.Registry.Append() - Extract", "uri", uri, "source", sourceResource, "doc", singleDocument, "error", err) documents = append(documents, singleDocument) } + } else { + slog.Warn("folio.Registry.Append(): failed loading extractor as resource") } } slog.Info("folio.Registry.Append()", "uri", uri, "converter", r.ConverterTypes, "error", err)