jx/internal/fan/user.go

76 lines
1.8 KiB
Go
Raw Permalink Normal View History

2024-07-17 08:34:57 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package fan
2024-07-17 08:34:57 +00:00
import (
_ "context"
_ "encoding/json"
_ "fmt"
_ "gopkg.in/yaml.v3"
"net/url"
_ "path/filepath"
"decl/internal/resource"
"decl/internal/data"
"decl/internal/folio"
2024-07-17 08:34:57 +00:00
_ "os"
_ "io"
"log/slog"
)
type User struct {
UserType resource.UserType `yaml:"type" json:"type"`
}
func NewUser() *User {
return &User{ UserType: resource.SystemUserType }
}
func init() {
folio.DocumentRegistry.ConverterTypes.Register([]string{"user"}, func(u *url.URL) data.Converter {
2024-07-17 08:34:57 +00:00
userSource := NewUser()
userType := u.Query().Get("type")
if len(userType) > 0 {
userSource.UserType = resource.UserType(userType)
}
return userSource
})
}
func (u *User) Type() data.TypeName { return "user" }
2024-07-17 08:34:57 +00:00
func (u *User) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) {
slog.Info("user source Extract()", "user", u)
2024-07-17 08:34:57 +00:00
Users := make([]*resource.User, 0, 100)
cmd := u.UserType.NewReadUsersCommand()
if cmd == nil {
return document, resource.ErrUnsupportedUserType
2024-07-17 08:34:57 +00:00
}
if out, err := cmd.Execute(u); err == nil {
slog.Info("user source ExtractResources()", "output", out)
if exErr := cmd.Extractor(out, &Users); exErr != nil {
return document, exErr
2024-07-17 08:34:57 +00:00
}
document = folio.DocumentRegistry.NewDocument("user://-")
2024-07-17 08:34:57 +00:00
for _, usr := range Users {
if usr == nil {
usr = resource.NewUser()
}
usr.UserType = u.UserType
document.AddResourceDeclaration("user", usr)
}
} else {
slog.Info("user source Extract()", "output", out, "error", err)
return document, err
2024-07-17 08:34:57 +00:00
}
return document, nil
}
func (u *User) Emit(document data.Document, filter data.ElementSelector) (resourceTarget data.Resource, err error) {
return nil, data.ErrUnsupportedConversion
}
func (u *User) Close() error {
return nil
2024-07-17 08:34:57 +00:00
}