diff --git a/main.go b/main.go index 77e58021981ed3b6a0d4de2802e79f662bfc2c75..f08b14f7c07b208b831c7ad40cb93b1ea018b378 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "io" "io/ioutil" "net/http" "os" @@ -291,6 +292,37 @@ func ReadOSRelease(configfile string) map[string]string { return ConfigParams } +// download file: taken as is from StackOverflow +func downloadFile(filepath string, url string) (err error) { + + // Create the file + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + + // Get the data + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Check server response + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("bad status: %s", resp.Status) + } + + // Writer the body to file + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil +} + func main() { OSInfo := ReadOSRelease("/etc/os-release") @@ -343,6 +375,7 @@ Options: --key-destination=KEYDESTINATION Key Destination [default: %v/<cert-name>.key] --ca-destination=CADESTINATION CA Destination [default: %v/COMODO_<type>.crt] --wildcard The certificate type is wildcard + --update Self-updates the tool and exit `, CertBase, CertBase, KeyBase, CertBase) arguments, _ := docopt.Parse(usage, nil, true, appVersion, false) @@ -352,6 +385,18 @@ Options: appExit(0) } + if arguments["--wildcard"] == 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] + ArtifactURL := fmt.Sprintf("%v/%v", ArtifactoryBase, ArtifactName) + if runtime.GOOS == "windows" { + ArtifactURL = fmt.Sprintf("%v/%v.exe", ArtifactoryBase, ArtifactName) + } + downloadFile(ToolName, ArtifactURL) + fmt.Printf("%v updated successfully!\n", ToolName) + } + if runtime.GOOS == "windows" { tmpCertificateDestination = "C:\\tmp\\acme-downloader\\cert\\amce_cert.pem" tmpFullchainDestination = "C:\\tmp\\acme-downloader\\cert\\amce_fullchain.pem"