jx/internal/resource/exec.go

144 lines
3.1 KiB
Go
Raw Normal View History

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"
"encoding/json"
2024-03-25 20:31:06 +00:00
_ "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"
"decl/internal/codec"
"decl/internal/command"
2024-03-25 20:27:30 +00:00
)
type Exec struct {
2024-05-24 05:11:51 +00:00
stater machine.Stater `yaml:"-" json:"-"`
Id string `yaml:"id,omitempty" json:"id,omitempty"`
CreateTemplate *command.Command `yaml:"create,omitempty" json:"create,omitempty"`
ReadTemplate *command.Command `yaml:"read,omitempty" json:"read,omitempty"`
UpdateTemplate *command.Command `yaml:"update,omitempty" json:"update,omitempty"`
DeleteTemplate *command.Command `yaml:"delete,omitempty" json:"delete,omitempty"`
2024-03-25 20:27:30 +00:00
2024-07-01 07:16:55 +00:00
config ConfigurationValueGetter
2024-07-17 08:34:57 +00:00
Resources ResourceMapper `yaml:"-" json:"-"`
2024-03-25 20:27:30 +00:00
}
func init() {
2024-07-01 07:16:55 +00:00
ResourceTypes.Register([]string{"exec"}, func(u *url.URL) Resource {
2024-03-25 20:31:06 +00:00
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-07-17 08:34:57 +00:00
func (x *Exec) SetResourceMapper(resources ResourceMapper) {
x.Resources = resources
}
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,
}
}
2024-05-06 00:48:54 +00:00
func (x *Exec) StateMachine() machine.Stater {
2024-05-24 05:11:51 +00:00
if x.stater == nil {
x.stater = ProcessMachine(x)
}
return x.stater
2024-05-06 00:48:54 +00:00
}
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
}
2024-07-01 07:16:55 +00:00
func (x *Exec) UseConfig(config ConfigurationValueGetter) {
x.config = config
}
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
}
func (x *Exec) Validate() (err error) {
var execJson []byte
if execJson, err = x.JSON(); err == nil {
s := NewSchema(x.Type())
err = s.Validate(string(execJson))
}
return err
2024-04-09 19:30:05 +00:00
}
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-05-24 05:11:51 +00:00
func (x *Exec) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := x.Create(ctx); e == nil {
if triggerErr := x.stater.Trigger("created"); triggerErr == nil {
return
}
}
case "present":
}
case machine.EXITSTATEEVENT:
}
}
2024-04-21 06:13:17 +00:00
func (x *Exec) Load(r io.Reader) error {
return codec.NewYAMLDecoder(r).Decode(x)
2024-04-21 06:13:17 +00:00
}
func (x *Exec) LoadDecl(yamlResourceDeclaration string) error {
return codec.NewYAMLStringDecoder(yamlResourceDeclaration).Decode(x)
2024-03-25 20:27:30 +00:00
}
func (x *Exec) JSON() ([]byte, error) {
return json.Marshal(x)
}
2024-03-25 20:27:30 +00:00
func (x *Exec) Type() string { return "exec" }
func (x *Exec) Create(ctx context.Context) (err error) {
x.CreateTemplate.Defaults()
2024-07-22 22:26:09 +00:00
_, err = x.CreateTemplate.Execute(x)
return err
2024-05-24 05:11:51 +00:00
}
2024-03-25 20:27:30 +00:00
func (x *Exec) Read(ctx context.Context) ([]byte, error) {
x.ReadTemplate.Defaults()
2024-03-25 20:31:06 +00:00
return yaml.Marshal(x)
2024-03-25 20:27:30 +00:00
}