From 44c84a20e3bd61fcfcd537ab342f89fb7148cdbe Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Thu, 21 Oct 2021 09:55:49 +0200 Subject: [PATCH 1/4] Fixes #749 by adding empty string for backup file in sed Sedon BSD Unix requires an argument for the -i switch. Unless you wanta backup file inemust add an empty string argument. Signed-off-by: Sven Strittmatter --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 87ef02800f..e25a406f97 100644 --- a/Makefile +++ b/Makefile @@ -154,7 +154,7 @@ ifdef NAME rsync -a ./.templates/new-scanner/ ./scanners/$(NAME) ; \ echo "Copied template files to new directory ./scanners/$(NAME)"; \ cd scanners/$(NAME) ; \ - find . -type f -exec sed -i 's/new-scanner/$(NAME)/g' {} + ; \ + find . -type f -exec sed -i '' 's/new-scanner/$(NAME)/g' {} + ; \ mv "./templates/new-scanner-parse-definition.yaml" "templates/$(NAME)-parse-definition.yaml" ; \ mv "./templates/new-scanner-scan-type.yaml" "templates/$(NAME)-scan-type.yaml" ; else From 4a75ef8d76e1c7fa46e494e878f6299f2b1dc349 Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Thu, 21 Oct 2021 11:19:56 +0200 Subject: [PATCH 2/4] Simplify the makefile by removing shell specific syntax It is not necessar to add ;\ at the end of each line because make runs each command step by step. So this is removed. Also echo what is done is not necessary because make prints by default each command for debug purpose. Also removed the cd because this is brittle: If the cd commandfails the subsequent commands work in a wrong directory. Better solution is to always use theconcrete path relative to the Makefile. Signed-off-by: Sven Strittmatter --- Makefile | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index e25a406f97..beb657a4cc 100644 --- a/Makefile +++ b/Makefile @@ -150,15 +150,13 @@ docs: readme hook-docs scanner-docs operator-docs auto-discovery-docs demo-apps- .PHONY: create-new-scanner create-new-scanner: ## Creates templates for a new scanner, pass NAME=NEW-SCANNER ifdef NAME - @mkdir scanners/$(NAME) ; \ - rsync -a ./.templates/new-scanner/ ./scanners/$(NAME) ; \ - echo "Copied template files to new directory ./scanners/$(NAME)"; \ - cd scanners/$(NAME) ; \ - find . -type f -exec sed -i '' 's/new-scanner/$(NAME)/g' {} + ; \ - mv "./templates/new-scanner-parse-definition.yaml" "templates/$(NAME)-parse-definition.yaml" ; \ - mv "./templates/new-scanner-scan-type.yaml" "templates/$(NAME)-scan-type.yaml" ; + mkdir scanners/$(NAME) + rsync -a ./.templates/new-scanner/ ./scanners/$(NAME) + find ./scanners/$(NAME) -type f -exec sed -i '' 's/new-scanner/$(NAME)/g' {} + + mv ./scanners/$(NAME)/templates/new-scanner-parse-definition.yaml ./scanners/$(NAME)/templates/$(NAME)-parse-definition.yaml + mv ./scanners/$(NAME)/templates/new-scanner-scan-type.yaml ./scanners/$(NAME)/templates/$(NAME)-scan-type.yaml else - @echo "Scanner name not defined, please provide via make create-new-scanner NAME=NEW-SCANNER"; + @echo "Scanner name not defined, please provide via make create-new-scanner NAME=NEW-SCANNER" endif .PHONY: From 6ebfbd5721830ee1200bcdd613d1e262cea7c853 Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Thu, 21 Oct 2021 11:24:13 +0200 Subject: [PATCH 3/4] Remove the rsync because rsync is for copy files between remote machines. Since we copy files local it makes no sense to use rsync here. A simple recursive copy is well suited for this. Signed-off-by: Sven Strittmatter --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index beb657a4cc..bf1913a6fc 100644 --- a/Makefile +++ b/Makefile @@ -150,8 +150,7 @@ docs: readme hook-docs scanner-docs operator-docs auto-discovery-docs demo-apps- .PHONY: create-new-scanner create-new-scanner: ## Creates templates for a new scanner, pass NAME=NEW-SCANNER ifdef NAME - mkdir scanners/$(NAME) - rsync -a ./.templates/new-scanner/ ./scanners/$(NAME) + cp -r ./.templates/new-scanner ./scanners/$(NAME) find ./scanners/$(NAME) -type f -exec sed -i '' 's/new-scanner/$(NAME)/g' {} + mv ./scanners/$(NAME)/templates/new-scanner-parse-definition.yaml ./scanners/$(NAME)/templates/$(NAME)-parse-definition.yaml mv ./scanners/$(NAME)/templates/new-scanner-scan-type.yaml ./scanners/$(NAME)/templates/$(NAME)-scan-type.yaml From 4faf5ff8aa50e44751242f9f595c2ca4f2e08bc5 Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Thu, 21 Oct 2021 16:28:03 +0200 Subject: [PATCH 4/4] Remove non-POSIX compliant switch from sed The -i Switch (in place file processing) is not POSIX compliant and behaves differently on some systems: * BSD Unix: requires an argument (e.g. -i '.bak') * GNU Linux: does not allow an argument This patch introduces a platform independent way for replacing the needle stringin with the scanner name in the copied template files. The trick is to use a temporary file to save the sed result. First aproach was to use > to redirect the sed output from STDOUT into the temp file. This does not work because find's -exec does not execute a shell and executes the command directly in a forked child process. So there are no shell features like redirect or pipe. The solution is to use the sed command 'w' to write the file and supress the output on STDOUT. Signed-off-by: Sven Strittmatter --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bf1913a6fc..ee7701dd50 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,9 @@ docs: readme hook-docs scanner-docs operator-docs auto-discovery-docs demo-apps- create-new-scanner: ## Creates templates for a new scanner, pass NAME=NEW-SCANNER ifdef NAME cp -r ./.templates/new-scanner ./scanners/$(NAME) - find ./scanners/$(NAME) -type f -exec sed -i '' 's/new-scanner/$(NAME)/g' {} + + find ./scanners/$(NAME) -type f ! -name 'tmp' \ + -exec sed -n 's/new-scanner/$(NAME)/g;w ./scanners/$(NAME)/tmp' {} \; \ + -exec mv ./scanners/$(NAME)/tmp {} \; mv ./scanners/$(NAME)/templates/new-scanner-parse-definition.yaml ./scanners/$(NAME)/templates/$(NAME)-parse-definition.yaml mv ./scanners/$(NAME)/templates/new-scanner-scan-type.yaml ./scanners/$(NAME)/templates/$(NAME)-scan-type.yaml else