Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
rednafi committed Mar 8, 2024
1 parent 80e1f7c commit 8f44cb1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
3 changes: 2 additions & 1 deletion cmd/fork-sweeper/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/rednafi/fork-sweeper/src"
"os"

"github.com/rednafi/fork-sweeper/src"
)

// Ldflags filled by goreleaser
Expand Down
53 changes: 26 additions & 27 deletions src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8f44cb1

Please sign in to comment.