From 0764493f5afba529e0911346bcc2ca11d16cf61d Mon Sep 17 00:00:00 2001 From: filifa Date: Sun, 28 Jul 2024 19:17:51 -0500 Subject: [PATCH] make leagueflag type --- cmd/leagueflag.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/standings.go | 11 ++++------- 2 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 cmd/leagueflag.go diff --git a/cmd/leagueflag.go b/cmd/leagueflag.go new file mode 100644 index 0000000..d1661f9 --- /dev/null +++ b/cmd/leagueflag.go @@ -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 . +*/ +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" +} diff --git a/cmd/standings.go b/cmd/standings.go index 33e2bef..ed1864c 100644 --- a/cmd/standings.go +++ b/cmd/standings.go @@ -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") }