Last active
August 18, 2022 14:13
-
-
Save ptrelford/3d132b9169e2cde21181 to your computer and use it in GitHub Desktop.
Go program to download and unzip the latest specified package from Nuget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "os" | |
import "io" | |
import "fmt" | |
import "path" | |
import "net/http" | |
import "archive/zip" | |
func downloadPackage(name string) { | |
url := fmt.Sprintf("https://www.nuget.org/api/v2/package/%s",name) | |
resp, _ := http.Get(url) | |
defer resp.Body.Close() | |
filename := fmt.Sprintf("%s.nupkg",name) | |
out, _ := os.Create(filename) | |
defer out.Close() | |
io.Copy(out, resp.Body) | |
} | |
func unzip(name string) { | |
filename := fmt.Sprintf("%s.nupkg",name) | |
reader, _ := zip.OpenReader(filename) | |
defer reader.Close() | |
for _, file := range reader.File { | |
in, _ := file.Open() | |
defer in.Close() | |
relname := path.Join("packages",name, file.Name) | |
dir := path.Dir(relname) | |
os.MkdirAll(dir, 0777) | |
out, _ := os.Create(relname) | |
defer out.Close() | |
io.Copy(out, in) | |
} | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Print("Please specify a Nuget package") | |
return | |
} | |
name := os.Args[1] | |
downloadPackage(name) | |
unzip(name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment