Skip to content

Commit

Permalink
refactor: ♻️ big refactor and move to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesChung committed Apr 14, 2023
1 parent a3fe3e5 commit d469c8a
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 35 deletions.
11 changes: 5 additions & 6 deletions cmd/codecommit/branch/remove/remove.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package remove

import (
"fmt"
"os"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,7 +38,7 @@ func setPersistentFlags(flags *pflag.FlagSet) {
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "remove",
Aliases: []string{"r", "rem"},
Aliases: []string{"r", "rm", "rem"},
Short: shortMessage,
Long: longMessage,
Example: example,
Expand All @@ -59,8 +59,7 @@ func runCmd(cmd *cobra.Command, args []string) {
// Select a repository
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
WithOptions(repos).Show("Select a repository")
util.ExitOnErr(err)
}

Expand Down Expand Up @@ -88,14 +87,14 @@ func runCmd(cmd *cobra.Command, args []string) {
failCount := 0
for _, r := range results {
if r.Err != nil {
pterm.Error.Printf("Failed to remove branch [%s]\n", r.Result)
pterm.Error.Printf("Failed to remove branch [%s]: %s\n", r.Result, r.Err)
failCount++
continue
}
pterm.Success.Printf("Successfully removed branch [%s]\n", r.Result)
}

if failCount > 0 {
util.ExitOnErr(fmt.Errorf("Failed to remove %d branches\n", failCount))
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions cmd/codecommit/pr/approve/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package approve

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codecommit"
Expand Down Expand Up @@ -67,8 +68,7 @@ func runCmd(cmd *cobra.Command, args []string) {
// Select a repository
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
WithOptions(repos).Show("Select a repository")
util.ExitOnErr(err)
}

Expand Down Expand Up @@ -123,6 +123,6 @@ func runCmd(cmd *cobra.Command, args []string) {
pterm.Success.Printf("Successfully approved PR [%s]\n", r.Result)
}
if errCount > 0 {
util.ExitOnErr(fmt.Errorf("%d PRs failed to be approved\n", errCount))
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions cmd/codecommit/pr/closes/closes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package closes

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codecommit"
Expand Down Expand Up @@ -67,8 +68,7 @@ func runCmd(cmd *cobra.Command, args []string) {
// Select a repository
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
WithOptions(repos).Show("Select a repository")
util.ExitOnErr(err)
}

Expand Down Expand Up @@ -123,6 +123,6 @@ func runCmd(cmd *cobra.Command, args []string) {
pterm.Success.Printf("Successfully closed PR [%s]\n", r.Result)
}
if errCount > 0 {
util.ExitOnErr(fmt.Errorf("%d PRs failed to be closed\n", errCount))
os.Exit(1)
}
}
3 changes: 1 addition & 2 deletions cmd/codecommit/pr/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func runCmd(cmd *cobra.Command, args []string) {
// Select a repository
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
WithOptions(repos).Show("Select a repository")
util.ExitOnErr(err)
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/codecommit/pr/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func runCmd(cmd *cobra.Command, args []string) {
// Select a repository
if cfg.Repository == "" {
cfg.Repository, err = pterm.DefaultInteractiveSelect.
WithDefaultText("Select a repository").
WithOptions(repos).Show()
WithOptions(repos).Show("Select a repository")
util.ExitOnErr(err)
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/console/open/open.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package open

import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/kubectl/pkg/util/templates"
Expand Down Expand Up @@ -51,8 +50,7 @@ func runCmd(cmd *cobra.Command, args []string) {
if err != nil {
util.ExitOnErr(err)
}
cfg.AWSProfile, err = pterm.DefaultInteractiveSelect.
WithOptions(profiles).Show("Select a profile")
err = cfg.InteractivelyAssignProfile(profiles)
util.ExitOnErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"k8s.io/kubectl/pkg/util/templates"

"github.com/JamesChung/cprl/cmd/credentials/assume"
"github.com/JamesChung/cprl/cmd/credentials/clear"
"github.com/JamesChung/cprl/cmd/credentials/list"
"github.com/JamesChung/cprl/cmd/credentials/output"
"github.com/JamesChung/cprl/cmd/credentials/remove"
"github.com/JamesChung/cprl/pkg/util"
)

Expand All @@ -20,9 +20,9 @@ var (
func credentialsCommands() []*cobra.Command {
return []*cobra.Command{
assume.NewCmd(),
clear.NewCmd(),
list.NewCmd(),
output.NewCmd(),
remove.NewCmd(),
}
}

Expand Down
12 changes: 8 additions & 4 deletions cmd/credentials/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/kubectl/pkg/util/templates"
Expand Down Expand Up @@ -38,6 +37,12 @@ var (
)

func setPersistentFlags(flags *pflag.FlagSet) {
flags.BoolP(
"interactive",
"i",
false,
"interactive prompt with suggested profiles",
)
flags.Bool(
"json",
false,
Expand Down Expand Up @@ -70,9 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
profiles, err := util.GetAllProfiles()
util.ExitOnErr(err)

if cfg.AWSProfile == "" {
cfg.AWSProfile, err = pterm.DefaultInteractiveSelect.
WithOptions(profiles).Show("Select a profile")
if cfg.Interactive {
err = cfg.InteractivelyAssignProfile(profiles)
util.ExitOnErr(err)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package clear
package remove

import (
"github.com/JamesChung/cprl/pkg/util"
Expand All @@ -8,19 +8,19 @@ import (
)

var (
shortMessage = "clears AWS credentials"
shortMessage = "remove AWS credentials"

example = templates.Examples(`
$ cprl credentials clear
Please select your options:
$ cprl credentials remove
Select profiles to remove:
> [✗] dev
enter: select | tab: confirm | left: none | right: all| type to filter`)
)

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "clear",
Aliases: []string{"c", "cl", "clr"},
Use: "remove",
Aliases: []string{"r", "rm", "rem"},
Short: shortMessage,
Example: example,
Run: runCmd,
Expand All @@ -32,8 +32,8 @@ func runCmd(cmd *cobra.Command, args []string) {
profiles, err := util.GetCredentialsProfiles()
util.ExitOnErr(err)
selections, err := pterm.DefaultInteractiveMultiselect.
WithOptions(profiles).Show()
WithOptions(profiles).Show("Select profiles to remove")
util.ExitOnErr(err)
err = util.ClearProfiles(selections)
err = util.RemoveProfiles(selections)
util.ExitOnErr(err)
}
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ func (c *Config) SetAWSProfile(cmd *cobra.Command) error {
return nil
}

func (c *Config) InteractivelyAssignProfile(profiles []string) error {
var err error
c.AWSProfile, err = pterm.DefaultInteractiveSelect.
WithOptions(profiles).Show("Select a profile")
if err != nil {
return err
}
return nil
}

func NewConfig(cmd *cobra.Command) (*Config, error) {
var err error
cfg := &Config{}
Expand Down
2 changes: 2 additions & 0 deletions internal/config/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type CredentialsConfig struct {
OutputProfile string
OutputStyle string
IsJSON bool
Interactive bool
}

func NewCredentialsConfig(cmd *cobra.Command) (*CredentialsConfig, error) {
Expand All @@ -22,5 +23,6 @@ func NewCredentialsConfig(cmd *cobra.Command) (*CredentialsConfig, error) {
cfg.OutputProfile, _ = cmd.Flags().GetString("output-profile")
cfg.OutputStyle, _ = cmd.Flags().GetString("output-style")
cfg.IsJSON, _ = cmd.Flags().GetBool("json")
cfg.Interactive, _ = cmd.Flags().GetBool("interactive")
return cfg, nil
}
2 changes: 1 addition & 1 deletion pkg/util/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func WriteCredentials(section string, creds aws.Credentials) error {
return nil
}

func ClearProfiles(profiles []string) error {
func RemoveProfiles(profiles []string) error {
credentialsFile, err := getCredentialsFilePath()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGetAllProfiles(t *testing.T) {
}

func TestClearProfile(t *testing.T) {
err := ClearProfiles([]string{"yo"})
err := RemoveProfiles([]string{"yo"})
if err != nil {
t.Fail()
}
Expand Down

0 comments on commit d469c8a

Please sign in to comment.