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")
}
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))

View File

@ -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)")

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:
- type: file
transition: create
attributes:
path: /tmp/foo.txt
owner: nobody
group: nobody
mode: 0644
state: present

View File

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

View File

@ -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

View File

@ -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(), "")
}

View File

@ -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

View File

@ -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))
}
}

View File

@ -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)

View File

@ -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)