Last active
January 10, 2020 18:56
-
-
Save HokieGeek/3a3aaa453ade5fd79a52004bb5f47969 to your computer and use it in GitHub Desktop.
Simple app that downloads all of the PDFs in Nexus IQ
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 ( | |
"flag" | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
nexusiq "github.com/sonatype-nexus-community/gonexus/iq" | |
) | |
// USAGE: go run downloadNexusIqPdfs.go --server <iqserver> --user <iquser> --password <iqpassword> <directory_path> | |
func main() { | |
iqServer := flag.String("server", "http://localhost:8070", "Domain and port of Nexus IQ Server") | |
iqUser := flag.String("user", "admin", "The username to use to log in to Nexus IQ Server") | |
iqPassword := flag.String("password", "admin123", "The password to use to log in to Nexus IQ Server") | |
flag.Parse() | |
// Initiate IQ client | |
fmt.Println(":: Creating client") | |
iq, err := nexusiq.New(*iqServer, *iqUser, *iqPassword) | |
if err != nil { | |
panic(err) | |
} | |
// Retrieve all reports | |
fmt.Println(":: Retrieving reports") | |
reports, err := nexusiq.GetAllReports(iq) | |
if err != nil { | |
panic(err) | |
} | |
// Download PDFs | |
fmt.Println(":: Downloading PDFs") | |
pdfDirectory := flag.Arg(0) | |
if _, err := os.Stat(pdfDirectory); os.IsNotExist(err) { | |
os.Mkdir(pdfDirectory, os.ModePerm) | |
} | |
for _, report := range reports { | |
reportInfo := report.Policy.ReportInfo | |
fmt.Println("Downloading PDF for report", reportInfo.ReportID()) | |
// Download the PDF | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", *iqServer, reportInfo.ReportPdfURL), nil) | |
if err != nil { | |
fmt.Println(err) | |
continue | |
} | |
req.SetBasicAuth(*iqUser, *iqPassword) | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println(err) | |
continue | |
} | |
defer resp.Body.Close() | |
// Create the PDF | |
filepath := fmt.Sprintf("%s/%s_%s_%s.pdf", pdfDirectory, reportInfo.ApplicationID, reportInfo.Stage, reportInfo.ReportID()) | |
out, err := os.Create(filepath) | |
if err != nil { | |
fmt.Println(err) | |
continue | |
} | |
defer out.Close() | |
if _, err = io.Copy(out, resp.Body); err != nil { | |
fmt.Println(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment