jx/internal/resource/resource.go
2024-03-20 09:15:27 -07:00

58 lines
976 B
Go

package resource
import (
"gopkg.in/yaml.v3"
)
type Declaration struct {
Type string `yaml:"type"`
Attributes yaml.Node `yaml:"attributes"`
}
type Resource interface {
ResourceLoader
StateTransformer
}
type ResourceLoader interface {
LoadDecl(string) error
}
type StateTransformer interface {
Apply() error
}
type YamlLoader func(string, any) error
func YamlLoadDecl(yamlFileResourceDeclaration string, resource any) error {
if err := yaml.Unmarshal([]byte(yamlFileResourceDeclaration), resource); err != nil {
return err
}
return nil
}
type ResourceCreator interface {
Create() error
}
type ResourceReader interface {
Read() ([]byte, error)
}
type ResourceUpdater interface {
Update() error
}
type ResourceDeleter interface {
Delete() error
}
func NewDeclaration() *Declaration {
return &Declaration{}
}
func (d *Declaration) LoadDecl(yamlResourceDeclaration string) error {
return YamlLoadDecl(yamlResourceDeclaration, d)
}