Skip to content

Commit

Permalink
adjust runner meta
Browse files Browse the repository at this point in the history
  • Loading branch information
akerl committed Jan 25, 2023
1 parent b728fdd commit 0e856da
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
38 changes: 33 additions & 5 deletions plugin/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
package plugin

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/ghodss/yaml"
)

// Mode signals whether the check should return expected or actual value
type Mode int

const (
// Actual checks the current state of the check
Actual Mode = iota
// Expected checks the desired state of the check
Expected
)

// Meta passes additional information to the check from the runner
type Meta struct {
Mode Mode `json:"mode"`
}

// Input merges runner metadata with config args
type Input struct {
Meta Meta `json:"meta"`
Args json.RawMessage `json:"args"`
}

// ParseConfig loads the config from stdin
func ParseConfig(cfg interface{}) error {
func ParseConfig(cfg interface{}) (Meta, error) {
info, err := os.Stdin.Stat()
if err != nil {
return err
return Meta{}, err
}

if info.Mode()&os.ModeNamedPipe != os.ModeNamedPipe || info.Size() <= 0 {
return fmt.Errorf("plugin executed without stdin")
return Meta{}, fmt.Errorf("plugin executed without stdin")
}

input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
return Meta{}, err
}

pi := Input{}
err = yaml.Unmarshal(input, &pi)
if err != nil {
return Meta{}, err
}

return yaml.Unmarshal(input, &cfg)
return pi.Meta, yaml.Unmarshal(pi.Args, &cfg)
}
54 changes: 46 additions & 8 deletions runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"strings"

"github.com/akerl/prospectus/v3/plugin"
"github.com/ghodss/yaml"
)

Expand All @@ -31,12 +32,47 @@ type ResultSet []Result
type Plugin struct {
Type string `json:"type"`
Args json.RawMessage `json:"args"`
Meta plugin.Meta `json:"-"`
}

// Check defines a set of expected/actual plugins
type Check struct {
Expected Plugin `json:"expected"`
Actual Plugin `json:"actual"`
Type string `json:"type"`
Args json.RawMessage `json:"args"`
ExpectedPlugin Plugin `json:"expected"`
ActualPlugin Plugin `json:"actual"`
}

// Expected returns either the merged plugin or expected plugin
func (c *Check) Expected() Plugin {
meta := plugin.Meta{
Mode: plugin.Expected,
}
if c.Type == "" {
c.ExpectedPlugin.Meta = meta
return c.ExpectedPlugin
}
return Plugin{
Type: c.Type,
Args: c.Args,
Meta: meta,
}
}

// Actual returns either the merged plugin or actual plugin
func (c *Check) Actual() Plugin {
meta := plugin.Meta{
Mode: plugin.Actual,
}
if c.Type == "" {
c.ActualPlugin.Meta = meta
return c.ActualPlugin
}
return Plugin{
Type: c.Type,
Args: c.Args,
Meta: meta,
}
}

// Runner defines a set of named Checks
Expand All @@ -63,8 +99,8 @@ func (r Runner) Check() ResultSet {

for name, check := range r.Items {
res := Result{Name: name}
res.Expected = check.Expected.Run()
res.Actual = check.Actual.Run()
res.Expected = check.Expected().Run()
res.Actual = check.Actual().Run()
rs = append(rs, res)
}

Expand All @@ -84,11 +120,13 @@ func (p Plugin) Run() Value {
if err != nil {
return Value{Error: err}
}
if len(p.Args) == 0 {
stdin.Write([]byte("{}"))
} else {
stdin.Write(p.Args)

pi := plugin.Input{
Meta: p.Meta,
Args: p.Args,
}
input, err := json.Marshal(pi)
stdin.Write(input)
stdin.Close()

var stdoutBytes bytes.Buffer
Expand Down

0 comments on commit 0e856da

Please sign in to comment.