From b08dd4558e8286d3efca663973c36ac64885bdcb Mon Sep 17 00:00:00 2001 From: postmannen Date: Wed, 7 Apr 2021 07:00:40 +0200 Subject: [PATCH] Added fileExtension type to message --- example/toCentral-REQTextToLogFile.json | 1 + example/toShip1-REQCliCommand.json | 1 + example/toShip1-REQHttpGet.json | 5 +++-- example/toShip1-REQOpCommand.json | 1 + example/toShip1-REQPing.json | 1 + example/toShip1-REQnCliCommand.json | 3 ++- message_and_subject.go | 15 +++++++++------ subscriber_method_types.go | 22 ++++++++-------------- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/example/toCentral-REQTextToLogFile.json b/example/toCentral-REQTextToLogFile.json index 2375030..80fdb74 100644 --- a/example/toCentral-REQTextToLogFile.json +++ b/example/toCentral-REQTextToLogFile.json @@ -1,6 +1,7 @@ [ { "directory": "random_text_log", + "fileExtension": "log", "toNode": "central", "data": ["some message sent from a ship for testing\n"], "commandOrEvent":"EventACK", diff --git a/example/toShip1-REQCliCommand.json b/example/toShip1-REQCliCommand.json index f3c59e2..b9b1777 100644 --- a/example/toShip1-REQCliCommand.json +++ b/example/toShip1-REQCliCommand.json @@ -1,6 +1,7 @@ [ { "directory":"cli_command__result", + "fileExtension": "result", "toNode": "ship1", "data": ["bash","-c","sleep 3 & tree ./"], "method":"REQCliCommand", diff --git a/example/toShip1-REQHttpGet.json b/example/toShip1-REQHttpGet.json index 7ae3730..a9e4edc 100644 --- a/example/toShip1-REQHttpGet.json +++ b/example/toShip1-REQHttpGet.json @@ -1,7 +1,8 @@ [ { - "directory":"metrics/network/sniffer", - "toNode": "ship1", + "directory": "metrics/network/sniffer", + "fileExtension": "html", + "toNode": "ship2", "data": ["http://vg.no"], "method":"REQHttpGet", "timeout":10, diff --git a/example/toShip1-REQOpCommand.json b/example/toShip1-REQOpCommand.json index e7b16af..4567994 100644 --- a/example/toShip1-REQOpCommand.json +++ b/example/toShip1-REQOpCommand.json @@ -1,6 +1,7 @@ [ { "directory":"opcommand_logs", + "fileExtension": "log", "toNode": "ship1", "data": ["ps"], "method":"REQOpCommand", diff --git a/example/toShip1-REQPing.json b/example/toShip1-REQPing.json index 6485915..070129f 100644 --- a/example/toShip1-REQPing.json +++ b/example/toShip1-REQPing.json @@ -1,6 +1,7 @@ [ { "directory": "ping", + "fileExtension":"ping.log", "toNode": "ship1", "data": [""], "method":"REQPing", diff --git a/example/toShip1-REQnCliCommand.json b/example/toShip1-REQnCliCommand.json index b949562..d568ce9 100644 --- a/example/toShip1-REQnCliCommand.json +++ b/example/toShip1-REQnCliCommand.json @@ -1,6 +1,7 @@ [ { - + "directory":"some-directory", + "fileExtension":"result", "toNode": "ship1", "data": ["bash","-c","tree ../"], "method":"REQnCliCommand", diff --git a/message_and_subject.go b/message_and_subject.go index edff941..b95c01a 100644 --- a/message_and_subject.go +++ b/message_and_subject.go @@ -11,11 +11,6 @@ import ( // --- Message type Message struct { - // Directory is a string that can be used to create the - //directory structure when saving the result of some method. - // For example "syslog","metrics", or "metrics/mysensor" - // The type is typically used in the handler of a method. - Directory string `json:"directory" yaml:"directory"` // The node to send the message to ToNode node `json:"toNode" yaml:"toNode"` // The Unique ID of the message @@ -35,7 +30,15 @@ type Message struct { RequestRetries int `json:"requestRetries" yaml:"requestRetries"` // Timeout for long a process should be allowed to operate MethodTimeout int `json:"methodTimeout" yaml:"methodTimeout"` - + // Directory is a string that can be used to create the + //directory structure when saving the result of some method. + // For example "syslog","metrics", or "metrics/mysensor" + // The type is typically used in the handler of a method. + Directory string `json:"directory" yaml:"directory"` + // FileExtension is used to be able to set a wanted extension + // on a file being saved as the result of data being handled + // by a method handler. + FileExtension string `json:"fileExtension" yaml:"fileExtension"` // PreviousMessage are used for example if a reply message is // generated and we also need a copy of thedetails of the the // initial request message diff --git a/subscriber_method_types.go b/subscriber_method_types.go index 69f6def..505aef6 100644 --- a/subscriber_method_types.go +++ b/subscriber_method_types.go @@ -288,20 +288,19 @@ func (m methodREQTextToLogFile) handler(proc process, message Message, node stri // If it was a request type message we want to check what the initial messages // method, so we can use that in creating the file name to store the data. - fmt.Printf(" ** DEBUG: %v\n", message.PreviousMessage) var fileName string var folderTree string switch { case message.PreviousMessage == nil: // If this was a direct request there are no previous message to take // information from, so we use the one that are in the current mesage. - fileName = fmt.Sprintf("%v.%v.log", message.ToNode, message.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.ToNode, message.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.Directory, string(message.FromNode)) case message.PreviousMessage.ToNode != "": - fileName = fmt.Sprintf("%v.%v.log", message.PreviousMessage.ToNode, message.PreviousMessage.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.PreviousMessage.ToNode, message.PreviousMessage.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) case message.PreviousMessage.ToNode == "": - fileName = fmt.Sprintf("%v.%v.log", message.FromNode, message.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.FromNode, message.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) } @@ -350,7 +349,6 @@ func (m methodREQTextToFile) handler(proc process, message Message, node string) // If it was a request type message we want to check what the initial messages // method, so we can use that in creating the file name to store the data. - fmt.Printf(" ** DEBUG: %v\n", message.PreviousMessage) var fileName string var folderTree string switch { @@ -359,15 +357,12 @@ func (m methodREQTextToFile) handler(proc process, message Message, node string) // information from, so we use the one that are in the current mesage. fileName = fmt.Sprintf("%v.%v.log", message.ToNode, message.Method) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.Directory, string(message.FromNode)) - fmt.Printf(" ** DEBUG1: foldertree: %v\n", folderTree) case message.PreviousMessage.ToNode != "": - fileName = fmt.Sprintf("%v.%v.log", message.PreviousMessage.ToNode, message.PreviousMessage.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.PreviousMessage.ToNode, message.PreviousMessage.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) - fmt.Printf(" ** DEBUG2: foldertree: %v\n", folderTree) case message.PreviousMessage.ToNode == "": - fileName = fmt.Sprintf("%v.%v.log", message.FromNode, message.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.FromNode, message.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) - fmt.Printf(" ** DEBUG3: foldertree: %v\n", folderTree) } // Check if folder structure exist, if not create it. @@ -439,20 +434,19 @@ func (m methodREQErrorLog) handler(proc process, message Message, node string) ( // If it was a request type message we want to check what the initial messages // method, so we can use that in creating the file name to store the data. - fmt.Printf(" ** DEBUG: %v\n", message.PreviousMessage) var fileName string var folderTree string switch { case message.PreviousMessage == nil: // If this was a direct request there are no previous message to take // information from, so we use the one that are in the current mesage. - fileName = fmt.Sprintf("%v.%v.log", message.ToNode, message.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.ToNode, message.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.Directory, string(message.FromNode)) case message.PreviousMessage.ToNode != "": - fileName = fmt.Sprintf("%v.%v.log", message.PreviousMessage.ToNode, message.PreviousMessage.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.PreviousMessage.ToNode, message.PreviousMessage.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) case message.PreviousMessage.ToNode == "": - fileName = fmt.Sprintf("%v.%v.log", message.FromNode, message.Method) + fileName = fmt.Sprintf("%v.%v.%v", message.FromNode, message.Method, message.PreviousMessage.FileExtension) folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Directory, string(message.PreviousMessage.ToNode)) }