Skip to content

Commit b55137d

Browse files
authored
Refactor project layout for better go build and get ergonomics (#20)
1 parent 21ac6e3 commit b55137d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+575
-433
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- checkout
4444
- run:
4545
<<: *install_gruntwork_utils
46-
- run: build-go-binaries --app-name git-xargs --src-path ./cmd --dest-path bin --ld-flags "-X main.VERSION=$CIRCLE_TAG"
46+
- run: build-go-binaries --app-name git-xargs --src-path ./ --dest-path bin --ld-flags "-X main.VERSION=$CIRCLE_TAG"
4747
- run: cd bin && sha256sum * > SHA256SUMS
4848
- run: upload-github-release-assets bin/*
4949
workflows:

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
git-xargs
21
test-repo/
32
.idea
4-
*.iml
3+
*.iml
4+
git-xargs

README.md

Lines changed: 95 additions & 60 deletions
Large diffs are not rendered by default.

cmd/auth.go renamed to auth/auth.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package main
1+
package auth
22

33
import (
44
"context"
55
"os"
66

77
"github.com/google/go-github/v32/github"
8+
"github.com/gruntwork-io/git-xargs/types"
89
"github.com/gruntwork-io/go-commons/errors"
910

1011
"golang.org/x/oauth2"
@@ -39,7 +40,7 @@ func NewClient(client *github.Client) GithubClient {
3940
}
4041

4142
// configureGithubClient creates a Github API client using the user-supplied GITHUB_OAUTH_TOKEN and return the configured Github client
42-
func configureGithubClient() GithubClient {
43+
func ConfigureGithubClient() GithubClient {
4344
// Ensure user provided a GITHUB_OAUTH_TOKEN
4445
GithubOauthToken := os.Getenv("GITHUB_OAUTH_TOKEN")
4546

@@ -56,9 +57,9 @@ func configureGithubClient() GithubClient {
5657
}
5758

5859
// ensureGithubOauthTokenSet is a sanity check that a value is exported for GITHUB_OAUTH_TOKEN
59-
func ensureGithubOauthTokenSet() error {
60+
func EnsureGithubOauthTokenSet() error {
6061
if os.Getenv("GITHUB_OAUTH_TOKEN") == "" {
61-
return errors.WithStackTrace(NoGithubOauthTokenProvidedErr{})
62+
return errors.WithStackTrace(types.NoGithubOauthTokenProvidedErr{})
6263
}
6364
return nil
6465
}

cmd/auth_test.go renamed to auth/auth_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package auth
22

33
import (
44
"os"
@@ -11,7 +11,7 @@ import (
1111
func TestConfigureGithubClient(t *testing.T) {
1212
t.Parallel()
1313

14-
client := configureGithubClient()
14+
client := ConfigureGithubClient()
1515
assert.NotNil(t, client)
1616
}
1717

@@ -24,6 +24,6 @@ func TestNoGithubOAuthTokenPassed(t *testing.T) {
2424

2525
os.Setenv("GITHUB_OAUTH_TOKEN", "")
2626

27-
err := ensureGithubOauthTokenSet()
27+
err := EnsureGithubOauthTokenSet()
2828
assert.Error(t, err)
2929
}

cmd/git-xargs.go

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,25 @@
1-
package main
1+
package cmd
22

33
import (
44
"bufio"
5-
"github.com/gruntwork-io/go-commons/errors"
6-
"github.com/gruntwork-io/go-commons/logging"
7-
"github.com/urfave/cli"
85
"io"
96
"os"
107
"strings"
11-
)
12-
13-
// GitXargsConfig is the internal representation of a given git-xargs run as specified by the user
14-
type GitXargsConfig struct {
15-
DryRun bool
16-
SkipPullRequests bool
17-
BranchName string
18-
CommitMessage string
19-
PullRequestTitle string
20-
PullRequestDescription string
21-
ReposFile string
22-
GithubOrg string
23-
RepoSlice []string
24-
RepoFromStdIn []string
25-
Args []string
26-
GithubClient GithubClient
27-
GitClient GitClient
28-
Stats *RunStats
29-
}
308

31-
// NewGitXargsConfig sets reasonable defaults for a GitXargsConfig and returns a pointer to a the config
32-
func NewGitXargsConfig() *GitXargsConfig {
33-
return &GitXargsConfig{
34-
DryRun: false,
35-
SkipPullRequests: false,
36-
BranchName: "",
37-
CommitMessage: DefaultCommitMessage,
38-
PullRequestTitle: DefaultPullRequestTitle,
39-
PullRequestDescription: DefaultPullRequestDescription,
40-
ReposFile: "",
41-
GithubOrg: "",
42-
RepoSlice: []string{},
43-
RepoFromStdIn: []string{},
44-
Args: []string{},
45-
GithubClient: configureGithubClient(),
46-
GitClient: NewGitClient(GitProductionProvider{}),
47-
Stats: NewStatsTracker(),
48-
}
49-
}
9+
"github.com/gruntwork-io/git-xargs/auth"
10+
"github.com/gruntwork-io/git-xargs/config"
11+
gitxargs_io "github.com/gruntwork-io/git-xargs/io"
12+
"github.com/gruntwork-io/git-xargs/repository"
13+
"github.com/gruntwork-io/git-xargs/types"
14+
"github.com/gruntwork-io/go-commons/errors"
15+
"github.com/gruntwork-io/go-commons/logging"
16+
"github.com/urfave/cli"
17+
)
5018

5119
// parseGitXargsConfig accepts a urfave cli context and binds its values
5220
// to an internal representation of the data supplied by the user
53-
func parseGitXargsConfig(c *cli.Context) (*GitXargsConfig, error) {
54-
config := NewGitXargsConfig()
21+
func parseGitXargsConfig(c *cli.Context) (*config.GitXargsConfig, error) {
22+
config := config.NewGitXargsConfig()
5523
config.DryRun = c.Bool("dry-run")
5624
config.SkipPullRequests = c.Bool("skip-pull-requests")
5725
config.BranchName = c.String("branch-name")
@@ -117,14 +85,14 @@ func parseSliceFromReader(reader io.Reader) ([]string, error) {
11785

11886
// handleRepoProcessing encapsulates the main processing logic for the supplied repos and printing the run report that
11987
// is built up throughout the processing
120-
func handleRepoProcessing(config *GitXargsConfig) error {
88+
func handleRepoProcessing(config *config.GitXargsConfig) error {
12189
// Track whether or not pull requests were skipped
12290
config.Stats.SetSkipPullRequests(config.SkipPullRequests)
12391

12492
// Update raw command supplied
12593
config.Stats.SetCommand(config.Args)
12694

127-
if err := OperateOnRepos(config); err != nil {
95+
if err := repository.OperateOnRepos(config); err != nil {
12896
return err
12997
}
13098

@@ -138,24 +106,24 @@ func handleRepoProcessing(config *GitXargsConfig) error {
138106
// 1. An exported GITHUB_OAUTH_TOKEN
139107
// 2. Arguments passed to the binary itself which should be executed against the targeted repos
140108
// 3. At least one of the three valid methods for selecting repositories
141-
func sanityCheckInputs(config *GitXargsConfig) error {
142-
if err := ensureGithubOauthTokenSet(); err != nil {
109+
func sanityCheckInputs(config *config.GitXargsConfig) error {
110+
if err := auth.EnsureGithubOauthTokenSet(); err != nil {
143111
return err
144112
}
145113

146114
if len(config.Args) < 1 {
147-
return errors.WithStackTrace(NoArgumentsPassedErr{})
115+
return errors.WithStackTrace(types.NoArgumentsPassedErr{})
148116
}
149117

150-
if err := ensureValidOptionsPassed(config); err != nil {
118+
if err := gitxargs_io.EnsureValidOptionsPassed(config); err != nil {
151119
return errors.WithStackTrace(err)
152120
}
153121

154122
return nil
155123
}
156124

157125
// runGitXargs is the urfave cli app's Action that is called when the user executes the binary
158-
func runGitXargs(c *cli.Context) error {
126+
func RunGitXargs(c *cli.Context) error {
159127
// If someone calls us with no args at all, show the help text and exit
160128
if !c.Args().Present() {
161129
return cli.ShowAppHelp(c)

cmd/git-xargs_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package main
1+
package cmd
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"strings"
65
"testing"
76

7+
"github.com/gruntwork-io/git-xargs/config"
8+
"github.com/gruntwork-io/git-xargs/mocks"
9+
"github.com/stretchr/testify/require"
10+
811
"github.com/stretchr/testify/assert"
912
)
1013

@@ -13,13 +16,13 @@ import (
1316
func TestHandleRepoProcessing(t *testing.T) {
1417
t.Parallel()
1518

16-
config := NewGitXargsTestConfig()
17-
config.ReposFile = "./_testdata/good-test-repos.txt"
18-
config.BranchName = "test-branch-name"
19-
config.CommitMessage = "test-commit-name"
20-
config.Args = []string{"touch", "test.txt"}
21-
config.GithubClient = configureMockGithubClient()
22-
err := handleRepoProcessing(config)
19+
testConfig := config.NewGitXargsTestConfig()
20+
testConfig.ReposFile = "../data/test/good-test-repos.txt"
21+
testConfig.BranchName = "test-branch-name"
22+
testConfig.CommitMessage = "test-commit-name"
23+
testConfig.Args = []string{"touch", "test.txt"}
24+
testConfig.GithubClient = mocks.ConfigureMockGithubClient()
25+
err := handleRepoProcessing(testConfig)
2326

2427
assert.NoError(t, err)
2528
}

cmd/git_test.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

cmd/helper_test.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

cmd/validate-input.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)