Skip to content

Commit

Permalink
Move shell to package
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Sep 9, 2023
1 parent a6cea43 commit 338ac40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
21 changes: 2 additions & 19 deletions handlers/day.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package handlers

import (
"fmt"
"goirc/bot"
"os/exec"
"strings"
"goirc/shell"
)

func NationalDay(params bot.HandlerParams) error {
r, err := shell(`curl -s https://nationaltoday.com/ | pup '.holiday-title-text json{}' | jq -r .[0].text`)
r, err := shell.Command(`curl -s https://nationaltoday.com/ | pup '.holiday-title-text json{}' | jq -r .[0].text`)
if err != nil {
params.Privmsgf(params.Target, "error: %v", err)
}
Expand All @@ -17,18 +15,3 @@ func NationalDay(params bot.HandlerParams) error {

return nil
}

func shell(command string) (string, error) {
cmd := exec.Command("/bin/sh", "-c", command)

var stdout, stderr strings.Builder

cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%s", stderr.String())
}

return stdout.String(), nil
}
5 changes: 3 additions & 2 deletions handlers/mlb/mlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mlb
import (
"fmt"
"goirc/bot"
"goirc/shell"
"sort"
"strconv"
"strings"
Expand All @@ -14,7 +15,7 @@ func SingleTeamOdds(params bot.HandlerParams) error {

date := time.Now().Format(time.DateOnly)

r, err := shell(fmt.Sprintf(`
r, err := shell.Command(fmt.Sprintf(`
curl -s 'https://www.fangraphs.com/api/playoff-odds/odds?dateEnd=%s&dateDelta=&projectionMode=2&standingsType=div' |\
jq .[] |\
jq 'select(.abbName == "%s")'.endData.poffTitle
Expand All @@ -36,7 +37,7 @@ jq 'select(.abbName == "%s")'.endData.poffTitle
func Teams(params bot.HandlerParams) error {
date := time.Now().Format(time.DateOnly)

r, err := shell(fmt.Sprintf(`
r, err := shell.Command(fmt.Sprintf(`
curl -s 'https://www.fangraphs.com/api/playoff-odds/odds?dateEnd=%s&dateDelta=&projectionMode=2&standingsType=div' | jq -r .[].abbName
`, date))
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions shell/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package shell

import (
"fmt"
"os/exec"
"strings"
)

func Command(command string) (string, error) {
cmd := exec.Command("/bin/sh", "-c", command)

var stdout, stderr strings.Builder

cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%s", stderr.String())
}

return stdout.String(), nil
}

0 comments on commit 338ac40

Please sign in to comment.