Commit 89b4a300 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add an interface for scripts

parent c6a90f58
package scripts
import (
"encoding/json"
"log"
)
type (
// SSHKey of a user
SSHKey struct {
Name string `json:"name"`
Key string `json:"key"`
}
// Group a group of which User is a member of
Group struct {
Name string `json:"name"`
}
// UserInfo info about the user
UserInfo map[string]interface{}
// User describes the user info
User struct {
Email string `json:"email"`
Groups []Group `json:"groups"`
UserInfo UserInfo `json:"userinfo"`
}
// Input of the deployment script
Input struct {
Action string `json:"action"` // either "deploy" or "withdraw"
User User `json:"user"`
Key SSHKey `json:"key"`
}
// Status for Output
Status string
// Output of the deployment script
Output struct {
// Status: StatusSuccess, StatusFail, StatusReject
Status Status `json:"status"`
// Message for the user
Msg string `json:"message"`
// data for the backend
Data map[string]interface{} `json:"data"`
}
)
const (
// Success value for Output.Status
Success Status = "success"
// Fail value for Output.Status
Fail Status = "fail"
// Reject value for Output.Status
Reject Status = "reject"
)
func (u User) String() string {
return u.Email
}
func (k SSHKey) String() string {
return k.Name
}
func (i Input) String() string {
iBytes, err := i.Marshal()
if err != nil {
log.Fatal(err)
}
return string(iBytes)
}
func (o Output) String() string {
oBytes, err := o.Marshal()
if err != nil {
log.Fatal(err)
}
return string(oBytes)
}
// Marshal encodes an Input as json
func (i Input) Marshal() (iBytes []byte, err error) {
iBytes, err = json.MarshalIndent(i, "", " ")
return
}
// Marshal encodes an Output as json
func (o Output) Marshal() (oBytes []byte, err error) {
oBytes, err = json.MarshalIndent(o, "", " ")
return
}
// UnmarshalInput decodes a json encoded input
func UnmarshalInput(inputBytes []byte) (i Input, err error) {
err = json.Unmarshal(inputBytes, &i)
return
}
// UnmarshalOutput decodes a json encoded output
func UnmarshalOutput(inputBytes []byte) (i Output, err error) {
err = json.Unmarshal(inputBytes, &i)
return
}
Supports Markdown
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