From f3fc4419b43f6c9359f4fa5d63564e525070fe56 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Fri, 20 Nov 2020 12:12:40 +0100 Subject: [PATCH] fix: Correctly strip root folder from downloaded archives --- nsd/cmd/download.go | 18 +++++++++++++++++- nsd/nodejs/arch_aix_ppc64.go | 6 +++--- nsd/nodejs/arch_darwin_amd64.go | 6 +++--- nsd/nodejs/arch_linux_amd64.go | 6 +++--- nsd/nodejs/arch_linux_arm64.go | 6 +++--- nsd/nodejs/arch_linux_ppc64le.go | 6 +++--- nsd/nodejs/arch_linux_s390x.go | 6 +++--- nsd/nodejs/arch_solaris_amd64.go | 6 +++--- nsd/nodejs/arch_windows_386.go | 6 +++--- nsd/nodejs/arch_windows_amd64.go | 6 +++--- nsd/nodejs/extension.go | 11 +++++++++++ 11 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 nsd/nodejs/extension.go diff --git a/nsd/cmd/download.go b/nsd/cmd/download.go index e6b97da..1a30746 100644 --- a/nsd/cmd/download.go +++ b/nsd/cmd/download.go @@ -70,7 +70,23 @@ var ( return } - err = archiver.Unarchive(nodeFilePath, downloadPath) + switch NodeJs.CurrentExtension { + case NodeJs.Zip: + zip := archiver.NewZip() + zip.StripComponents = 1 + err = zip.Unarchive(nodeFilePath, downloadPath) + case NodeJs.TarXz: + tar := archiver.NewTarXz() + tar.StripComponents = 1 + err = tar.Unarchive(nodeFilePath, downloadPath) + case NodeJs.TarGz: + tar := archiver.NewTarGz() + tar.StripComponents = 1 + err = tar.Unarchive(nodeFilePath, downloadPath) + default: + return errors.New("Invalid archive format. Aborting") + } + if err != nil { return } diff --git a/nsd/nodejs/arch_aix_ppc64.go b/nsd/nodejs/arch_aix_ppc64.go index f5c1c6f..e95c60c 100644 --- a/nsd/nodejs/arch_aix_ppc64.go +++ b/nsd/nodejs/arch_aix_ppc64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "aix-ppc64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "aix-ppc64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarGz ) diff --git a/nsd/nodejs/arch_darwin_amd64.go b/nsd/nodejs/arch_darwin_amd64.go index 1595318..ece27c1 100644 --- a/nsd/nodejs/arch_darwin_amd64.go +++ b/nsd/nodejs/arch_darwin_amd64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "darwin-x64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "darwin-x64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_linux_amd64.go b/nsd/nodejs/arch_linux_amd64.go index d97294e..2d768de 100644 --- a/nsd/nodejs/arch_linux_amd64.go +++ b/nsd/nodejs/arch_linux_amd64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "linux-x64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "linux-x64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_linux_arm64.go b/nsd/nodejs/arch_linux_arm64.go index b3ffa42..d97dc8c 100644 --- a/nsd/nodejs/arch_linux_arm64.go +++ b/nsd/nodejs/arch_linux_arm64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "linux-arm64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "linux-arm64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_linux_ppc64le.go b/nsd/nodejs/arch_linux_ppc64le.go index 9426114..437633f 100644 --- a/nsd/nodejs/arch_linux_ppc64le.go +++ b/nsd/nodejs/arch_linux_ppc64le.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "linux-ppc64le" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "linux-ppc64le" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_linux_s390x.go b/nsd/nodejs/arch_linux_s390x.go index 41af5b2..cff6bb6 100644 --- a/nsd/nodejs/arch_linux_s390x.go +++ b/nsd/nodejs/arch_linux_s390x.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "linux-s390x" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "linux-s390x" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_solaris_amd64.go b/nsd/nodejs/arch_solaris_amd64.go index 10a019d..46d3453 100644 --- a/nsd/nodejs/arch_solaris_amd64.go +++ b/nsd/nodejs/arch_solaris_amd64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "sunos-x64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "tar.gz" + CurrentArch string = "sunos-x64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = TarXz ) diff --git a/nsd/nodejs/arch_windows_386.go b/nsd/nodejs/arch_windows_386.go index 3aeb174..111909d 100644 --- a/nsd/nodejs/arch_windows_386.go +++ b/nsd/nodejs/arch_windows_386.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "win-x86" - CurrentURL DownloadURL = Stable - CurrentExtension string = "zip" + CurrentArch string = "win-x86" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = Zip ) diff --git a/nsd/nodejs/arch_windows_amd64.go b/nsd/nodejs/arch_windows_amd64.go index c522d30..8c73b0e 100644 --- a/nsd/nodejs/arch_windows_amd64.go +++ b/nsd/nodejs/arch_windows_amd64.go @@ -2,7 +2,7 @@ package nodejs // CurrentArch and CurrentURL describe how the URL of the nodejs download will look like const ( - CurrentArch string = "win-x64" - CurrentURL DownloadURL = Stable - CurrentExtension string = "zip" + CurrentArch string = "win-x64" + CurrentURL DownloadURL = Stable + CurrentExtension FileExtension = Zip ) diff --git a/nsd/nodejs/extension.go b/nsd/nodejs/extension.go new file mode 100644 index 0000000..b3908c6 --- /dev/null +++ b/nsd/nodejs/extension.go @@ -0,0 +1,11 @@ +package nodejs + +// FileExtension defines all possible file extensions for nodejs downloads +type FileExtension string + +// Stable and Unofficial are the two values for DownloadURL +const ( + TarGz FileExtension = "tar.gz" + TarXz FileExtension = "tar.xz" + Zip FileExtension = "zip" +)