Skip to content

Commit cfdaa23

Browse files
committed
refactor: ♻️ big refactor to use config
1 parent 511def0 commit cfdaa23

File tree

12 files changed

+150
-199
lines changed

12 files changed

+150
-199
lines changed

cmd/codecommit/branch/remove/remove.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"k8s.io/kubectl/pkg/util/templates"
1010

1111
"github.com/JamesChung/cprl/internal/config"
12-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1312
"github.com/JamesChung/cprl/pkg/client"
1413
"github.com/JamesChung/cprl/pkg/util"
1514
)
@@ -50,28 +49,24 @@ func NewCmd() *cobra.Command {
5049
}
5150

5251
func runCmd(cmd *cobra.Command, args []string) {
53-
profile, err := config.GetProfile(cmd)
52+
cfg, err := config.NewCodeCommitConfig(cmd)
5453
util.ExitOnErr(err)
55-
repos, err := cc.GetRepositories(profile)
54+
repos, err := config.GetRepositories(cfg.Profile)
5655
util.ExitOnErr(err)
57-
awsProfile, err := config.GetAWSProfile(cmd)
58-
util.ExitOnErr(err)
59-
ccClient, err := client.NewCodeCommitClient(awsProfile)
56+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
6057
util.ExitOnErr(err)
6158

6259
// Select a repository
63-
repo, err := cmd.Flags().GetString("repository")
64-
util.ExitOnErr(err)
65-
if repo == "" {
66-
repo, err = pterm.DefaultInteractiveSelect.
60+
if cfg.Repository == "" {
61+
cfg.Repository, err = pterm.DefaultInteractiveSelect.
6762
WithDefaultText("Select a repository").
6863
WithOptions(repos).Show()
6964
util.ExitOnErr(err)
7065
}
7166

7267
// Get branches for a given repository
7368
branches, err := util.Spinner("Retrieving branches...", func() ([]string, error) {
74-
branches, err := ccClient.GetBranches(repo)
69+
branches, err := ccClient.GetBranches(cfg.Repository)
7570
if err != nil {
7671
return nil, err
7772
}
@@ -86,7 +81,7 @@ func runCmd(cmd *cobra.Command, args []string) {
8681

8782
// Remove branches
8883
results, _ := util.Spinner("Removing...", func() ([]client.Result[string], error) {
89-
results := ccClient.DeleteBranches(repo, branchSelections)
84+
results := ccClient.DeleteBranches(cfg.Repository, branchSelections)
9085
return results, nil
9186
})
9287

cmd/codecommit/pr/approve/approve.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"k8s.io/kubectl/pkg/util/templates"
1313

1414
"github.com/JamesChung/cprl/internal/config"
15-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1615
"github.com/JamesChung/cprl/pkg/client"
1716
"github.com/JamesChung/cprl/pkg/util"
1817
)
@@ -58,22 +57,16 @@ func NewCmd() *cobra.Command {
5857
}
5958

6059
func runCmd(cmd *cobra.Command, args []string) {
61-
profile, err := config.GetProfile(cmd)
60+
cfg, err := config.NewCodeCommitConfig(cmd)
6261
util.ExitOnErr(err)
63-
repos, err := cc.GetRepositories(profile)
62+
repos, err := config.GetRepositories(cfg.Profile)
6463
util.ExitOnErr(err)
65-
awsProfile, err := config.GetAWSProfile(cmd)
66-
util.ExitOnErr(err)
67-
ccClient, err := client.NewCodeCommitClient(awsProfile)
68-
util.ExitOnErr(err)
69-
authorARN, err := cc.GetAuthorARN(cmd)
64+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
7065
util.ExitOnErr(err)
7166

7267
// Select a repository
73-
repo, err := cmd.Flags().GetString("repository")
74-
util.ExitOnErr(err)
75-
if repo == "" {
76-
repo, err = pterm.DefaultInteractiveSelect.
68+
if cfg.Repository == "" {
69+
cfg.Repository, err = pterm.DefaultInteractiveSelect.
7770
WithDefaultText("Select a repository").
7871
WithOptions(repos).Show()
7972
util.ExitOnErr(err)
@@ -82,8 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
8275
// Get PRs for a given repository
8376
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
8477
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
85-
AuthorARN: authorARN,
86-
Repositories: []string{repo},
78+
AuthorARN: cfg.AuthorARN,
79+
Repositories: []string{cfg.Repository},
8780
Status: types.PullRequestStatusEnumOpen,
8881
})
8982
if err != nil {
@@ -105,7 +98,7 @@ func runCmd(cmd *cobra.Command, args []string) {
10598
prMap[s] = prs[i]
10699
}
107100
if len(li) == 0 {
108-
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", repo))
101+
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", cfg.Repository))
109102
}
110103

111104
// Prompt for PRs to approve

cmd/codecommit/pr/closes/closes.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"k8s.io/kubectl/pkg/util/templates"
1313

1414
"github.com/JamesChung/cprl/internal/config"
15-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1615
"github.com/JamesChung/cprl/pkg/client"
1716
"github.com/JamesChung/cprl/pkg/util"
1817
)
@@ -58,22 +57,16 @@ func NewCmd() *cobra.Command {
5857
}
5958

6059
func runCmd(cmd *cobra.Command, args []string) {
61-
profile, err := config.GetProfile(cmd)
60+
cfg, err := config.NewCodeCommitConfig(cmd)
6261
util.ExitOnErr(err)
63-
repos, err := cc.GetRepositories(profile)
62+
repos, err := config.GetRepositories(cfg.Profile)
6463
util.ExitOnErr(err)
65-
awsProfile, err := config.GetAWSProfile(cmd)
66-
util.ExitOnErr(err)
67-
ccClient, err := client.NewCodeCommitClient(awsProfile)
68-
util.ExitOnErr(err)
69-
authorARN, err := cc.GetAuthorARN(cmd)
64+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
7065
util.ExitOnErr(err)
7166

7267
// Select a repository
73-
repo, err := cmd.Flags().GetString("repository")
74-
util.ExitOnErr(err)
75-
if repo == "" {
76-
repo, err = pterm.DefaultInteractiveSelect.
68+
if cfg.Repository == "" {
69+
cfg.Repository, err = pterm.DefaultInteractiveSelect.
7770
WithDefaultText("Select a repository").
7871
WithOptions(repos).Show()
7972
util.ExitOnErr(err)
@@ -82,8 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
8275
// Get PRs for a given repository
8376
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
8477
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
85-
AuthorARN: authorARN,
86-
Repositories: []string{repo},
78+
AuthorARN: cfg.AuthorARN,
79+
Repositories: []string{cfg.Repository},
8780
Status: types.PullRequestStatusEnumOpen,
8881
})
8982
if err != nil {
@@ -105,7 +98,7 @@ func runCmd(cmd *cobra.Command, args []string) {
10598
prMap[s] = prs[i]
10699
}
107100
if len(li) == 0 {
108-
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", repo))
101+
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", cfg.Repository))
109102
}
110103

111104
// Prompt for PRs to close

cmd/codecommit/pr/create/create.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"k8s.io/kubectl/pkg/util/templates"
1212

1313
"github.com/JamesChung/cprl/internal/config"
14-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1514
"github.com/JamesChung/cprl/pkg/client"
1615
"github.com/JamesChung/cprl/pkg/util"
1716
)
@@ -52,28 +51,24 @@ func NewCmd() *cobra.Command {
5251
}
5352

5453
func runCmd(cmd *cobra.Command, args []string) {
55-
profile, err := config.GetProfile(cmd)
54+
cfg, err := config.NewCodeCommitConfig(cmd)
5655
util.ExitOnErr(err)
57-
repos, err := cc.GetRepositories(profile)
56+
repos, err := config.GetRepositories(cfg.Profile)
5857
util.ExitOnErr(err)
59-
awsProfile, err := config.GetAWSProfile(cmd)
60-
util.ExitOnErr(err)
61-
ccClient, err := client.NewCodeCommitClient(awsProfile)
58+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
6259
util.ExitOnErr(err)
6360

6461
// Select a repository
65-
repo, err := cmd.Flags().GetString("repository")
66-
util.ExitOnErr(err)
67-
if repo == "" {
68-
repo, err = pterm.DefaultInteractiveSelect.
62+
if cfg.Repository == "" {
63+
cfg.Repository, err = pterm.DefaultInteractiveSelect.
6964
WithDefaultText("Select a repository").
7065
WithOptions(repos).Show()
7166
util.ExitOnErr(err)
7267
}
7368

7469
// Get branches
7570
branches, err := util.Spinner("Getting branches...", func() ([]string, error) {
76-
branches, err := ccClient.GetBranches(repo)
71+
branches, err := ccClient.GetBranches(cfg.Repository)
7772
if err != nil {
7873
return nil, err
7974
}
@@ -115,7 +110,7 @@ func runCmd(cmd *cobra.Command, args []string) {
115110
msg, err := util.Spinner("Creating PR...", func() (string, error) {
116111
targets := []types.Target{
117112
{
118-
RepositoryName: aws.String(repo),
113+
RepositoryName: aws.String(cfg.Repository),
119114
SourceReference: aws.String(srcBranch),
120115
DestinationReference: aws.String(destBranch),
121116
},

cmd/codecommit/pr/diff/diff.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/kubectl/pkg/util/templates"
1515

1616
"github.com/JamesChung/cprl/internal/config"
17-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1817
"github.com/JamesChung/cprl/pkg/client"
1918
"github.com/JamesChung/cprl/pkg/util"
2019
)
@@ -60,22 +59,16 @@ func NewCmd() *cobra.Command {
6059
}
6160

6261
func runCmd(cmd *cobra.Command, args []string) {
63-
profile, err := config.GetProfile(cmd)
62+
cfg, err := config.NewCodeCommitConfig(cmd)
6463
util.ExitOnErr(err)
65-
repos, err := cc.GetRepositories(profile)
64+
repos, err := config.GetRepositories(cfg.Profile)
6665
util.ExitOnErr(err)
67-
awsProfile, err := config.GetAWSProfile(cmd)
68-
util.ExitOnErr(err)
69-
ccClient, err := client.NewCodeCommitClient(awsProfile)
70-
util.ExitOnErr(err)
71-
authorARN, err := cc.GetAuthorARN(cmd)
66+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
7267
util.ExitOnErr(err)
7368

7469
// Select a repository
75-
repo, err := cmd.Flags().GetString("repository")
76-
util.ExitOnErr(err)
77-
if repo == "" {
78-
repo, err = pterm.DefaultInteractiveSelect.
70+
if cfg.Repository == "" {
71+
cfg.Repository, err = pterm.DefaultInteractiveSelect.
7972
WithDefaultText("Select a repository").
8073
WithOptions(repos).Show()
8174
util.ExitOnErr(err)
@@ -84,8 +77,8 @@ func runCmd(cmd *cobra.Command, args []string) {
8477
// Get PRs for a given repository
8578
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
8679
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
87-
AuthorARN: authorARN,
88-
Repositories: []string{repo},
80+
AuthorARN: cfg.AuthorARN,
81+
Repositories: []string{cfg.Repository},
8982
Status: types.PullRequestStatusEnumOpen,
9083
})
9184
if err != nil {
@@ -118,7 +111,7 @@ func runCmd(cmd *cobra.Command, args []string) {
118111
d := make([]*codecommit.GetDifferencesOutput, 0, 10)
119112
for _, t := range prMap[prSelection].PullRequest.PullRequestTargets {
120113
d, err = ccClient.GetDifferences(
121-
aws.String(repo),
114+
aws.String(cfg.Repository),
122115
t.DestinationReference,
123116
t.SourceReference,
124117
)
@@ -132,7 +125,7 @@ func runCmd(cmd *cobra.Command, args []string) {
132125

133126
// Concurrently generate individual file diffs
134127
results, _ := util.Spinner("Generating Differences...", func() ([]client.Result[[]byte], error) {
135-
results := ccClient.GenerateDiffs(repo, diffOut)
128+
results := ccClient.GenerateDiffs(cfg.Repository, diffOut)
136129
return results, nil
137130
})
138131

cmd/codecommit/pr/list/list.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"k8s.io/kubectl/pkg/util/templates"
99

1010
"github.com/JamesChung/cprl/internal/config"
11-
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
1211
"github.com/JamesChung/cprl/pkg/client"
1312
"github.com/JamesChung/cprl/pkg/util"
1413
)
@@ -54,17 +53,11 @@ func NewCmd() *cobra.Command {
5453
}
5554

5655
func runCmd(cmd *cobra.Command, args []string) {
57-
profile, err := config.GetProfile(cmd)
56+
cfg, err := config.NewCodeCommitConfig(cmd)
5857
util.ExitOnErr(err)
59-
awsProfile, err := config.GetAWSProfile(cmd)
58+
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
6059
util.ExitOnErr(err)
61-
ccClient, err := client.NewCodeCommitClient(awsProfile)
62-
util.ExitOnErr(err)
63-
repos, err := cc.GetRepositories(profile)
64-
util.ExitOnErr(err)
65-
authorARN, err := cc.GetAuthorARN(cmd)
66-
util.ExitOnErr(err)
67-
status, err := cc.GetClosed(cmd)
60+
repos, err := config.GetRepositories(cfg.Profile)
6861
util.ExitOnErr(err)
6962

7063
// Get repository query list
@@ -90,9 +83,9 @@ func runCmd(cmd *cobra.Command, args []string) {
9083
prIDs, err := util.Spinner("Getting PR IDs...", func() ([][]string, error) {
9184
prIDs, err := ccClient.GetPullRequestIDs(
9285
client.PullRequestInput{
93-
AuthorARN: authorARN,
86+
AuthorARN: cfg.AuthorARN,
9487
Repositories: repoSelections,
95-
Status: status,
88+
Status: cfg.PRStatus,
9689
})
9790
if err != nil {
9891
return nil, err

0 commit comments

Comments
 (0)