From 8184ac915b8d25b5f07ecf33aff086568c020e11 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Sun, 17 Dec 2023 15:38:31 +0100
Subject: [PATCH] fix: shell result was not written to db #39

---
 job.go                    | 14 ++++++++++++++
 runnable-counter.go       |  4 ++++
 runnable-fileoperation.go | 14 +++++++++-----
 runnable-gorm.go          | 14 +++++++++-----
 runnable-http.go          | 16 ++++++++++------
 runnable-mail.go          | 26 +++++++++++++++-----------
 runnable-sftp.go          | 28 ++++++++++++++++------------
 runnable-shell.go         | 12 ++++++++----
 8 files changed, 85 insertions(+), 43 deletions(-)

diff --git a/job.go b/job.go
index b0b554e..80444ec 100644
--- a/job.go
+++ b/job.go
@@ -17,6 +17,10 @@ func (id JobID) String() string {
 	return string(id)
 }
 
+type GetResult interface {
+	GetResult() string
+}
+
 // Priority is the priority of a job
 type Priority int
 
@@ -260,12 +264,22 @@ func (j *Job[T]) Execute(ctx context.Context) (RunGenericResult, error) {
 		newLog.ErrorMsg = runnerError.Error()
 	}
 
+	newLog.Result = CheckAndCallGetResult(r.Data)
+
 	j.logs = append(j.logs, newLog)
 
 	genericResult := RunGenericResult(r)
 	return genericResult, runnerError
 }
 
