jx/internal/resource/exec.go

77 lines
1.5 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"
_ "log"
"net/url"
_ "os"
_ "os/exec"
2024-03-27 21:14:40 +00:00
"path/filepath"
2024-03-25 20:31:06 +00:00
_ "strings"
2024-03-25 20:27:30 +00:00
)
type Exec struct {
2024-03-25 20:31:06 +00:00
loader YamlLoader
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-03-25 20:31:06 +00:00
return &Exec{loader: YamlLoadDecl}
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
}
func (x *Exec) LoadDecl(yamlFileResourceDeclaration string) error {
2024-03-25 20:31:06 +00:00
return x.loader(yamlFileResourceDeclaration, 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
}