From 01966cd202e02e1972576ecabd173288b2cf551a Mon Sep 17 00:00:00 2001
From: Massimiliano Adamo <massimiliano.adamo@geant.org>
Date: Wed, 2 Jun 2021 01:48:58 +0200
Subject: [PATCH] fixing self update feature

---
 build.sh |  9 +++++----
 main.go  | 21 +++++++++++++++++++--
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/build.sh b/build.sh
index 863dbbe..27734b5 100755
--- a/build.sh
+++ b/build.sh
@@ -38,7 +38,7 @@ usage() {
     exit
 }
 
-OPTS=$(getopt -o "h" --longoptions "help,os:,arch:,upx,no-upx:,version:" -- "$@")
+OPTS=$(getopt -o "h" --longoptions "help,os:,arch:,upx,no-upx,version:" -- "$@")
 eval set -- "$OPTS"
 
 while true; do
@@ -106,10 +106,9 @@ run_upx() {
     upx --brute "$EXECUTABLE_PATH"
 }
 
-if [ -n $UPX ]; then
+if [[ "$UPX" == 'UPX' ]]; then
     run_upx
-    cp $EXECUTABLE_PATH .
-elif [ -n $NOUPX ]; then
+elif [[ "$NOUPX" == 'NOUPX' ]]; then
     true
 else
     while true; do
@@ -130,4 +129,6 @@ else
     done
 fi
 
+cp $EXECUTABLE_PATH .
+
 echo -e "\nthe binary was compiled and it is avilable as:\n - \"$EXECUTABLE_PATH\"\n"
diff --git a/main.go b/main.go
index f08b14f..a4a8351 100644
--- a/main.go
+++ b/main.go
@@ -320,6 +320,12 @@ func downloadFile(filepath string, url string) (err error) {
 		return err
 	}
 
+	// change filemode
+	err = os.Chmod(filepath, 0755)
+	if err != nil {
+		return err
+	}
+
 	return nil
 }
 
@@ -359,6 +365,7 @@ Usage:
   acme-downloader -h | --help
   acme-downloader -v | --version
   acme-downloader -b | --build
+  acme-downloader --update
 
 Options:
   -h --help                                     Show this screen
@@ -385,7 +392,7 @@ Options:
 		appExit(0)
 	}
 
-	if arguments["--wildcard"] == true {
+	if arguments["--update"] == true {
 		ArtifactoryBase := "https://artifactory.software.geant.org/artifactory/acme-downloader"
 		ArtifactName := fmt.Sprintf("acme-downloader_%v_%v", runtime.GOOS, runtime.GOARCH)
 		ToolName := os.Args[0]
@@ -393,8 +400,18 @@ Options:
 		if runtime.GOOS == "windows" {
 			ArtifactURL = fmt.Sprintf("%v/%v.exe", ArtifactoryBase, ArtifactName)
 		}
-		downloadFile(ToolName, ArtifactURL)
+		deleteErr := os.Remove(ToolName)
+		if deleteErr != nil {
+			fmt.Printf("Error deleting %v: %v!\n", ToolName, deleteErr)
+			appExit(2)
+		}
+		downloadErr := downloadFile(ToolName, ArtifactURL)
+		if downloadErr != nil {
+			fmt.Printf("Error downloading %v: %v!\n", ToolName, downloadErr)
+			appExit(2)
+		}
 		fmt.Printf("%v updated successfully!\n", ToolName)
+		appExit(0)
 	}
 
 	if runtime.GOOS == "windows" {
-- 
GitLab