// Copyright 2024 Matthew Rich . All rights reserved. package fan import ( _ "context" _ "encoding/json" _ "fmt" _ "gopkg.in/yaml.v3" "net/url" _ "path/filepath" "decl/internal/resource" "decl/internal/data" "decl/internal/folio" _ "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 { 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" } func (u *User) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) { slog.Info("user source Extract()", "user", u) Users := make([]*resource.User, 0, 100) cmd := u.UserType.NewReadUsersCommand() if cmd == nil { return document, resource.ErrUnsupportedUserType } 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 } document = folio.DocumentRegistry.NewDocument("user://-") 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 } 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 }