2024-03-25 20:27:30 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2024-03-25 20:31:06 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
_ "log"
|
|
|
|
"net/url"
|
|
|
|
_ "os"
|
|
|
|
_ "os/exec"
|
2024-03-27 21:14:40 +00:00
|
|
|
"path/filepath"
|
2024-04-21 06:13:17 +00:00
|
|
|
_ "strings"
|
|
|
|
"io"
|
2024-05-06 00:48:54 +00:00
|
|
|
"gitea.rosskeen.house/rosskeen.house/machine"
|
2024-03-25 20:27:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Exec struct {
|
2024-04-05 17:22:17 +00:00
|
|
|
Id string `yaml:"id" json:"id"`
|
|
|
|
CreateTemplate Command `yaml:"create" json:"create"`
|
|
|
|
ReadTemplate Command `yaml:"read" json:"read"`
|
|
|
|
UpdateTemplate Command `yaml:"update" json:"update"`
|
|
|
|
DeleteTemplate Command `yaml:"delete" json:"delete"`
|
2024-03-25 20:27:30 +00:00
|
|
|
|
2024-03-25 20:31:06 +00:00
|
|
|
// state attributes
|
|
|
|
State string `yaml:"state"`
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2024-03-25 20:31:06 +00:00
|
|
|
ResourceTypes.Register("exec", func(u *url.URL) Resource {
|
|
|
|
x := NewExec()
|
|
|
|
return x
|
|
|
|
})
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewExec() *Exec {
|
2024-04-21 06:13:17 +00:00
|
|
|
return &Exec{}
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
func (x *Exec) Clone() Resource {
|
|
|
|
return &Exec {
|
|
|
|
Id: x.Id,
|
|
|
|
CreateTemplate: x.CreateTemplate,
|
|
|
|
ReadTemplate: x.ReadTemplate,
|
|
|
|
UpdateTemplate: x.UpdateTemplate,
|
|
|
|
DeleteTemplate: x.DeleteTemplate,
|
|
|
|
State: x.State,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 00:48:54 +00:00
|
|
|
func (x *Exec) StateMachine() machine.Stater {
|
|
|
|
return ProcessMachine()
|
|
|
|
}
|
|
|
|
|
2024-03-25 20:27:30 +00:00
|
|
|
func (x *Exec) URI() string {
|
2024-03-25 20:31:06 +00:00
|
|
|
return fmt.Sprintf("exec://%s", x.Id)
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Exec) SetURI(uri string) error {
|
2024-03-27 21:14:40 +00:00
|
|
|
resourceUri, e := url.Parse(uri)
|
2024-04-05 17:22:17 +00:00
|
|
|
if e == nil {
|
|
|
|
if resourceUri.Scheme == "exec" {
|
|
|
|
x.Id = filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI())
|
|
|
|
} else {
|
|
|
|
e = fmt.Errorf("%w: %s is not an exec resource ", ErrInvalidResourceURI, uri)
|
|
|
|
}
|
2024-03-27 21:14:40 +00:00
|
|
|
}
|
|
|
|
return e
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Exec) ResolveId(ctx context.Context) string {
|
2024-03-25 20:31:06 +00:00
|
|
|
return ""
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 19:30:05 +00:00
|
|
|
func (x *Exec) Validate() error {
|
|
|
|
return fmt.Errorf("failed")
|
|
|
|
}
|
|
|
|
|
2024-03-25 20:27:30 +00:00
|
|
|
func (x *Exec) Apply() error {
|
2024-03-25 20:31:06 +00:00
|
|
|
return nil
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-21 06:13:17 +00:00
|
|
|
func (x *Exec) Load(r io.Reader) error {
|
|
|
|
c := NewYAMLDecoder(r)
|
|
|
|
return c.Decode(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Exec) LoadDecl(yamlResourceDeclaration string) error {
|
|
|
|
c := NewYAMLStringDecoder(yamlResourceDeclaration)
|
|
|
|
return c.Decode(x)
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *Exec) Type() string { return "exec" }
|
|
|
|
|
|
|
|
func (x *Exec) Read(ctx context.Context) ([]byte, error) {
|
2024-03-25 20:31:06 +00:00
|
|
|
return yaml.Marshal(x)
|
2024-03-25 20:27:30 +00:00
|
|
|
}
|