69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package source
|
||
|
|
||
|
import (
|
||
|
_ "context"
|
||
|
_ "encoding/json"
|
||
|
_ "fmt"
|
||
|
_ "gopkg.in/yaml.v3"
|
||
|
"net/url"
|
||
|
_ "path/filepath"
|
||
|
"decl/internal/resource"
|
||
|
_ "os"
|
||
|
_ "io"
|
||
|
"log/slog"
|
||
|
)
|
||
|
|
||
|
type User struct {
|
||
|
UserType resource.UserType `yaml:"type" json:"type"`
|
||
|
}
|
||
|
|
||
|
func NewUser() *User {
|
||
|
return &User{ UserType: resource.SystemUserType }
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
SourceTypes.Register([]string{"user"}, func(u *url.URL) DocSource {
|
||
|
userSource := NewUser()
|
||
|
userType := u.Query().Get("type")
|
||
|
if len(userType) > 0 {
|
||
|
userSource.UserType = resource.UserType(userType)
|
||
|
}
|
||
|
return userSource
|
||
|
})
|
||
|
|
||
|
}
|
||
|
|
||
|
func (u *User) Type() string { return "user" }
|
||
|
|
||
|
func (u *User) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
|
||
|
documents := make([]*resource.Document, 0, 100)
|
||
|
|
||
|
slog.Info("user source ExtractResources()", "user", u)
|
||
|
Users := make([]*resource.User, 0, 100)
|
||
|
cmd := u.UserType.NewReadUsersCommand()
|
||
|
if cmd == nil {
|
||
|
return documents, 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 documents, exErr
|
||
|
}
|
||
|
document := resource.NewDocument()
|
||
|
for _, usr := range Users {
|
||
|
if usr == nil {
|
||
|
usr = resource.NewUser()
|
||
|
}
|
||
|
usr.UserType = u.UserType
|
||
|
document.AddResourceDeclaration("user", usr)
|
||
|
}
|
||
|
documents = append(documents, document)
|
||
|
} else {
|
||
|
slog.Info("user source ExtractResources()", "output", out, "error", err)
|
||
|
return documents, err
|
||
|
}
|
||
|
return documents, nil
|
||
|
}
|