make leagueflag type

This commit is contained in:
filifa 2024-07-28 19:17:51 -05:00
parent 30d52f5f25
commit 0764493f5a
2 changed files with 53 additions and 7 deletions

49
cmd/leagueflag.go Normal file
View File

@ -0,0 +1,49 @@
/*
Copyright © 2024 filifa
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"errors"
"strings"
)
type leagueFlag string
func (t *leagueFlag) String() string {
return string(*t)
}
func (t *leagueFlag) Set(v string) error {
var err error
v = strings.ToLower(v)
if v == "" {
*t = leagueFlag(v)
return nil
}
_, ok := leagueIDs[v]
if !ok {
err = errors.New("invalid league ID")
} else {
*t = leagueFlag(v)
}
return err
}
func (t *leagueFlag) Type() string {
return "leagueFlag"
}

View File

@ -25,7 +25,7 @@ import (
"scm.dairydemon.net/filifa/mlblive/cmd/internal/statsapi"
)
var league string
var league leagueFlag
var leagueIDs = map[string]statsapi.LeagueID{
"al": statsapi.AL,
"nl": statsapi.NL,
@ -43,11 +43,7 @@ var leagueIDs = map[string]statsapi.LeagueID{
}
func standings(cmd *cobra.Command, args []string) {
lgID, ok := leagueIDs[league]
if !ok {
log.Fatal("invalid league")
}
leagueId := strconv.Itoa(int(lgID))
leagueId := strconv.Itoa(int(leagueIDs[string(league)]))
standing, err := statsapi.RequestStandings(leagueId)
if err != nil {
@ -62,6 +58,7 @@ var standingsCmd = &cobra.Command{
Use: "standings -l [league]",
Short: "Retrieve league standings",
Long: `Retrieve league standings`,
Args: cobra.NoArgs,
Run: standings,
}
@ -77,6 +74,6 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// standingsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
standingsCmd.Flags().StringVarP(&league, "league", "l", "", "league to get standings for")
standingsCmd.Flags().VarP(&league, "league", "l", "league to get standings for")
standingsCmd.MarkFlagRequired("league")
}