Skip to content

Commit e33adc7

Browse files
committed
modernize Go code
Signed-off-by: Taylor Silva <dev@taydev.net>
1 parent 08dbac1 commit e33adc7

9 files changed

Lines changed: 87 additions & 56 deletions

github.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"github.com/google/go-github/v66/github"
1515
"github.com/shurcooL/githubv4"
1616
"golang.org/x/oauth2"
17-
18-
_ "github.com/maxbrunsfeld/counterfeiter/v6"
1917
)
2018

2119
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_git_hub.go . GitHub

github_graphql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (g *GitHubClient) listReleasesV4EnterPrice() ([]*github.RepositoryRelease,
3232
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
3333
}
3434

35-
vars := map[string]interface{}{
35+
vars := map[string]any{
3636
"repositoryOwner": githubv4.String(g.owner),
3737
"repositoryName": githubv4.String(g.repository),
3838
"releaseCursor": (*githubv4.String)(nil),
@@ -103,7 +103,7 @@ func (g *GitHubClient) listReleasesV4() ([]*github.RepositoryRelease, error) {
103103
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
104104
}
105105

106-
vars := map[string]interface{}{
106+
vars := map[string]any{
107107
"repositoryOwner": githubv4.String(g.owner),
108108
"repositoryName": githubv4.String(g.repository),
109109
"releaseCursor": (*githubv4.String)(nil),

github_test.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package resource_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/url"
88
"time"
@@ -276,9 +276,36 @@ var _ = Describe("GitHub Client", func() {
276276
Expect(releases).To(HaveLen(3))
277277
Expect(server.ReceivedRequests()).To(HaveLen(2))
278278
Expect(releases).To(Equal([]*github.RepositoryRelease{
279-
{TagName: github.String("xyz"), Name: github.String("xyz"), Draft: github.Bool(false), Prerelease: github.Bool(false), ID: github.Int64(32095103), CreatedAt: &github.Timestamp{time.Date(2010, time.October, 01, 00, 58, 07, 0, time.UTC)}, PublishedAt: &github.Timestamp{time.Date(2010, time.October, 02, 15, 39, 53, 0, time.UTC)}, URL: github.String("https://github.com/xyz/xyz/releases/tag/xyz")},
280-
{TagName: github.String("xyz"), Name: github.String("xyz"), Draft: github.Bool(false), Prerelease: github.Bool(false), ID: github.Int64(30230659), CreatedAt: &github.Timestamp{time.Date(2010, time.August, 27, 13, 55, 36, 0, time.UTC)}, PublishedAt: &github.Timestamp{time.Date(2010, time.August, 27, 17, 18, 06, 0, time.UTC)}, URL: github.String("https://github.com/xyz/xyz/releases/tag/xyz")},
281-
{TagName: github.String("xyq"), Name: github.String("xyq"), Draft: github.Bool(false), Prerelease: github.Bool(false), ID: github.Int64(33222243), CreatedAt: &github.Timestamp{time.Date(2010, time.October, 10, 01, 01, 07, 0, time.UTC)}, PublishedAt: &github.Timestamp{time.Date(2010, time.October, 10, 15, 39, 53, 0, time.UTC)}, URL: github.String("https://github.com/xyq/xyq/releases/tag/xyq")},
279+
{
280+
TagName: github.String("xyz"),
281+
Name: github.String("xyz"),
282+
Draft: github.Bool(false),
283+
Prerelease: github.Bool(false),
284+
ID: github.Int64(32095103),
285+
CreatedAt: &github.Timestamp{Time: time.Date(2010, time.October, 01, 00, 58, 07, 0, time.UTC)},
286+
PublishedAt: &github.Timestamp{Time: time.Date(2010, time.October, 02, 15, 39, 53, 0, time.UTC)},
287+
URL: github.String("https://github.com/xyz/xyz/releases/tag/xyz"),
288+
},
289+
{
290+
TagName: github.String("xyz"),
291+
Name: github.String("xyz"),
292+
Draft: github.Bool(false),
293+
Prerelease: github.Bool(false),
294+
ID: github.Int64(30230659),
295+
CreatedAt: &github.Timestamp{Time: time.Date(2010, time.August, 27, 13, 55, 36, 0, time.UTC)},
296+
PublishedAt: &github.Timestamp{Time: time.Date(2010, time.August, 27, 17, 18, 06, 0, time.UTC)},
297+
URL: github.String("https://github.com/xyz/xyz/releases/tag/xyz"),
298+
},
299+
{
300+
TagName: github.String("xyq"),
301+
Name: github.String("xyq"),
302+
Draft: github.Bool(false),
303+
Prerelease: github.Bool(false),
304+
ID: github.Int64(33222243),
305+
CreatedAt: &github.Timestamp{Time: time.Date(2010, time.October, 10, 01, 01, 07, 0, time.UTC)},
306+
PublishedAt: &github.Timestamp{Time: time.Date(2010, time.October, 10, 15, 39, 53, 0, time.UTC)},
307+
URL: github.String("https://github.com/xyq/xyq/releases/tag/xyq"),
308+
},
282309
}))
283310
})
284311
})
@@ -574,7 +601,7 @@ var _ = Describe("GitHub Client", func() {
574601
Expect(err).NotTo(HaveOccurred())
575602
defer readCloser.Close()
576603

577-
body, err := ioutil.ReadAll(readCloser)
604+
body, err := io.ReadAll(readCloser)
578605
Expect(err).NotTo(HaveOccurred())
579606
Expect(string(body)).To(Equal(fileContents))
580607
})
@@ -613,7 +640,7 @@ var _ = Describe("GitHub Client", func() {
613640
Expect(err).NotTo(HaveOccurred())
614641
defer readCloser.Close()
615642

616-
body, err := ioutil.ReadAll(readCloser)
643+
body, err := io.ReadAll(readCloser)
617644
Expect(err).NotTo(HaveOccurred())
618645
Expect(string(body)).To(Equal(redirectFileContents))
619646
})
@@ -635,7 +662,7 @@ var _ = Describe("GitHub Client", func() {
635662
Expect(err).NotTo(HaveOccurred())
636663
defer readCloser.Close()
637664

638-
body, err := ioutil.ReadAll(readCloser)
665+
body, err := io.ReadAll(readCloser)
639666
Expect(err).NotTo(HaveOccurred())
640667
Expect(string(body)).To(Equal(redirectFileContents))
641668
})
@@ -663,7 +690,7 @@ var _ = Describe("GitHub Client", func() {
663690
Expect(err).NotTo(HaveOccurred())
664691
defer readCloser.Close()
665692

666-
body, err := ioutil.ReadAll(readCloser)
693+
body, err := io.ReadAll(readCloser)
667694
Expect(err).NotTo(HaveOccurred())
668695
Expect(string(body)).To(Equal(redirectFileContents))
669696
})
@@ -745,7 +772,7 @@ var _ = Describe("GitHub Client", func() {
745772
Expect(err).NotTo(HaveOccurred())
746773
defer readCloser.Close()
747774

748-
body, err := ioutil.ReadAll(readCloser)
775+
body, err := io.ReadAll(readCloser)
749776
Expect(err).NotTo(HaveOccurred())
750777
Expect(string(body)).To(Equal(redirectFileContents))
751778
})

in_command.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"os"
109
"path/filepath"
@@ -59,15 +58,15 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
5958

6059
if foundRelease.HTMLURL != nil && *foundRelease.HTMLURL != "" {
6160
urlPath := filepath.Join(destDir, "url")
62-
err = ioutil.WriteFile(urlPath, []byte(*foundRelease.HTMLURL), 0644)
61+
err = os.WriteFile(urlPath, []byte(*foundRelease.HTMLURL), 0644)
6362
if err != nil {
6463
return InResponse{}, err
6564
}
6665
}
6766

6867
if foundRelease.TagName != nil && *foundRelease.TagName != "" {
6968
tagPath := filepath.Join(destDir, "tag")
70-
err = ioutil.WriteFile(tagPath, []byte(*foundRelease.TagName), 0644)
69+
err = os.WriteFile(tagPath, []byte(*foundRelease.TagName), 0644)
7170
if err != nil {
7271
return InResponse{}, err
7372
}
@@ -78,7 +77,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
7877
}
7978
version := versionParser.parse(*foundRelease.TagName)
8079
versionPath := filepath.Join(destDir, "version")
81-
err = ioutil.WriteFile(versionPath, []byte(version), 0644)
80+
err = os.WriteFile(versionPath, []byte(version), 0644)
8281
if err != nil {
8382
return InResponse{}, err
8483
}
@@ -91,7 +90,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
9190
}
9291

9392
if commitSHA != "" {
94-
err = ioutil.WriteFile(commitPath, []byte(commitSHA), 0644)
93+
err = os.WriteFile(commitPath, []byte(commitSHA), 0644)
9594
if err != nil {
9695
return InResponse{}, err
9796
}
@@ -101,7 +100,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
101100
if foundRelease.Body != nil && *foundRelease.Body != "" {
102101
body := *foundRelease.Body
103102
bodyPath := filepath.Join(destDir, "body")
104-
err = ioutil.WriteFile(bodyPath, []byte(body), 0644)
103+
err = os.WriteFile(bodyPath, []byte(body), 0644)
105104
if err != nil {
106105
return InResponse{}, err
107106
}
@@ -113,7 +112,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
113112
if err != nil {
114113
return InResponse{}, err
115114
}
116-
err = ioutil.WriteFile(timestampPath, timestamp, 0644)
115+
err = os.WriteFile(timestampPath, timestamp, 0644)
117116
if err != nil {
118117
return InResponse{}, err
119118
}

in_command_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package resource_test
33
import (
44
"bytes"
55
"errors"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/url"
99
"os"
@@ -40,14 +40,14 @@ var _ = Describe("In Command", func() {
4040

4141
githubClient = &fakes.FakeGitHub{}
4242
githubServer = ghttp.NewServer()
43-
command = resource.NewInCommand(githubClient, ioutil.Discard)
43+
command = resource.NewInCommand(githubClient, io.Discard)
4444

45-
tmpDir, err = ioutil.TempDir("", "github-release")
45+
tmpDir, err = os.MkdirTemp("", "github-release")
4646
Ω(err).ShouldNot(HaveOccurred())
4747

4848
destDir = filepath.Join(tmpDir, "destination")
4949

50-
githubClient.DownloadReleaseAssetReturns(ioutil.NopCloser(bytes.NewBufferString("some-content")), nil)
50+
githubClient.DownloadReleaseAssetReturns(io.NopCloser(bytes.NewBufferString("some-content")), nil)
5151

5252
inRequest = resource.InRequest{}
5353
})
@@ -167,27 +167,27 @@ var _ = Describe("In Command", func() {
167167
It("does create the body, tag, version, and url files", func() {
168168
inResponse, inErr = command.Run(destDir, inRequest)
169169

170-
contents, err := ioutil.ReadFile(path.Join(destDir, "tag"))
170+
contents, err := os.ReadFile(path.Join(destDir, "tag"))
171171
Ω(err).ShouldNot(HaveOccurred())
172172
Ω(string(contents)).Should(Equal("v0.35.0"))
173173

174-
contents, err = ioutil.ReadFile(path.Join(destDir, "version"))
174+
contents, err = os.ReadFile(path.Join(destDir, "version"))
175175
Ω(err).ShouldNot(HaveOccurred())
176176
Ω(string(contents)).Should(Equal("0.35.0"))
177177

178-
contents, err = ioutil.ReadFile(path.Join(destDir, "commit_sha"))
178+
contents, err = os.ReadFile(path.Join(destDir, "commit_sha"))
179179
Ω(err).ShouldNot(HaveOccurred())
180180
Ω(string(contents)).Should(Equal("f28085a4a8f744da83411f5e09fd7b1709149eee"))
181181

182-
contents, err = ioutil.ReadFile(path.Join(destDir, "body"))
182+
contents, err = os.ReadFile(path.Join(destDir, "body"))
183183
Ω(err).ShouldNot(HaveOccurred())
184184
Ω(string(contents)).Should(Equal("*markdown*"))
185185

186-
contents, err = ioutil.ReadFile(path.Join(destDir, "timestamp"))
186+
contents, err = os.ReadFile(path.Join(destDir, "timestamp"))
187187
Ω(err).ShouldNot(HaveOccurred())
188188
Ω(string(contents)).Should(Equal("2018-01-01T00:00:00Z"))
189189

190-
contents, err = ioutil.ReadFile(path.Join(destDir, "url"))
190+
contents, err = os.ReadFile(path.Join(destDir, "url"))
191191
Ω(err).ShouldNot(HaveOccurred())
192192
Ω(string(contents)).Should(Equal("http://google.com"))
193193
})
@@ -211,15 +211,15 @@ var _ = Describe("In Command", func() {
211211
It("does create the tag, version, and url files", func() {
212212
inResponse, inErr = command.Run(destDir, inRequest)
213213

214-
contents, err := ioutil.ReadFile(path.Join(destDir, "tag"))
214+
contents, err := os.ReadFile(path.Join(destDir, "tag"))
215215
Ω(err).ShouldNot(HaveOccurred())
216216
Ω(string(contents)).Should(Equal("package-0.35.0"))
217217

218-
contents, err = ioutil.ReadFile(path.Join(destDir, "version"))
218+
contents, err = os.ReadFile(path.Join(destDir, "version"))
219219
Ω(err).ShouldNot(HaveOccurred())
220220
Ω(string(contents)).Should(Equal("0.35.0"))
221221

222-
contents, err = ioutil.ReadFile(path.Join(destDir, "url"))
222+
contents, err = os.ReadFile(path.Join(destDir, "url"))
223223
Ω(err).ShouldNot(HaveOccurred())
224224
Ω(string(contents)).Should(Equal("http://google.com"))
225225
})
@@ -265,7 +265,7 @@ var _ = Describe("In Command", func() {
265265
It("saves the source tarball in the destination directory", func() {
266266
inResponse, inErr = command.Run(destDir, inRequest)
267267

268-
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz"))
268+
fileContents, err := os.ReadFile(filepath.Join(destDir, "source.tar.gz"))
269269
fContents := string(fileContents)
270270
Expect(err).NotTo(HaveOccurred())
271271
Expect(fContents).To(Equal("source-tar-file-contents"))
@@ -275,7 +275,7 @@ var _ = Describe("In Command", func() {
275275
inRequest.Source.AssetDir = true
276276
inResponse, inErr = command.Run(destDir, inRequest)
277277

278-
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "assets", "source.tar.gz"))
278+
fileContents, err := os.ReadFile(filepath.Join(destDir, "assets", "source.tar.gz"))
279279
fContents := string(fileContents)
280280
Expect(err).NotTo(HaveOccurred())
281281
Expect(fContents).To(Equal("source-tar-file-contents"))
@@ -355,7 +355,7 @@ var _ = Describe("In Command", func() {
355355
It("saves the source zip in the destination directory", func() {
356356
inResponse, inErr = command.Run(destDir, inRequest)
357357

358-
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.zip"))
358+
fileContents, err := os.ReadFile(filepath.Join(destDir, "source.zip"))
359359
fContents := string(fileContents)
360360
Expect(err).NotTo(HaveOccurred())
361361
Expect(fContents).To(Equal("source-zip-file-contents"))
@@ -365,7 +365,7 @@ var _ = Describe("In Command", func() {
365365
inRequest.Source.AssetDir = true
366366
inResponse, inErr = command.Run(destDir, inRequest)
367367

368-
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "assets", "source.zip"))
368+
fileContents, err := os.ReadFile(filepath.Join(destDir, "assets", "source.zip"))
369369
fContents := string(fileContents)
370370
Expect(err).NotTo(HaveOccurred())
371371
Expect(fContents).To(Equal("source-zip-file-contents"))
@@ -525,15 +525,15 @@ var _ = Describe("In Command", func() {
525525
})
526526

527527
It("does create the tag, version, and URL files", func() {
528-
contents, err := ioutil.ReadFile(path.Join(destDir, "tag"))
528+
contents, err := os.ReadFile(path.Join(destDir, "tag"))
529529
Ω(err).ShouldNot(HaveOccurred())
530530
Ω(string(contents)).Should(Equal("v0.35.0"))
531531

532-
contents, err = ioutil.ReadFile(path.Join(destDir, "version"))
532+
contents, err = os.ReadFile(path.Join(destDir, "version"))
533533
Ω(err).ShouldNot(HaveOccurred())
534534
Ω(string(contents)).Should(Equal("0.35.0"))
535535

536-
contents, err = ioutil.ReadFile(path.Join(destDir, "url"))
536+
contents, err = os.ReadFile(path.Join(destDir, "url"))
537537
Ω(err).ShouldNot(HaveOccurred())
538538
Ω(string(contents)).Should(Equal("http://google.com"))
539539
})
@@ -572,7 +572,7 @@ var _ = Describe("In Command", func() {
572572
})
573573

574574
It("does create the url file", func() {
575-
contents, err := ioutil.ReadFile(path.Join(destDir, "url"))
575+
contents, err := os.ReadFile(path.Join(destDir, "url"))
576576
Ω(err).ShouldNot(HaveOccurred())
577577
Ω(string(contents)).Should(Equal("http://google.com"))
578578
})
@@ -610,7 +610,7 @@ var _ = Describe("In Command", func() {
610610
})
611611

612612
It("does create the url file", func() {
613-
contents, err := ioutil.ReadFile(path.Join(destDir, "url"))
613+
contents, err := os.ReadFile(path.Join(destDir, "url"))
614614
Ω(err).ShouldNot(HaveOccurred())
615615
Ω(string(contents)).Should(Equal("http://google.com"))
616616
})

out_command.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package resource
33
import (
44
"fmt"
55
"io"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"strings"
@@ -153,7 +152,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
153152
}
154153

155154
func (c *OutCommand) fileContents(path string) (string, error) {
156-
contents, err := ioutil.ReadFile(path)
155+
contents, err := os.ReadFile(path)
157156
if err != nil {
158157
return "", err
159158
}
@@ -167,7 +166,7 @@ func (c *OutCommand) upload(release *github.RepositoryRelease, filePath string)
167166
name := filepath.Base(filePath)
168167

169168
var retryErr error
170-
for i := 0; i < 10; i++ {
169+
for range 10 {
171170
file, err := os.Open(filePath)
172171
if err != nil {
173172
return err

0 commit comments

Comments
 (0)