Skip to content

Commit

Permalink
Dedup code and migrate away from deprecated funcs (#1141)
Browse files Browse the repository at this point in the history
Co-authored-by: Anbraten <anton@ju60.de>
  • Loading branch information
6543 and anbraten authored Aug 29, 2022
1 parent ca84f70 commit 08a9915
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 113 deletions.
7 changes: 7 additions & 0 deletions cli/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ func FormatFlag(tmpl string, hidden ...bool) *cli.StringFlag {
Hidden: len(hidden) != 0,
}
}

// specify repository
var RepoFlag = &cli.StringFlag{
Name: "repository",
Aliases: []string{"repo"},
Usage: "repository name (e.g. octocat/hello-world)",
}
9 changes: 3 additions & 6 deletions cli/registry/registry_add.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package registry

import (
"io/ioutil"
"os"
"strings"

"github.com/urfave/cli/v2"
Expand All @@ -17,10 +17,7 @@ var registryCreateCmd = &cli.Command{
ArgsUsage: "[repo/name]",
Action: registryCreate,
Flags: append(common.GlobalFlags,
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Expand Down Expand Up @@ -62,7 +59,7 @@ func registryCreate(c *cli.Context) error {
}
if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path)
out, ferr := os.ReadFile(path)
if ferr != nil {
return ferr
}
Expand Down
5 changes: 1 addition & 4 deletions cli/registry/registry_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ var registryInfoCmd = &cli.Command{
ArgsUsage: "[repo/name]",
Action: registryInfo,
Flags: append(common.GlobalFlags,
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Expand Down
5 changes: 1 addition & 4 deletions cli/registry/registry_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ var registryListCmd = &cli.Command{
ArgsUsage: "[repo/name]",
Action: registryList,
Flags: append(common.GlobalFlags,
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
common.FormatFlag(tmplRegistryList, true),
),
}
Expand Down
5 changes: 1 addition & 4 deletions cli/registry/registry_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ var registryDeleteCmd = &cli.Command{
ArgsUsage: "[repo/name]",
Action: registryDelete,
Flags: append(common.GlobalFlags,
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Expand Down
9 changes: 3 additions & 6 deletions cli/registry/registry_set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package registry

import (
"io/ioutil"
"os"
"strings"

"github.com/urfave/cli/v2"
Expand All @@ -17,10 +17,7 @@ var registryUpdateCmd = &cli.Command{
ArgsUsage: "[repo/name]",
Action: registryUpdate,
Flags: append(common.GlobalFlags,
&cli.StringFlag{
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.RepoFlag,
&cli.StringFlag{
Name: "hostname",
Usage: "registry hostname",
Expand Down Expand Up @@ -62,7 +59,7 @@ func registryUpdate(c *cli.Context) error {
}
if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path)
out, ferr := os.ReadFile(path)
if ferr != nil {
return ferr
}
Expand Down
4 changes: 2 additions & 2 deletions pipeline/multipart/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package multipart

import (
"bytes"
"io/ioutil"
"io"
"testing"
)

Expand All @@ -20,7 +20,7 @@ func TestReader(t *testing.T) {
if got, want := header.Get("Content-Type"), "text/plain"; got != want {
t.Errorf("Want Content-Type %q, got %q", want, got)
}
body, err := ioutil.ReadAll(part)
body, err := io.ReadAll(part)
if err != nil {
t.Error(err)
return
Expand Down
3 changes: 1 addition & 2 deletions server/plugins/utils/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -53,7 +52,7 @@ func Send(ctx context.Context, method, path string, privateKey crypto.PrivateKey
defer resp.Body.Close()

if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, err
}
Expand Down
3 changes: 1 addition & 2 deletions server/remote/bitbucket/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -252,7 +251,7 @@ func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error)
return nil, json.NewDecoder(resp.Body).Decode(out)
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions server/remote/bitbucket/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package bitbucket

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"

"github.com/woodpecker-ci/woodpecker/server/model"
Expand All @@ -34,7 +34,7 @@ const (
// parseHook parses a Bitbucket hook from an http.Request request and returns
// Repo and Build detail. If a hook type is unsupported nil values are returned.
func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
payload, _ := ioutil.ReadAll(r.Body)
payload, _ := io.ReadAll(r.Body)

switch r.Header.Get(hookEvent) {
case hookPush:
Expand Down
4 changes: 2 additions & 2 deletions server/remote/bitbucketserver/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/mrjones/oauth"

Expand Down Expand Up @@ -86,7 +86,7 @@ func New(opts Opts) (remote.Remote, error) {
var keyFileBytes []byte
if opts.ConsumerRSA != "" {
var err error
keyFileBytes, err = ioutil.ReadFile(opts.ConsumerRSA)
keyFileBytes, err = os.ReadFile(opts.ConsumerRSA)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions server/remote/bitbucketserver/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (c *Client) FindCurrentUser() (*User, error) {
return nil, err
}

bits, err := ioutil.ReadAll(CurrentUserIDResponse.Body)
bits, err := io.ReadAll(CurrentUserIDResponse.Body)
if err != nil {
return nil, err
}
Expand All @@ -92,7 +91,7 @@ func (c *Client) FindCurrentUser() (*User, error) {
return nil, err
}

contents, err := ioutil.ReadAll(CurrentUserResponse.Body)
contents, err := io.ReadAll(CurrentUserResponse.Body)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +114,7 @@ func (c *Client) FindRepo(owner, name string) (*Repo, error) {
if err != nil {
log.Err(err).Msg("")
}
contents, err := ioutil.ReadAll(response.Body)
contents, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +161,7 @@ func (c *Client) FindFileForRepo(owner, repo, fileName, ref string) ([]byte, err
if response.StatusCode == 404 {
return nil, nil
}
responseBytes, err := ioutil.ReadAll(response.Body)
responseBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Err(err).Msg("")
}
Expand Down
4 changes: 2 additions & 2 deletions server/remote/coding/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package coding
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -94,7 +94,7 @@ type MergeRequestHook struct {
}

func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
raw, err := ioutil.ReadAll(r.Body)
raw, err := io.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
return nil, nil, err
Expand Down
4 changes: 2 additions & 2 deletions server/remote/coding/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package coding

import (
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
Expand All @@ -30,7 +30,7 @@ func Test_hook(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Coding hook", func() {
g.It("Should parse hook", func() {
reader := ioutil.NopCloser(strings.NewReader(fixtures.PushHook))
reader := io.NopCloser(strings.NewReader(fixtures.PushHook))
r := &http.Request{
Header: map[string][]string{
hookEvent: {hookPush},
Expand Down
4 changes: 2 additions & 2 deletions server/remote/coding/internal/coding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *Client) Do(method, u string, params url.Values) ([]byte, error) {
return nil, fmt.Errorf("%s %s respond %d", req.Method, req.URL, resp.StatusCode)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("fail to read response from %s %s: %v", req.Method, req.URL.String(), err)
}
Expand Down
3 changes: 1 addition & 2 deletions server/remote/github/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"

Expand Down Expand Up @@ -46,7 +45,7 @@ func parseHook(r *http.Request, merge bool) (*github.PullRequest, *model.Repo, *
reader = bytes.NewBufferString(payload)
}

raw, err := ioutil.ReadAll(reader)
raw, err := io.ReadAll(reader)
if err != nil {
return nil, nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion server/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

package remote

//go:generate mockery -name Remote -output mocks -case=underscore
//go:generate go install github.com/vektra/mockery/v2@latest
//go:generate mockery --name Remote --output mocks --case underscore

import (
"context"
Expand Down
4 changes: 2 additions & 2 deletions server/shared/configFetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestFetchFromConfigService(t *testing.T) {
}

var req incoming
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "can't read body", http.StatusBadRequest)
return
Expand Down
Loading

0 comments on commit 08a9915

Please sign in to comment.