add Delete method
Some checks failed
Lint / golangci-lint (push) Failing after 17s
Declarative Tests / test (push) Failing after 6s

This commit is contained in:
Matthew Rich 2024-10-09 22:31:39 +00:00
parent 2e84afa87f
commit 6925598bf2
11 changed files with 81 additions and 11 deletions

View File

@ -184,6 +184,24 @@ func TestResourcesRead(t *testing.T) {
t.Skip("cli not built") 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")) assert.Nil(t, TempDir.CreateFile("testread", "data"))
resources := fmt.Sprintf(` resources := fmt.Sprintf(`
@ -215,7 +233,7 @@ resources:
- type: http - type: http
transition: read transition: read
attributes: attributes:
endpoint: https://gitea.rosskeen.house endpoint: %s/resource/user
- type: route - type: route
transition: read transition: read
attributes: attributes:
@ -227,7 +245,7 @@ resources:
rtid: all rtid: all
routetype: local routetype: local
metric: 100 metric: 100
`, TempDir.FilePath("testread")) `, TempDir.FilePath("testread"), server.URL)
assert.Nil(t, TempDir.CreateFile("resources.jx.yaml", resources)) assert.Nil(t, TempDir.CreateFile("resources.jx.yaml", resources))

View File

@ -167,8 +167,8 @@ func main() {
for _, subCmd := range jxSubCommands { for _, subCmd := range jxSubCommands {
cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError) cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError)
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx", "Config file path") cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx/conf.d", "Config file path")
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx", "Config file path") cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx/conf.d", "Config file path")
GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format") GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format")
cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)") cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)")

26
cmd/cli/main_test.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. 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())
*/
}

View File

@ -1,8 +1,8 @@
resources: resources:
- type: file - type: file
transition: create
attributes: attributes:
path: /tmp/foo.txt path: /tmp/foo.txt
owner: nobody owner: nobody
group: nobody
mode: 0644 mode: 0644
state: present state: present

View File

@ -1,7 +1,7 @@
resources: resources:
- type: user - type: user
transition: create
attributes: attributes:
name: "testuser" name: "testuser"
uid: "12001" uid: "12001"
home: "/home/testuser" home: "/home/testuser"
state: present

View File

@ -183,7 +183,7 @@ func (a *App) Apply(ctx context.Context, deleteResources bool) (err error) {
continue 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 { if e := d.(*folio.Document).Apply(overrideState); e != nil {
slog.Info("Client.Apply() error", "error", e) slog.Info("Client.Apply() error", "error", e)
return e return e

View File

@ -10,6 +10,7 @@ import (
"decl/internal/data" "decl/internal/data"
"decl/internal/folio" "decl/internal/folio"
"runtime" "runtime"
"decl/internal/system"
) )
// Collects facts about the system // Collects facts about the system
@ -35,9 +36,21 @@ func NewSystem() *System {
for k, v := range buildValues { for k, v := range buildValues {
s[k] = v s[k] = v
} }
s.CurrentUser()
return &s 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 { func (s *System) URI() string {
return fmt.Sprintf("%s://%s", s.Type(), "") return fmt.Sprintf("%s://%s", s.Type(), "")
} }

View File

@ -120,8 +120,10 @@ func (j *JxFile) setdecoder(source data.ContentIdentifier) {
for _,v := range strings.Split(source.ContentType(), ".") { for _,v := range strings.Split(source.ContentType(), ".") {
_ = j.Format.Set(v) _ = j.Format.Set(v)
} }
slog.Info("JxFile.setdecoder()", "type", source.ContentType(), "format", j.Format)
j.decoder = codec.NewDecoder(j.reader, 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" } func (j *JxFile) Type() data.TypeName { return "jx" }
@ -179,6 +181,7 @@ func (j *JxFile) ExtractMany(resourceSource data.Resource, filter data.ElementSe
} }
break break
} }
slog.Info("JxFile.ExtractMany() loading", "document", j.index)
} }
slog.Info("JxFile.ExtractMany()", "jxfile", j, "error", err) slog.Info("JxFile.ExtractMany()", "jxfile", j, "error", err)
return return

View File

@ -245,7 +245,7 @@ func (d *Declaration) SetConfig(configDoc data.Document) {
return return
} }
if d.Config != "" { // XXX if d.Config != "" { // XXX
panic("failed setting config") panic(fmt.Sprintf("failed setting config: %s", d.Config))
} }
} }

View File

@ -99,6 +99,10 @@ func (d *Document) Set(key string, value any) {
d.uris.Set(key, value.(data.Declaration)) d.uris.Set(key, value.(data.Declaration))
} }
func (d *Document) Delete(key string) {
d.uris.Delete(key)
}
func (d *Document) GetResource(uri string) *Declaration { func (d *Document) GetResource(uri string) *Declaration {
if decl, ok := d.uris[uri]; ok { if decl, ok := d.uris[uri]; ok {
return decl.(*Declaration) return decl.(*Declaration)
@ -323,6 +327,10 @@ func (d *Document) MapResourceURI(uri string, declaration data.Declaration) {
d.uris[uri] = declaration d.uris[uri] = declaration
} }
func (d *Document) UnMapResourceURI(uri string) {
d.uris.Delete(uri)
}
func (d *Document) AddDeclaration(declaration data.Declaration) { func (d *Document) AddDeclaration(declaration data.Declaration) {
uri := declaration.URI() uri := declaration.URI()
decl := declaration.(*Declaration) decl := declaration.(*Declaration)

View File

@ -88,17 +88,17 @@ func (r *Registry) AppendParsedURI(uri *url.URL, documents []data.Document) (add
case data.ManyExtractor: case data.ManyExtractor:
var docs []data.Document var docs []data.Document
docs, err = extractor.ExtractMany(sourceResource, nil) 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...) documents = append(documents, docs...)
case data.Extractor: case data.Extractor:
var singleDocument data.Document var singleDocument data.Document
singleDocument, err = extractor.Extract(sourceResource, nil) 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) 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 addedDocuments = documents
return 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) slog.Info("folio.Registry.Append() - Extract", "uri", uri, "source", sourceResource, "doc", singleDocument, "error", err)
documents = append(documents, singleDocument) 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) slog.Info("folio.Registry.Append()", "uri", uri, "converter", r.ConverterTypes, "error", err)