Skip to content

Commit d469c8a

Browse files
committed
refactor: ♻️ big refactor and move to beta
1 parent a3fe3e5 commit d469c8a

File tree

13 files changed

+46
-35
lines changed

13 files changed

+46
-35
lines changed

cmd/codecommit/branch/remove/remove.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package remove
22

33
import (
4-
"fmt"
4+
"os"
55

66
"github.com/pterm/pterm"
77
"github.com/spf13/cobra"
@@ -38,7 +38,7 @@ func setPersistentFlags(flags *pflag.FlagSet) {
3838
func NewCmd() *cobra.Command {
3939
cmd := &cobra.Command{
4040
Use: "remove",
41-
Aliases: []string{"r", "rem"},
41+
Aliases: []string{"r", "rm", "rem"},
4242
Short: shortMessage,
4343
Long: longMessage,
4444
Example: example,
@@ -59,8 +59,7 @@ func runCmd(cmd *cobra.Command, args []string) {
5959
// Select a repository
6060
if cfg.Repository == "" {
6161
cfg.Repository, err = pterm.DefaultInteractiveSelect.
62-
WithDefaultText("Select a repository").
63-
WithOptions(repos).Show()
62+
WithOptions(repos).Show("Select a repository")
6463
util.ExitOnErr(err)
6564
}
6665

@@ -88,14 +87,14 @@ func runCmd(cmd *cobra.Command, args []string) {
8887
failCount := 0
8988
for _, r := range results {
9089
if r.Err != nil {
91-
pterm.Error.Printf("Failed to remove branch [%s]\n", r.Result)
90+
pterm.Error.Printf("Failed to remove branch [%s]: %s\n", r.Result, r.Err)
9291
failCount++
9392
continue
9493
}
9594
pterm.Success.Printf("Successfully removed branch [%s]\n", r.Result)
9695
}
9796

9897
if failCount > 0 {
99-
util.ExitOnErr(fmt.Errorf("Failed to remove %d branches\n", failCount))
98+
os.Exit(1)
10099
}
101100
}

cmd/codecommit/pr/approve/approve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package approve
22

33
import (
44
"fmt"
5+
"os"
56

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

@@ -123,6 +123,6 @@ func runCmd(cmd *cobra.Command, args []string) {
123123
pterm.Success.Printf("Successfully approved PR [%s]\n", r.Result)
124124
}
125125
if errCount > 0 {
126-
util.ExitOnErr(fmt.Errorf("%d PRs failed to be approved\n", errCount))
126+
os.Exit(1)
127127
}
128128
}

cmd/codecommit/pr/closes/closes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package closes
22

33
import (
44
"fmt"
5+
"os"
56

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

@@ -123,6 +123,6 @@ func runCmd(cmd *cobra.Command, args []string) {
123123
pterm.Success.Printf("Successfully closed PR [%s]\n", r.Result)
124124
}
125125
if errCount > 0 {
126-
util.ExitOnErr(fmt.Errorf("%d PRs failed to be closed\n", errCount))
126+
os.Exit(1)
127127
}
128128
}

cmd/codecommit/pr/create/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func runCmd(cmd *cobra.Command, args []string) {
6161
// Select a repository
6262
if cfg.Repository == "" {
6363
cfg.Repository, err = pterm.DefaultInteractiveSelect.
64-
WithDefaultText("Select a repository").
65-
WithOptions(repos).Show()
64+
WithOptions(repos).Show("Select a repository")
6665
util.ExitOnErr(err)
6766
}
6867

cmd/codecommit/pr/diff/diff.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ func runCmd(cmd *cobra.Command, args []string) {
6969
// Select a repository
7070
if cfg.Repository == "" {
7171
cfg.Repository, err = pterm.DefaultInteractiveSelect.
72-
WithDefaultText("Select a repository").
73-
WithOptions(repos).Show()
72+
WithOptions(repos).Show("Select a repository")
7473
util.ExitOnErr(err)
7574
}
7675

cmd/console/open/open.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package open
22

33
import (
4-
"github.com/pterm/pterm"
54
"github.com/spf13/cobra"
65
"github.com/spf13/pflag"
76
"k8s.io/kubectl/pkg/util/templates"
@@ -51,8 +50,7 @@ func runCmd(cmd *cobra.Command, args []string) {
5150
if err != nil {
5251
util.ExitOnErr(err)
5352
}
54-
cfg.AWSProfile, err = pterm.DefaultInteractiveSelect.
55-
WithOptions(profiles).Show("Select a profile")
53+
err = cfg.InteractivelyAssignProfile(profiles)
5654
util.ExitOnErr(err)
5755
}
5856

cmd/credentials/credentials.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"k8s.io/kubectl/pkg/util/templates"
66

77
"github.com/JamesChung/cprl/cmd/credentials/assume"
8-
"github.com/JamesChung/cprl/cmd/credentials/clear"
98
"github.com/JamesChung/cprl/cmd/credentials/list"
109
"github.com/JamesChung/cprl/cmd/credentials/output"
10+
"github.com/JamesChung/cprl/cmd/credentials/remove"
1111
"github.com/JamesChung/cprl/pkg/util"
1212
)
1313

@@ -20,9 +20,9 @@ var (
2020
func credentialsCommands() []*cobra.Command {
2121
return []*cobra.Command{
2222
assume.NewCmd(),
23-
clear.NewCmd(),
2423
list.NewCmd(),
2524
output.NewCmd(),
25+
remove.NewCmd(),
2626
}
2727
}
2828

cmd/credentials/output/output.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55

66
"github.com/aws/aws-sdk-go-v2/aws"
7-
"github.com/pterm/pterm"
87
"github.com/spf13/cobra"
98
"github.com/spf13/pflag"
109
"k8s.io/kubectl/pkg/util/templates"
@@ -38,6 +37,12 @@ var (
3837
)
3938

4039
func setPersistentFlags(flags *pflag.FlagSet) {
40+
flags.BoolP(
41+
"interactive",
42+
"i",
43+
false,
44+
"interactive prompt with suggested profiles",
45+
)
4146
flags.Bool(
4247
"json",
4348
false,
@@ -70,9 +75,8 @@ func runCmd(cmd *cobra.Command, args []string) {
7075
profiles, err := util.GetAllProfiles()
7176
util.ExitOnErr(err)
7277

73-
if cfg.AWSProfile == "" {
74-
cfg.AWSProfile, err = pterm.DefaultInteractiveSelect.
75-
WithOptions(profiles).Show("Select a profile")
78+
if cfg.Interactive {
79+
err = cfg.InteractivelyAssignProfile(profiles)
7680
util.ExitOnErr(err)
7781
}
7882

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package clear
1+
package remove
22

33
import (
44
"github.com/JamesChung/cprl/pkg/util"
@@ -8,19 +8,19 @@ import (
88
)
99

1010
var (
11-
shortMessage = "clears AWS credentials"
11+
shortMessage = "remove AWS credentials"
1212

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

2020
func NewCmd() *cobra.Command {
2121
cmd := &cobra.Command{
22-
Use: "clear",
23-
Aliases: []string{"c", "cl", "clr"},
22+
Use: "remove",
23+
Aliases: []string{"r", "rm", "rem"},
2424
Short: shortMessage,
2525
Example: example,
2626
Run: runCmd,
@@ -32,8 +32,8 @@ func runCmd(cmd *cobra.Command, args []string) {
3232
profiles, err := util.GetCredentialsProfiles()
3333
util.ExitOnErr(err)
3434
selections, err := pterm.DefaultInteractiveMultiselect.
35-
WithOptions(profiles).Show()
35+
WithOptions(profiles).Show("Select profiles to remove")
3636
util.ExitOnErr(err)
37-
err = util.ClearProfiles(selections)
37+
err = util.RemoveProfiles(selections)
3838
util.ExitOnErr(err)
3939
}

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ func (c *Config) SetAWSProfile(cmd *cobra.Command) error {
191191
return nil
192192
}
193193

194+
func (c *Config) InteractivelyAssignProfile(profiles []string) error {
195+
var err error
196+
c.AWSProfile, err = pterm.DefaultInteractiveSelect.
197+
WithOptions(profiles).Show("Select a profile")
198+
if err != nil {
199+
return err
200+
}
201+
return nil
202+
}
203+
194204
func NewConfig(cmd *cobra.Command) (*Config, error) {
195205
var err error
196206
cfg := &Config{}

internal/config/credentials.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type CredentialsConfig struct {
99
OutputProfile string
1010
OutputStyle string
1111
IsJSON bool
12+
Interactive bool
1213
}
1314

1415
func NewCredentialsConfig(cmd *cobra.Command) (*CredentialsConfig, error) {
@@ -22,5 +23,6 @@ func NewCredentialsConfig(cmd *cobra.Command) (*CredentialsConfig, error) {
2223
cfg.OutputProfile, _ = cmd.Flags().GetString("output-profile")
2324
cfg.OutputStyle, _ = cmd.Flags().GetString("output-style")
2425
cfg.IsJSON, _ = cmd.Flags().GetBool("json")
26+
cfg.Interactive, _ = cmd.Flags().GetBool("interactive")
2527
return cfg, nil
2628
}

pkg/util/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func WriteCredentials(section string, creds aws.Credentials) error {
134134
return nil
135135
}
136136

137-
func ClearProfiles(profiles []string) error {
137+
func RemoveProfiles(profiles []string) error {
138138
credentialsFile, err := getCredentialsFilePath()
139139
if err != nil {
140140
return err

pkg/util/credentials_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestGetAllProfiles(t *testing.T) {
4141
}
4242

4343
func TestClearProfile(t *testing.T) {
44-
err := ClearProfiles([]string{"yo"})
44+
err := RemoveProfiles([]string{"yo"})
4545
if err != nil {
4646
t.Fail()
4747
}

0 commit comments

Comments
 (0)