Skip to content

Commit

Permalink
refactor: pointed from codebeat.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 15, 2023
1 parent 77acba6 commit 427c1f4
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions cmd/urleap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func shortenEach(bitly *urleap.Bitly, config *urleap.Config, url string) error {
return nil
}

func deleteEach(bitly *urleap.Bitly, opts *options, config *urleap.Config, url string) error {
func deleteEach(bitly *urleap.Bitly, config *urleap.Config, url string) error {
return bitly.Delete(config, url)
}

Expand All @@ -165,6 +165,16 @@ func listGroups(bitly *urleap.Bitly, config *urleap.Config) error {
return nil
}

func performImpl(args []string, executor func(url string) error) *UrleapError {
for _, url := range args {
err := executor(url)
if err != nil {
return makeError(err, 3)
}
}
return nil
}

func perform(opts *options, args []string) *UrleapError {
bitly := urleap.NewBitly(opts.runOpt.group)
config := urleap.NewConfig(opts.runOpt.config, opts.mode(args))
Expand All @@ -177,19 +187,13 @@ func perform(opts *options, args []string) *UrleapError {
err := listGroups(bitly, config)
return makeError(err, 2)
case urleap.Delete:
for _, url := range args {
err := deleteEach(bitly, opts, config, url)
if err != nil {
return makeError(err, 3)
}
}
return performImpl(args, func(url string) error {
return deleteEach(bitly, config, url)
})
case urleap.Shorten:
for _, url := range args {
err := shortenEach(bitly, config, url)
if err != nil {
return makeError(err, 4)
}
}
return performImpl(args, func(url string) error {
return shortenEach(bitly, config, url)
})
}
return nil
}
Expand Down

0 comments on commit 427c1f4

Please sign in to comment.