Commit e63d0039 authored by Lukas Burgey's avatar Lukas Burgey

Add questionnaire-script

parent 94d2582e
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"git.scc.kit.edu/feudal/feudalScripts/v2"
)
const (
version = "2.0.0"
)
var (
httpClient = &http.Client{}
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func deploy(input scripts.Input) (output scripts.Output, err error) {
time.Sleep(time.Second)
if input.Answers != nil {
_, fooOK := input.Answers["Question-Foo"]
_, barOK := input.Answers["Question-Bar"]
if fooOK && barOK {
output.State = scripts.Deployed
output.Msg = "I got all the answers"
return
}
}
output.State = scripts.Questionnaire
output.Msg = "I need some answers"
output.Questionnaire = map[string]string{
"Question-Foo": "Description of question foo",
"Question-Bar": "Description of question bar",
}
return
}
func withdraw(input scripts.Input) (output scripts.Output, err error) {
time.Sleep(time.Second)
output.State = scripts.NotDeployed
output.Msg = "I removed"
return
}
func handleInput() (output scripts.Output, err error) {
inputBytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return
}
input, err := scripts.UnmarshalInput(inputBytes)
if err != nil {
return
}
switch input.StateTarget {
case "deployed":
output, err = deploy(input)
case "not_deployed":
output, err = withdraw(input)
default:
err = fmt.Errorf("Not implemented")
}
return
}
func main() {
log.SetOutput(os.Stderr)
output, err := handleInput()
if err != nil {
output = scripts.Output{
State: scripts.Failed,
Msg: fmt.Sprintf("%s", err),
}
}
outputBytes, err := json.MarshalIndent(output, "", " ")
fmt.Fprintf(os.Stdout, "%s", outputBytes)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment