mirror of
https://github.com/postmannen/ctrl.git
synced 2025-01-07 04:49:17 +00:00
implemented reading and embedding the content of a local file with {{CTRL_FILE}}
This commit is contained in:
parent
dcdbc9308e
commit
4e6e4c515e
2 changed files with 52 additions and 1 deletions
|
@ -55,7 +55,10 @@ func methodCliCommand(proc process, message Message, node string) ([]byte, error
|
||||||
// data payload there.
|
// data payload there.
|
||||||
var foundEnvData bool
|
var foundEnvData bool
|
||||||
var envData string
|
var envData string
|
||||||
|
//var foundEnvFile bool
|
||||||
|
//var envFile string
|
||||||
for i, v := range message.MethodArgs {
|
for i, v := range message.MethodArgs {
|
||||||
|
// Check if to use the content of the data field are specified.
|
||||||
if strings.Contains(v, "{{CTRL_DATA}}") {
|
if strings.Contains(v, "{{CTRL_DATA}}") {
|
||||||
foundEnvData = true
|
foundEnvData = true
|
||||||
// Replace the found env variable placeholder with an actual env variable
|
// Replace the found env variable placeholder with an actual env variable
|
||||||
|
|
50
server.go
50
server.go
|
@ -5,11 +5,14 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -537,9 +540,54 @@ func (s *server) routeMessagesToPublisherProcess() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---
|
// ---
|
||||||
|
// Check for {{CTRL_FILE}} and if we should read and load a local file into
|
||||||
|
// the message before sending.
|
||||||
|
|
||||||
|
var filePathToOpen string
|
||||||
|
foundFile := false
|
||||||
|
var argPos int
|
||||||
|
for i, v := range message.MethodArgs {
|
||||||
|
if strings.Contains(v, "{{CTRL_FILE:") {
|
||||||
|
foundFile = true
|
||||||
|
argPos = i
|
||||||
|
|
||||||
|
// Example to split:
|
||||||
|
// echo {{CTRL_FILE:/somedir/msg_file.yaml}}>ctrlfile.txt
|
||||||
|
//
|
||||||
|
// Split at colon. We want the part after.
|
||||||
|
ss := strings.Split(v, ":")
|
||||||
|
// Split at "}}",so pos [0] in the result contains just the file path.
|
||||||
|
sss := strings.Split(ss[1], "}}")
|
||||||
|
filePathToOpen = sss[0]
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if foundFile {
|
||||||
|
|
||||||
|
fh, err := os.Open(filePathToOpen)
|
||||||
|
if err != nil {
|
||||||
|
er := fmt.Errorf("error: routeMessagesToPublisherProcess: failed to open file given as CTRL_FILE argument: %v", err)
|
||||||
|
s.errorKernel.logError(er)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer fh.Close()
|
||||||
|
|
||||||
|
b, err := io.ReadAll(fh)
|
||||||
|
if err != nil {
|
||||||
|
er := fmt.Errorf("error: routeMessagesToPublisherProcess: failed to read file %v given as CTRL_FILE argument: %v", filePathToOpen, err)
|
||||||
|
s.errorKernel.logError(er)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the {{CTRL_FILE}} with the actual content read from file.
|
||||||
|
re := regexp.MustCompile(`(.*)({{CTRL_FILE.*}})(.*)`)
|
||||||
|
message.MethodArgs[argPos] = re.ReplaceAllString(message.MethodArgs[argPos], `${1}`+string(b)+`${3}`)
|
||||||
|
// ---
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
message.ArgSignature = s.processInitial.addMethodArgSignature(message)
|
message.ArgSignature = s.processInitial.addMethodArgSignature(message)
|
||||||
// fmt.Printf(" * DEBUG: add signature, fromNode: %v, method: %v, len of signature: %v\n", m.FromNode, m.Method, len(m.ArgSignature))
|
|
||||||
|
|
||||||
go s.processInitial.publishAMessage(message, s.natsConn)
|
go s.processInitial.publishAMessage(message, s.natsConn)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue