@@ -12,23 +12,52 @@ import (
1212 "github.com/urfave/cli"
1313)
1414
15- func pushNupkg (c * cli.Context ) error {
15+ func cliPushNupkg (c * cli.Context ) error {
1616
1717 // Check .nuspec file has been supplied
1818 filename := c .Args ().First ()
1919 if filename == "" {
2020 return errors .New ("Error NU5002: Please specify a nuspec file to use" )
2121 }
2222
23+ // Read in package contents
24+ fileContents , err := ioutil .ReadFile (filename )
25+ checkError (err )
26+
27+ // print out for CLI
28+ fmt .Println ("Pushing " + filename + " to '" + c .String ("Source" ) + "'..." )
29+ fmt .Println (" " , http .MethodPut , c .String ("Source" ))
30+ status , dur , err := PushNupkg (fileContents , c .String ("ApiKey" ), c .String ("Source" ))
31+ checkError (err )
32+
33+ // print out for CLI
34+ fmt .Println (" " , status , c .String ("Source" ), dur , "ms" )
35+
36+ // Log out result
37+ if status >= 200 && status <= 299 {
38+ fmt .Println ("Your package was pushed." )
39+ } else {
40+ fmt .Println ("Response status code does not indicate success:" , status )
41+ }
42+
43+ // Return Ok
44+ return nil
45+ }
46+
47+ // PushNupkg PUTs a .nupkg binary to a NuGet Repository
48+ func PushNupkg (fileContents []byte , apiKey string , host string ) (int , int64 , error ) {
49+
50+ // If no Source provided, exit
51+ if host == "" {
52+ return 0 , 0 , errors .New ("Error: Please specify a Source/Host" )
53+ }
54+
2355 // Create MultiPart Writer
2456 body := new (bytes.Buffer )
2557 w := multipart .NewWriter (body )
2658 // Create new File part
2759 p , err := w .CreateFormFile ("package" , "package.nupkg" )
2860 checkError (err )
29- // Read in package contents
30- fileContents , err := ioutil .ReadFile (filename )
31- checkError (err )
3261 // Write contents to part
3362 _ , err = p .Write (fileContents )
3463 checkError (err )
@@ -37,31 +66,22 @@ func pushNupkg(c *cli.Context) error {
3766 checkError (err )
3867
3968 // Create new PUT request
40- request , err := http .NewRequest (http .MethodPut , c . String ( "Source" ) , body )
69+ request , err := http .NewRequest (http .MethodPut , host , body )
4170 checkError (err )
4271 // Add the ApiKey if supplied
43- if c . String ( "ApiKey" ) != "" {
44- request .Header .Add ("X-Nuget-Apikey" , c . String ( "ApiKey" ) )
72+ if apiKey != "" {
73+ request .Header .Add ("X-Nuget-Apikey" , apiKey )
4574 }
4675 // Add the Content Type header from the reader - includes boundary
4776 request .Header .Add ("Content-Type" , w .FormDataContentType ())
4877
4978 // Push to the server
50- fmt .Println ("Pushing " + filename + " to '" + c .String ("Source" ) + "'..." )
51- fmt .Println (" " , request .Method , request .URL .String ())
5279 startTime := time .Now ()
5380 client := & http.Client {}
5481 resp , err := client .Do (request )
5582 checkError (err )
5683 duration := time .Now ().Sub (startTime )
5784
58- // Log out result
59- fmt .Println (" " , resp .StatusCode , resp .Request .URL .String (), duration .Milliseconds (), "ms" )
60- if resp .StatusCode >= 200 && resp .StatusCode <= 299 {
61- fmt .Println ("Your package was pushed." )
62- } else {
63- fmt .Println ("Response status code does not indicate success:" , resp .StatusCode )
64- }
65-
66- return nil
85+ // Return Results
86+ return resp .StatusCode , duration .Milliseconds (), nil
6787}
0 commit comments