// Copyright 2024 Matthew Rich . All rights reserved. package resource import ( "context" "fmt" "gopkg.in/yaml.v3" _ "log" "net/url" _ "os" _ "os/exec" "io" "path/filepath" _ "strings" ) type Package struct { Name string `yaml:"name" json:"name"` Version string `yaml:"version" json:"version"` // state attributes State string `yaml:"state"` } func init() { ResourceTypes.Register("package", func(u *url.URL) Resource { p := NewPackage() return p }) } func NewPackage() *Package { return &Package{} } func (p *Package) URI() string { return fmt.Sprintf("package://%s?version=%s", p.Name, p.Version) } func (p *Package) SetURI(uri string) error { resourceUri, e := url.Parse(uri) if e == nil { if resourceUri.Scheme == "package" { p.Name = filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI()) p.Version = resourceUri.Query().Get("version") if p.Version == "" { p.Version = "latest" } } else { e = fmt.Errorf("%w: %s is not a package resource ", ErrInvalidResourceURI, uri) } } return e } func (p *Package) ResolveId(ctx context.Context) string { return "" } func (p *Package) Apply() error { return nil } func (p *Package) Load(r io.Reader) error { c := NewYAMLDecoder(r) return c.Decode(p) } func (p *Package) LoadDecl(yamlResourceDeclaration string) error { c := NewYAMLStringDecoder(yamlResourceDeclaration) return c.Decode(p) } func (p *Package) Type() string { return "package" } func (p *Package) Read(ctx context.Context) ([]byte, error) { return yaml.Marshal(p) } func NewApkCreateCommand() *Command { c := NewCommand() c.Path = "apk" c.Args = []CommandArg{ CommandArg("add"), CommandArg("{{ .Name }}={{ .Version }}"), } return c } func NewApkReadCommand() *Command { c := NewCommand() c.Path = "apk" c.Args = []CommandArg{ CommandArg("info"), CommandArg("-ev"), CommandArg("{{ .Name }}"), } return c } func NewApkDeleteCommand() *Command { c := NewCommand() c.Path = "apk" c.Args = []CommandArg{ CommandArg("del"), CommandArg("{{ .Name }}"), } return c }