diff --git a/cmd/cook.go b/cmd/cook.go index 4b381bd..7816cb7 100644 --- a/cmd/cook.go +++ b/cmd/cook.go @@ -91,6 +91,8 @@ var cookCmd = &cobra.Command{ log.Println("TofuGu removed tofi temp dir: " + tofuguStruct.CmdWorkTempDir) } + tofuguStruct.SendHistoryData(cmdToExec, cmdArgs, exitCodeFinal) + log.Printf("TofuGu: %v finished with code %v", cmdToExec, exitCodeFinal) os.Exit(exitCodeFinal) }, diff --git a/utils/externals.go b/utils/externals.go index 6b256b1..8e75274 100644 --- a/utils/externals.go +++ b/utils/externals.go @@ -1,6 +1,7 @@ package utils import ( + "bytes" "crypto/md5" "encoding/hex" "encoding/json" @@ -118,3 +119,32 @@ func (tofuguStruct *Tofugu) GetDimData(dimensionKey string, dimensionValue strin return dimensionJsonMap } + +func (tofuguStruct *Tofugu) SendHistoryData(cmdToExec string, cmdArgs []string, exitCodeFinal int) { + if tofuguStruct.ToasterUrl != "" { + + var historyData HistoryStruct + historyData.CmdToExec = cmdToExec + historyData.CmdMainArg = cmdArgs[0] + if len(cmdArgs) > 1 { + historyData.CmdArgs = cmdArgs[1:] + } + historyData.ExitCode = exitCodeFinal + historyData.Dimensions = tofuguStruct.ParsedDimensions + + byteStream := new(bytes.Buffer) + err := json.NewEncoder(byteStream).Encode(historyData) + if err != nil { + log.Printf("tofugu: failed to prepare json data: %s", err) + } + + resp, err := http.Post(tofuguStruct.ToasterUrl+"/api/history/"+tofuguStruct.OrgName+"/"+tofuguStruct.Workspace+"/"+tofuguStruct.TofiName, "application/json; charset=UTF-8", byteStream) + if err != nil { + log.Printf("tofugu toaster: history post request failed: %s", err) + } else if resp.StatusCode != 200 { + resp.Body.Close() + log.Printf("tofugu toaster: history post request failed with response: %v", resp.StatusCode) + } + defer resp.Body.Close() + } +} diff --git a/utils/structures.go b/utils/structures.go index bf36c4e..5cb3821 100644 --- a/utils/structures.go +++ b/utils/structures.go @@ -30,3 +30,11 @@ type DimensionInToaster struct { WorkSpace string DimData map[string]interface{} } + +type HistoryStruct struct { + CmdToExec string + CmdArgs []string + CmdMainArg string + ExitCode int + Dimensions map[string]string +}