+func CheckAndCallGetResult[T any](data T) string {
+	switch v := any(data).(type) {
+	case GetResult:
+		return v.GetResult()
+	}
+	return ""
+}
+
 // Cancel cancels the job, currently a no-op
 func (j *Job[T]) Cancel() error {
 	return nil
diff --git a/runnable-counter.go b/runnable-counter.go
index e24517d..2484da2 100644
--- a/runnable-counter.go
+++ b/runnable-counter.go
@@ -27,6 +27,10 @@ type CounterResult struct {
 	Count int
 }
 
+func (c *CounterResult) GetResult() string {
+	return fmt.Sprintf("Count: %d", c.Count)
+}
+
 // CounterRunnable is a runnable that counts
 type CounterRunnable struct {
 	Count int `json:"count" yaml:"count"`
diff --git a/runnable-fileoperation.go b/runnable-fileoperation.go
index a24acb8..859a7e8 100644
--- a/runnable-fileoperation.go
+++ b/runnable-fileoperation.go
@@ -37,6 +37,10 @@ type FileOperationResult struct {
 	Content string // Optional, je nach Operation
 }
 
+func (f *FileOperationResult) GetResult() string {
+	return f.Content
+}
+
 const (
 	FileOperationRead   = "read"
 	FileOperationWrite  = "write"
@@ -130,16 +134,16 @@ func (f *FileOperationRunnable) GetType() string {
 	return "fileoperation"
 }
 
-func (c *FileOperationRunnable) GetPersistence() RunnableImport {
+func (f *FileOperationRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"operation": c.Operation,
-		"filepath":  c.FilePath,
-		"content":   c.Content,
+		"operation": f.Operation,
+		"filepath":  f.FilePath,
+		"content":   f.Content,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: f.GetType(),
 		Data: data,
 	}
 }
diff --git a/runnable-gorm.go b/runnable-gorm.go
index b6d81ba..9aabc84 100644
--- a/runnable-gorm.go
+++ b/runnable-gorm.go
@@ -38,6 +38,10 @@ type DBResult struct {
 	RowsAffected int
 }
 
+func (d *DBResult) GetResult() string {
+	return fmt.Sprintf("RowsAffected: %d", d.RowsAffected)
+}
+
 type DBRunnable struct {
 	Type  string
 	DSN   string
@@ -93,16 +97,16 @@ func (d *DBRunnable) GetType() string {
 	return "db"
 }
 
-func (c *DBRunnable) GetPersistence() RunnableImport {
+func (d *DBRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"type":  c.Type,
-		"dsn":   c.DSN,
-		"query": c.Query,
+		"type":  d.Type,
+		"dsn":   d.DSN,
+		"query": d.Query,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: d.GetType(),
 		Data: data,
 	}
 }
diff --git a/runnable-http.go b/runnable-http.go
index 37fd088..9f780bb 100644
--- a/runnable-http.go
+++ b/runnable-http.go
@@ -46,6 +46,10 @@ type HTTPResult struct {
 	Body       string
 }
 
+func (h *HTTPResult) GetResult() string {
+	return fmt.Sprintf("StatusCode: %d\n\n%s", h.StatusCode, h.Body)
+}
+
 type HTTPRunnable struct {
 	URL    string
 	Method string
@@ -90,17 +94,17 @@ func (h *HTTPRunnable) GetType() string {
 	return "http"
 }
 
-func (c *HTTPRunnable) GetPersistence() RunnableImport {
+func (h *HTTPRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"url":    c.URL,
-		"method": c.Method,
-		"header": c.Header,
-		"body":   c.Body,
+		"url":    h.URL,
+		"method": h.Method,
+		"header": h.Header,
+		"body":   h.Body,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: h.GetType(),
 		Data: data,
 	}
 }
diff --git a/runnable-mail.go b/runnable-mail.go
index b1b8dc9..5b27853 100644
--- a/runnable-mail.go
+++ b/runnable-mail.go
@@ -75,6 +75,10 @@ type MailResult struct {
 	SmtpStatusCode uint
 }
 
+func (m *MailResult) GetResult() string {
+	return fmt.Sprintf("Sent: %t\n\nServerReply: %s\n\nSmtpStatusCode: %d", m.Sent, m.ServerReply, m.SmtpStatusCode)
+}
+
 type MailRunnable struct {
 	To       string
 	From     string
@@ -146,22 +150,22 @@ func (m *MailRunnable) GetType() string {
 	return "mail"
 }
 
-func (c *MailRunnable) GetPersistence() RunnableImport {
+func (m *MailRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"to":       c.To,
-		"from":     c.From,
-		"subject":  c.Subject,
-		"body":     c.Body,
-		"server":   c.Server,
-		"port":     c.Port,
-		"username": c.Username,
-		"password": c.Password,
-		"headers":  c.Headers,
+		"to":       m.To,
+		"from":     m.From,
+		"subject":  m.Subject,
+		"body":     m.Body,
+		"server":   m.Server,
+		"port":     m.Port,
+		"username": m.Username,
+		"password": m.Password,
+		"headers":  m.Headers,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: m.GetType(),
 		Data: data,
 	}
 }
diff --git a/runnable-sftp.go b/runnable-sftp.go
index 94ec1e2..86f9d99 100644
--- a/runnable-sftp.go
+++ b/runnable-sftp.go
@@ -82,6 +82,10 @@ type SFTPResult struct {
 	FilesCopied []string
 }
 
+func (s *SFTPResult) GetResult() string {
+	return fmt.Sprintf("FilesCopied: %v", s.FilesCopied)
+}
+
 const (
 	CredentialTypePassword = "password"
 	CredentialTypeKey      = "key"
@@ -282,23 +286,23 @@ func (s *SFTPRunnable) GetType() string {
 	return "sftp"
 }
 
-func (c *SFTPRunnable) GetPersistence() RunnableImport {
+func (s *SFTPRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"host":               c.Host,
-		"port":               c.Port,
-		"user":               c.User,
-		"insecure":           c.Insecure,
-		"credential":         c.Credential,
-		"credential_type":    c.CredentialType,
-		"hostkey":            c.HostKey,
-		"src_dir":            c.SrcDir,
-		"dst_dir":            c.DstDir,
-		"transfer_direction": c.TransferDirection,
+		"host":               s.Host,
+		"port":               s.Port,
+		"user":               s.User,
+		"insecure":           s.Insecure,
+		"credential":         s.Credential,
+		"credential_type":    s.CredentialType,
+		"hostkey":            s.HostKey,
+		"src_dir":            s.SrcDir,
+		"dst_dir":            s.DstDir,
+		"transfer_direction": s.TransferDirection,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: s.GetType(),
 		Data: data,
 	}
 }
diff --git a/runnable-shell.go b/runnable-shell.go
index ed79f3f..444e529 100644
--- a/runnable-shell.go
+++ b/runnable-shell.go
@@ -37,6 +37,10 @@ type ShellResult struct {
 	ExitCode int
 }
 
+func (s *ShellResult) GetResult() string {
+	return s.Output
+}
+
 type ShellRunnable struct {
 	ScriptPath string
 	Script     string
@@ -117,15 +121,15 @@ func (s *ShellRunnable) GetType() string {
 	return "shell"
 }
 
-func (c *ShellRunnable) GetPersistence() RunnableImport {
+func (s *ShellRunnable) GetPersistence() RunnableImport {
 
 	data := JSONMap{
-		"scriptPath": c.ScriptPath,
-		"script":      c.Script,
+		"scriptPath": s.ScriptPath,
+		"script":     s.Script,
 	}
 
 	return RunnableImport{
-		Type: c.GetType(),
+		Type: s.GetType(),
 		Data: data,
 	}
 }
-- 
GitLab