Skip to content

Commit 0f86f98

Browse files
author
Sam Shelton
committed
Abstracted NuGet handling to utilities package
1 parent 230caeb commit 0f86f98

File tree

7 files changed

+11
-405
lines changed

7 files changed

+11
-405
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module github.com/soloworks/go-nuget
1+
module github.com/soloworks/go-nuget-cli
22

33
go 1.13
44

55
require (
6-
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
6+
github.com/soloworks/go-nuget v1.0.0
77
github.com/soloworks/go-nuspec v0.3.0
88
github.com/urfave/cli v1.22.1
99
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
99
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1010
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
1111
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
12+
github.com/soloworks/go-nuget v1.0.0 h1:KMFgIWPKtC12rUeh9QtqSt9Z1S/DVSh+nzWcx8Y5ZBE=
13+
github.com/soloworks/go-nuget v1.0.0/go.mod h1:IqUp1+6g/kBxoROxkBFtlT62JtAuaEM6u4bquZq5dd8=
14+
github.com/soloworks/go-nuspec v0.1.0/go.mod h1:mLMhXDLZHIjX/90kJfhlt51RgJrYzj7LdgxqLI3zxl0=
1215
github.com/soloworks/go-nuspec v0.3.0 h1:guoIL3K13UkjjriTqXV6Xfs1PVcLLib3X4E1fTlm26U=
1316
github.com/soloworks/go-nuspec v0.3.0/go.mod h1:mLMhXDLZHIjX/90kJfhlt51RgJrYzj7LdgxqLI3zxl0=
1417
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=

main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"log"
5-
"math/rand"
65
"os"
76
"strconv"
87
"time"
@@ -14,16 +13,6 @@ import (
1413
var version string = "0.0.0.0-master"
1514
var compiled string = "1262304000" // 01/01/2010 @ 12:00am
1615

17-
const letterBytes = "abcdef0123456789"
18-
19-
func randomString(n int) string {
20-
b := make([]byte, n)
21-
for i := range b {
22-
b[i] = letterBytes[rand.Intn(len(letterBytes))]
23-
}
24-
return string(b)
25-
}
26-
2716
func checkError(e error) {
2817
if e != nil {
2918
println(e.Error())

pack.go

Lines changed: 2 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
11
package main
22

33
import (
4-
"archive/zip"
5-
"bytes"
64
"errors"
75
"fmt"
86
"io/ioutil"
97
"log"
108
"os"
119
"path/filepath"
1210

11+
nuget "github.com/soloworks/go-nuget"
1312
nuspec "github.com/soloworks/go-nuspec"
1413
"github.com/urfave/cli"
1514
)
1615

17-
func archiveFile(fn string, w *zip.Writer, b []byte) {
18-
19-
// Check and convert filepath to `/` if required
20-
fn = filepath.ToSlash(fn)
21-
22-
// Create the file in the zip
23-
f, err := w.Create(fn)
24-
checkError(err)
25-
26-
// Write .nuspec bytes to file
27-
_, err = f.Write([]byte(b))
28-
checkError(err)
29-
}
30-
3116
func cliPackNupkg(c *cli.Context) error {
3217

3318
// Get filename from command line
@@ -61,7 +46,7 @@ func cliPackNupkg(c *cli.Context) error {
6146
outputPath = op
6247
}
6348

64-
b, err := PackNupkg(ns, basePath, outputPath)
49+
b, err := nuget.PackNupkg(ns, basePath, outputPath)
6550
checkError(err)
6651

6752
// Override Version if option is set
@@ -87,116 +72,3 @@ func cliPackNupkg(c *cli.Context) error {
8772

8873
return nil
8974
}
90-
91-
// PackNupkg produces a .nupkg file in byte format
92-
func PackNupkg(ns *nuspec.NuSpec, basePath string, outputPath string) ([]byte, error) {
93-
94-
// Assume filename from ID
95-
nsfilename := ns.Meta.ID + ".nuspec"
96-
97-
// Create a buffer to write our archive to.
98-
buf := new(bytes.Buffer)
99-
100-
// Create a new zip archive
101-
w := zip.NewWriter(buf)
102-
defer w.Close()
103-
104-
// Create a new Contenttypes Structure
105-
ct := NewContentTypes()
106-
107-
// Add .nuspec to Archive
108-
b, err := ns.ToBytes()
109-
checkError(err)
110-
archiveFile(filepath.Base(nsfilename), w, b)
111-
ct.Add(filepath.Ext(nsfilename))
112-
113-
// Process files
114-
// If there are no file globs specified then
115-
if len(ns.Files.File) == 0 {
116-
// walk the basePath and zip up all found files. Everything.]
117-
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
118-
if !info.IsDir() && filepath.Base(path) != filepath.Base(nsfilename) {
119-
// Open the file
120-
x, err := os.Open(path)
121-
checkError(err)
122-
// Gather all contents
123-
y, err := ioutil.ReadAll(x)
124-
checkError(err)
125-
// Set relative path for file in archive
126-
p, err := filepath.Rel(basePath, path)
127-
checkError(err)
128-
// Store the file
129-
archiveFile(p, w, y)
130-
// Add extension to the Rels file
131-
ct.Add(filepath.Ext(p))
132-
}
133-
return nil
134-
})
135-
checkError(err)
136-
} else {
137-
// For each of the specified globs, get files an put in target
138-
for _, f := range ns.Files.File {
139-
// Apply glob, cater for
140-
matches, err := filepath.Glob(filepath.ToSlash(filepath.Join(basePath, f.Source)))
141-
checkError(err)
142-
for _, m := range matches {
143-
info, err := os.Stat(m)
144-
if !info.IsDir() && filepath.Base(m) != filepath.Base(nsfilename) {
145-
// Open the file
146-
x, err := os.Open(m)
147-
checkError(err)
148-
// Gather all contents
149-
y, err := ioutil.ReadAll(x)
150-
checkError(err)
151-
// Set relative path for file in archive
152-
p, err := filepath.Rel(basePath, m)
153-
checkError(err)
154-
// Overide path if Target is set
155-
if f.Target != "" {
156-
p = filepath.Join(f.Target, filepath.Base(m))
157-
}
158-
// Store the file
159-
archiveFile(p, w, y)
160-
// Add extension to the Rels file
161-
ct.Add(filepath.Ext(p))
162-
}
163-
checkError(err)
164-
}
165-
}
166-
}
167-
168-
// Create and add .psmdcp file to Archive
169-
pf := NewPsmdcpFile()
170-
pf.Creator = ns.Meta.Authors
171-
pf.Description = ns.Meta.Description
172-
pf.Identifier = ns.Meta.ID
173-
pf.Version = ns.Meta.Version
174-
pf.Keywords = ns.Meta.Tags
175-
pf.LastModifiedBy = "go-nuget"
176-
b, err = pf.ToBytes()
177-
checkError(err)
178-
pfn := "package/services/metadata/core-properties/" + randomString(32) + ".psmdcp"
179-
archiveFile(pfn, w, b)
180-
ct.Add(filepath.Ext(pfn))
181-
182-
// Create and add .rels to Archive
183-
rf := NewRelFile()
184-
rf.Add("http://schemas.microsoft.com/packaging/2010/07/manifest", "/"+filepath.Base(nsfilename))
185-
rf.Add("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties", pfn)
186-
187-
b, err = rf.ToBytes()
188-
checkError(err)
189-
archiveFile(filepath.Join("_rels", ".rels"), w, b)
190-
ct.Add(filepath.Ext(".rels"))
191-
192-
// Add [Content_Types].xml to Archive
193-
b, err = ct.ToBytes()
194-
checkError(err)
195-
archiveFile(`[Content_Types].xml`, w, b)
196-
197-
// Close the zipwriter
198-
w.Close()
199-
200-
// Return
201-
return buf.Bytes(), nil
202-
}

push.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package main
22

33
import (
4-
"bytes"
54
"errors"
65
"fmt"
76
"io/ioutil"
8-
"mime/multipart"
97
"net/http"
10-
"time"
118

9+
nuget "github.com/soloworks/go-nuget"
1210
"github.com/urfave/cli"
1311
)
1412

@@ -27,7 +25,7 @@ func cliPushNupkg(c *cli.Context) error {
2725
// print out for CLI
2826
fmt.Println("Pushing " + filename + " to '" + c.String("Source") + "'...")
2927
fmt.Println(" ", http.MethodPut, c.String("Source"))
30-
status, dur, err := PushNupkg(fileContents, c.String("ApiKey"), c.String("Source"))
28+
status, dur, err := nuget.PushNupkg(fileContents, c.String("ApiKey"), c.String("Source"))
3129
checkError(err)
3230

3331
// print out for CLI
@@ -43,45 +41,3 @@ func cliPushNupkg(c *cli.Context) error {
4341
// Return Ok
4442
return nil
4543
}
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-
55-
// Create MultiPart Writer
56-
body := new(bytes.Buffer)
57-
w := multipart.NewWriter(body)
58-
// Create new File part
59-
p, err := w.CreateFormFile("package", "package.nupkg")
60-
checkError(err)
61-
// Write contents to part
62-
_, err = p.Write(fileContents)
63-
checkError(err)
64-
// Close the writer
65-
err = w.Close()
66-
checkError(err)
67-
68-
// Create new PUT request
69-
request, err := http.NewRequest(http.MethodPut, host, body)
70-
checkError(err)
71-
// Add the ApiKey if supplied
72-
if apiKey != "" {
73-
request.Header.Add("X-Nuget-Apikey", apiKey)
74-
}
75-
// Add the Content Type header from the reader - includes boundary
76-
request.Header.Add("Content-Type", w.FormDataContentType())
77-
78-
// Push to the server
79-
startTime := time.Now()
80-
client := &http.Client{}
81-
resp, err := client.Do(request)
82-
checkError(err)
83-
duration := time.Now().Sub(startTime)
84-
85-
// Return Results
86-
return resp.StatusCode, duration.Milliseconds(), nil
87-
}

spec.go

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"io/ioutil"
77
"os"
88
"os/user"
9-
"time"
109

11-
nuspec "github.com/soloworks/go-nuspec"
10+
nuget "github.com/soloworks/go-nuget"
1211
"github.com/urfave/cli"
1312
)
1413

@@ -19,7 +18,7 @@ func cliSampleNuSpec(c *cli.Context) error {
1918
checkError(err)
2019

2120
// Get the NuSpec
22-
ns := SampleNuSpec(c.Args().First(), user.Name)
21+
ns := nuget.SampleNuSpec(c.Args().First(), user.Name)
2322

2423
// Set filename string
2524
fn := ns.Meta.ID + ".nuspec"
@@ -44,33 +43,3 @@ func cliSampleNuSpec(c *cli.Context) error {
4443

4544
return nil
4645
}
47-
48-
// SampleNuSpec returns a populated sample NuSpec struct
49-
func SampleNuSpec(id string, user string) *nuspec.NuSpec {
50-
51-
// Create a new structure
52-
n := nuspec.New()
53-
54-
// Populate Defaults
55-
n.Meta.ID = "Package"
56-
n.Meta.Version = "1.0.0"
57-
n.Meta.Authors = user
58-
n.Meta.Owners = user
59-
n.Meta.LicenseURL = "http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE"
60-
n.Meta.ProjectURL = "http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE"
61-
n.Meta.IconURL = "http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE"
62-
n.Meta.ReqLicenseAccept = false
63-
n.Meta.Description = "Package Description"
64-
n.Meta.ReleaseNotes = "Summary of changes made in this release of the package."
65-
n.Meta.Copyright = "Copyright " + time.Now().Format("2006")
66-
n.Meta.Tags = "Tag1 Tag2"
67-
d := nuspec.Dependency{ID: "SampleDependency", Version: "1.0"}
68-
n.Meta.Dependencies.Dependency = append(n.Meta.Dependencies.Dependency, d)
69-
70-
// Override package ID if present
71-
if id != "" {
72-
n.Meta.ID = id
73-
}
74-
75-
return n
76-
}

0 commit comments

Comments
 (0)