Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send basic tofi history to Toaster-ToasterDB #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/cook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
30 changes: 30 additions & 0 deletions utils/externals.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -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()
}
}
8 changes: 8 additions & 0 deletions utils/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}