// Add a no definition fields to the form if a a field within the
// struct were missing an action above, so we can easily detect
// if there is missing a case action for one of the struct fields.
p.msgInputForm.AddDropDown("error: no case for: "+fieldName,values,0,nil).SetItemPadding(1)
}
}
// Add Buttons below the message fields. Like Generate and Exit.
p.msgInputForm.
// Add a generate button, which when pressed will loop through all the
// message form items, and if found fill the value into a msg struct,
// and at last write it to a file.
//
// TODO: Should also add a write directly to socket here.
AddButton("generate to console",func(){
// fh, err := os.Create("message.json")
// if err != nil {
// log.Fatalf("error: failed to create test.log file: %v\n", err)
// }
// defer fh.Close()
p.msgOutputForm.Clear()
fh:=p.msgOutputForm
m:=msg{}
// Loop trough all the form fields
fori:=0;i<p.msgInputForm.GetFormItemCount();i++{
fi:=p.msgInputForm.GetFormItem(i)
label,value:=getLabelAndValue(fi)
switchlabel{
case"ToNode":
ifvalue==""{
fmt.Fprintf(p.logForm,"%v : error: missing ToNode \n",time.Now().Format("Mon Jan _2 15:04:05 2006"))
return
}
m.ToNode=Node(value)
case"Data":
// Split the comma separated string into a
// and remove the start and end ampersand.
sp:=strings.Split(value,",")
vardata[]string
for_,v:=rangesp{
// Check if format is correct, return if not.
pre:=strings.HasPrefix(v,"\"")
suf:=strings.HasSuffix(v,"\"")
if!pre||!suf{
fmt.Fprintf(p.logForm,"%v : error: missing or malformed format for command, should be \"cmd\",\"arg1\",\"arg2\" ...\n",time.Now().Format("Mon Jan _2 15:04:05 2006"))
return
}
// Remove leading and ending ampersand.
v=v[1:]
v=strings.TrimSuffix(v,"\"")
data=append(data,v)
}
m.Data=data
case"Method":
m.Method=Method(value)
case"ReplyMethod":
m.ReplyMethod=Method(value)
case"ACKTimeout":
v,_:=strconv.Atoi(value)
m.ACKTimeout=v
case"Retries":
v,_:=strconv.Atoi(value)
m.Retries=v
case"ReplyACKTimeout":
v,_:=strconv.Atoi(value)
m.ReplyACKTimeout=v
case"ReplyRetries":
v,_:=strconv.Atoi(value)
m.ReplyRetries=v
case"MethodTimeout":
v,_:=strconv.Atoi(value)
m.MethodTimeout=v
case"Directory":
m.Directory=value
case"FileName":
m.FileName=value
default:
fmt.Fprintf(p.logForm,"%v : error: did not find case defenition for how to handle the \"%v\" within the switch statement\n",time.Now().Format("Mon Jan _2 15:04:05 2006"),label)
}
}
msgs:=[]msg{}
msgs=append(msgs,m)
msgsIndented,err:=json.MarshalIndent(msgs,""," ")
iferr!=nil{
fmt.Fprintf(p.logForm,"%v : error: jsonIndent failed: %v\n",time.Now().Format("Mon Jan _2 15:04:05 2006"),err)
}
_,err=fh.Write(msgsIndented)
iferr!=nil{
fmt.Fprintf(p.logForm,"%v : error: write to fh failed: %v\n",time.Now().Format("Mon Jan _2 15:04:05 2006"),err)
}
}).
// Add exit button.
AddButton("exit",func(){
app.Stop()
})
app.SetFocus(p.msgInputForm)
returnp.flex
}
// Check and compare all the fields of the main message struct
// used in Steward, and the message struct used in Stew that they are
// equal.
// If they are not equal an error will be returned to the user with
// the name of the field that was missing in the Stew message struct.
//
// Some of the fields in the Steward Message struct are used by the
// system for control, and not needed when creating an initial message
// template, and we can add case statements for those fields below
// that we do not wan't to check.
funccompareMsgAndMessage()error{
stewardMessage:=Message{}
stewMsg:=msg{}
stewardRefVal:=reflect.ValueOf(stewardMessage)
stewRefVal:=reflect.ValueOf(stewMsg)
// Loop trough all the fields of the Message struct.