diff --git a/cmd/fork-sweeper/main.go b/cmd/fork-sweeper/main.go index b5200bd..1242315 100644 --- a/cmd/fork-sweeper/main.go +++ b/cmd/fork-sweeper/main.go @@ -1,8 +1,9 @@ package main import ( - "github.com/rednafi/fork-sweeper/src" "os" + + "github.com/rednafi/fork-sweeper/src" ) // Ldflags filled by goreleaser diff --git a/src/cli.go b/src/cli.go index 399a629..497fba2 100644 --- a/src/cli.go +++ b/src/cli.go @@ -12,11 +12,12 @@ import ( ) const ( - exitOk = 0 - exitErr = 1 - userNotFoundErr = "API request failed with status: 404" - invalidTokenErr = "API request failed with status: 401" - tokenPermissionErr = "API request failed with status: 403" + exitOk = 0 + exitErr = 1 + + errUserNotFound = "API request failed with status: 404" + errInvalidToken = "API request failed with status: 401" + errInsufficientTokenPermission = "API request failed with status: 403" ) type repo struct { @@ -170,7 +171,6 @@ func deleteRepos(ctx context.Context, baseURL, token string, repos []repo) error } type cliConfig struct { - // Required stdout io.Writer stderr io.Writer @@ -189,6 +189,23 @@ type cliConfig struct { deleteRepos func(ctx context.Context, baseURL, token string, repos []repo) error } +func NewCLIConfig( + stdout, + stderr io.Writer, + version string, +) *cliConfig { + + return &cliConfig{ + stdout: stdout, + stderr: stderr, + version: version, + + flagErrorHandling: flag.ExitOnError, + fetchForkedRepos: fetchForkedRepos, + deleteRepos: deleteRepos, + } +} + // Dysfunctional options pattern func (c *cliConfig) withFlagErrorHandling(h flag.ErrorHandling) *cliConfig { c.flagErrorHandling = h @@ -216,23 +233,6 @@ func (c *cliConfig) withDeleteRepos( return c } -func NewCLIConfig( - stdout, - stderr io.Writer, - version string, -) *cliConfig { - - return &cliConfig{ - stdout: stdout, - stderr: stderr, - version: version, - - flagErrorHandling: flag.ExitOnError, - fetchForkedRepos: fetchForkedRepos, - deleteRepos: deleteRepos, - } -} - func (c *cliConfig) CLI(args []string) int { var ( owner string @@ -298,16 +298,15 @@ func (c *cliConfig) CLI(args []string) int { if err != nil { switch err.Error() { - case userNotFoundErr: + case errUserNotFound: fmt.Fprintf(stderr, "Error: user not found\n") - case invalidTokenErr: + case errInvalidToken: fmt.Fprintf(stderr, "Error: invalid token\n") default: fmt.Fprintf(stderr, "Error: %s\n", err) } return exitErr } - if len(forkedRepos) == 0 { fmt.Fprintf(stdout, "\nNo forked repositories found\n") return exitOk @@ -327,7 +326,7 @@ func (c *cliConfig) CLI(args []string) int { fmt.Fprintf(stdout, "\nDeleting forked repositories...\n") if err := deleteRepos(ctx, baseURL, token, forkedRepos); err != nil { switch err.Error() { - case tokenPermissionErr: + case errInsufficientTokenPermission: fmt.Fprintf(stderr, "Error: token does not have permission to delete repos\n") default: fmt.Fprintf(stderr, "Error: %s\n", err)