Skip to content

Commit

Permalink
create option struct so that the inputs on "New" is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaardsholt committed Oct 7, 2020
1 parent 4f45341 commit 791e7d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
client := github.NewClient(tc)

//
ghr, err := releasediff.New(client, "goharbor", "harbor", "v2.0.2", "", "", false, false)
ghr, err := releasediff.New(client, "goharbor", "harbor", "v2.0.2", "", nil)
if err != nil {
panic(err)
}
Expand Down
23 changes: 19 additions & 4 deletions releasediff.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ func isRelase(client *github.Client, owner string, repo string, release string,
return true
}

type Options struct {
Filter string
IncludePreReleases bool
VerifyRelease bool
}

// New creates a new GitHubReleases
func New(client *github.Client, owner string, repo string, release1 string, release2 string, filter string, includePreReleases bool, verifyRelease bool) (*GitHubReleases, error) {
func New(client *github.Client, owner string, repo string, release1 string, release2 string, options *Options) (*GitHubReleases, error) {
missingFields := []string{}
if owner == "" {
missingFields = append([]string{"Owner"}, missingFields...)
Expand All @@ -50,8 +56,17 @@ func New(client *github.Client, owner string, repo string, release1 string, rele
return nil, fmt.Errorf("Missing required field(s): %s", missingFields)
}

var verify bool
var prerelease bool
var filter string
if options != nil {
verify = options.VerifyRelease
prerelease = options.IncludePreReleases
filter = options.Filter
}

// Check if Release1 is a valid release
if !isRelase(client, owner, repo, release1, verifyRelease) {
if !isRelase(client, owner, repo, release1, verify) {
return nil, fmt.Errorf("'%s' is not a release on %s/%s", release1, owner, repo)
}

Expand All @@ -64,7 +79,7 @@ func New(client *github.Client, owner string, repo string, release1 string, rele
release2 = latest.GetTagName()
} else {
// Check if Release2 is a valid release
if !isRelase(client, owner, repo, release1, verifyRelease) {
if !isRelase(client, owner, repo, release1, verify) {
return nil, fmt.Errorf("'%s' is not a release on %s/%s", release2, owner, repo)
}
}
Expand All @@ -75,7 +90,7 @@ func New(client *github.Client, owner string, repo string, release1 string, rele
Release1: release1,
Release2: release2,
Filter: filter,
IncludePreReleases: includePreReleases,
IncludePreReleases: prerelease,
Client: client,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion releasediff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
go func(name string, version string) {
defer wg.Done()

ghr, err := New(client, "goharbor", name, version, "", "", false, false)
ghr, err := New(client, "goharbor", name, version, "", nil)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 791e7d9

Please sign in to comment.