Skip to content

Instantly share code, notes, and snippets.

@ptrelford
Last active August 18, 2022 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptrelford/3d132b9169e2cde21181 to your computer and use it in GitHub Desktop.
Save ptrelford/3d132b9169e2cde21181 to your computer and use it in GitHub Desktop.
Go program to download and unzip the latest specified package from Nuget
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