Skip to content

Commit

Permalink
refactor: ♻️ big refactor to use config
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChung committed Apr 14, 2023
1 parent 511def0 commit cfdaa23
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 199 deletions.
19 changes: 7 additions & 12 deletions cmd/codecommit/branch/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -50,28 +49,24 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)

// Select a repository
repo, err := cmd.Flags().GetString("repository")
util.ExitOnErr(err)
if repo == "" {
repo, err = pterm.DefaultInteractiveSelect.
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
util.ExitOnErr(err)
}

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

// Remove branches
results, _ := util.Spinner("Removing...", func() ([]client.Result[string], error) {
results := ccClient.DeleteBranches(repo, branchSelections)
results := ccClient.DeleteBranches(cfg.Repository, branchSelections)
return results, nil
})

Expand Down
23 changes: 8 additions & 15 deletions cmd/codecommit/pr/approve/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -58,22 +57,16 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
util.ExitOnErr(err)
authorARN, err := cc.GetAuthorARN(cmd)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)

// Select a repository
repo, err := cmd.Flags().GetString("repository")
util.ExitOnErr(err)
if repo == "" {
repo, err = pterm.DefaultInteractiveSelect.
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
util.ExitOnErr(err)
Expand All @@ -82,8 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
// Get PRs for a given repository
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
AuthorARN: authorARN,
Repositories: []string{repo},
AuthorARN: cfg.AuthorARN,
Repositories: []string{cfg.Repository},
Status: types.PullRequestStatusEnumOpen,
})
if err != nil {
Expand All @@ -105,7 +98,7 @@ func runCmd(cmd *cobra.Command, args []string) {
prMap[s] = prs[i]
}
if len(li) == 0 {
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", repo))
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", cfg.Repository))
}

// Prompt for PRs to approve
Expand Down
23 changes: 8 additions & 15 deletions cmd/codecommit/pr/closes/closes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -58,22 +57,16 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
util.ExitOnErr(err)
authorARN, err := cc.GetAuthorARN(cmd)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)

// Select a repository
repo, err := cmd.Flags().GetString("repository")
util.ExitOnErr(err)
if repo == "" {
repo, err = pterm.DefaultInteractiveSelect.
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
util.ExitOnErr(err)
Expand All @@ -82,8 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
// Get PRs for a given repository
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
AuthorARN: authorARN,
Repositories: []string{repo},
AuthorARN: cfg.AuthorARN,
Repositories: []string{cfg.Repository},
Status: types.PullRequestStatusEnumOpen,
})
if err != nil {
Expand All @@ -105,7 +98,7 @@ func runCmd(cmd *cobra.Command, args []string) {
prMap[s] = prs[i]
}
if len(li) == 0 {
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", repo))
util.ExitOnErr(fmt.Errorf("[%s] has no available PRs", cfg.Repository))
}

// Prompt for PRs to close
Expand Down
19 changes: 7 additions & 12 deletions cmd/codecommit/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -52,28 +51,24 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)

// Select a repository
repo, err := cmd.Flags().GetString("repository")
util.ExitOnErr(err)
if repo == "" {
repo, err = pterm.DefaultInteractiveSelect.
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
util.ExitOnErr(err)
}

// Get branches
branches, err := util.Spinner("Getting branches...", func() ([]string, error) {
branches, err := ccClient.GetBranches(repo)
branches, err := ccClient.GetBranches(cfg.Repository)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +110,7 @@ func runCmd(cmd *cobra.Command, args []string) {
msg, err := util.Spinner("Creating PR...", func() (string, error) {
targets := []types.Target{
{
RepositoryName: aws.String(repo),
RepositoryName: aws.String(cfg.Repository),
SourceReference: aws.String(srcBranch),
DestinationReference: aws.String(destBranch),
},
Expand Down
25 changes: 9 additions & 16 deletions cmd/codecommit/pr/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -60,22 +59,16 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
util.ExitOnErr(err)
authorARN, err := cc.GetAuthorARN(cmd)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)

// Select a repository
repo, err := cmd.Flags().GetString("repository")
util.ExitOnErr(err)
if repo == "" {
repo, err = pterm.DefaultInteractiveSelect.
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
util.ExitOnErr(err)
Expand All @@ -84,8 +77,8 @@ func runCmd(cmd *cobra.Command, args []string) {
// Get PRs for a given repository
prs, err := util.Spinner("Retrieving PRs...", func() ([]*codecommit.GetPullRequestOutput, error) {
prs, err := ccClient.GetPullRequests(client.PullRequestInput{
AuthorARN: authorARN,
Repositories: []string{repo},
AuthorARN: cfg.AuthorARN,
Repositories: []string{cfg.Repository},
Status: types.PullRequestStatusEnumOpen,
})
if err != nil {
Expand Down Expand Up @@ -118,7 +111,7 @@ func runCmd(cmd *cobra.Command, args []string) {
d := make([]*codecommit.GetDifferencesOutput, 0, 10)
for _, t := range prMap[prSelection].PullRequest.PullRequestTargets {
d, err = ccClient.GetDifferences(
aws.String(repo),
aws.String(cfg.Repository),
t.DestinationReference,
t.SourceReference,
)
Expand All @@ -132,7 +125,7 @@ func runCmd(cmd *cobra.Command, args []string) {

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

Expand Down
17 changes: 5 additions & 12 deletions cmd/codecommit/pr/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/internal/config"
cc "github.com/JamesChung/cprl/internal/config/services/codecommit"
"github.com/JamesChung/cprl/pkg/client"
"github.com/JamesChung/cprl/pkg/util"
)
Expand Down Expand Up @@ -54,17 +53,11 @@ func NewCmd() *cobra.Command {
}

func runCmd(cmd *cobra.Command, args []string) {
profile, err := config.GetProfile(cmd)
cfg, err := config.NewCodeCommitConfig(cmd)
util.ExitOnErr(err)
awsProfile, err := config.GetAWSProfile(cmd)
ccClient, err := client.NewCodeCommitClient(cfg.AWSProfile)
util.ExitOnErr(err)
ccClient, err := client.NewCodeCommitClient(awsProfile)
util.ExitOnErr(err)
repos, err := cc.GetRepositories(profile)
util.ExitOnErr(err)
authorARN, err := cc.GetAuthorARN(cmd)
util.ExitOnErr(err)
status, err := cc.GetClosed(cmd)
repos, err := config.GetRepositories(cfg.Profile)
util.ExitOnErr(err)

// Get repository query list
Expand All @@ -90,9 +83,9 @@ func runCmd(cmd *cobra.Command, args []string) {
prIDs, err := util.Spinner("Getting PR IDs...", func() ([][]string, error) {
prIDs, err := ccClient.GetPullRequestIDs(
client.PullRequestInput{
AuthorARN: authorARN,
AuthorARN: cfg.AuthorARN,
Repositories: repoSelections,
Status: status,
Status: cfg.PRStatus,
})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit cfdaa23

Please sign in to comment.