-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters