diff --git a/cli/papero/set.go b/cli/papero/set.go index e95e1a6..be6f068 100644 --- a/cli/papero/set.go +++ b/cli/papero/set.go @@ -1,7 +1,6 @@ package main import ( - "errors" "flag" "fmt" "io" @@ -51,57 +50,6 @@ func (s SetCmd) Help(w io.Writer, set *flag.FlagSet) { } } -// FLAG UTILS - -type pair struct { - set, unset bool -} - -type pairFlag struct { - *pair -} - -func newPairFlag() pairFlag { - return pairFlag{ - &pair{false, false}, - } -} - -func (p pair) String() string { - switch p { - case pair{true, false}: - return "set" - case pair{false, true}: - return "unset" - default: - return "undef" - } -} - -func (pf pairFlag) String() string { - return pf.pair.String() -} - -func (pf pairFlag) Set(s string) error { - Log.Debugf("into Set: %s\n", s) - if s == "set" { - Log.Debug("setting") - v := pair{true, false} - *pf.pair = v - return nil - } - - if s == "unset" { - Log.Debug("unsetting") - v := pair{false, true} - *pf.pair = v - return nil - } - - Log.Debug(fmt.Sprintf("unacceptable value %s\n", s)) - return errors.New("only `set` or `unset` are allowed") -} - // VERBS type SetMessageCmd struct{} @@ -112,11 +60,11 @@ func (sm SetMessageCmd) Func(args []string) error { } flags := imaputils.NewFlagsStatus() var opts imaputils.SetFlagsOpts - flagSeen := newPairFlag() - flagAnswered := newPairFlag() - flagFlagged := newPairFlag() - flagDeleted := newPairFlag() - flagDraft := newPairFlag() + flagSeen := cli.NewPairFlag() + flagAnswered := cli.NewPairFlag() + flagFlagged := cli.NewPairFlag() + flagDeleted := cli.NewPairFlag() + flagDraft := cli.NewPairFlag() flagset := flag.NewFlagSet(args[0], flag.ExitOnError) @@ -148,19 +96,19 @@ func (sm SetMessageCmd) Func(args []string) error { opts.MessageSeq = seq opts.Debug = Session.Info.Opts.Debug - if err := assignFlag(*flagSeen.pair, flags.SetSeen, flags.UnsetSeen); err != nil { + if err := assignFlag(*flagSeen.Pair, flags.SetSeen, flags.UnsetSeen); err != nil { return err } - if err := assignFlag(*flagAnswered.pair, flags.SetAnswered, flags.UnsetAnswered); err != nil { + if err := assignFlag(*flagAnswered.Pair, flags.SetAnswered, flags.UnsetAnswered); err != nil { return err } - if err := assignFlag(*flagFlagged.pair, flags.SetFlagged, flags.UnsetFlagged); err != nil { + if err := assignFlag(*flagFlagged.Pair, flags.SetFlagged, flags.UnsetFlagged); err != nil { return err } - if err := assignFlag(*flagDeleted.pair, flags.SetDeleted, flags.UnsetDeleted); err != nil { + if err := assignFlag(*flagDeleted.Pair, flags.SetDeleted, flags.UnsetDeleted); err != nil { return err } - if err := assignFlag(*flagDraft.pair, flags.SetDraft, flags.UnsetDraft); err != nil { + if err := assignFlag(*flagDraft.Pair, flags.SetDraft, flags.UnsetDraft); err != nil { return err } @@ -202,11 +150,11 @@ func parseMessageSeq(seq *imap.SeqSet, data string) error { return nil } -func assignFlag(flagOp pair, setCallback, unsetCallback func()) error { +func assignFlag(flagOp cli.Pair, setCallback, unsetCallback func()) error { switch flagOp { - case pair{true, false}: + case cli.Pair{true, false}: setCallback() - case pair{false, true}: + case cli.Pair{false, true}: unsetCallback() default: return nil diff --git a/cli/utils.go b/cli/utils.go index d808595..4edd324 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -1,6 +1,7 @@ package cli import ( + "errors" "flag" "fmt" "io" @@ -36,3 +37,50 @@ func Usage(w io.Writer, info ProgramInfo) { fmt.Fprintf(w, "\t%s\n", command) } } + +type Pair struct { + Set, Unset bool +} + +type PairFlag struct { + *Pair +} + +func NewPairFlag() PairFlag { + return PairFlag{ + &Pair{false, false}, + } +} + +func (p Pair) String() string { + switch p { + case Pair{true, false}: + return "set" + case Pair{false, true}: + return "unset" + default: + return "undef" + } +} + +func (pf PairFlag) String() string { + return pf.Pair.String() +} + +func (pf PairFlag) Set(s string) error { + if s == "set" { + v := Pair{true, false} + *pf.Pair = v + return nil + } + + if s == "unset" { + v := Pair{false, true} + *pf.Pair = v + return nil + } + + return ErrAllowedPairFlagValues +} + +var ErrAllowedPairFlagValues = errors.New("only `set` or `unset` are allowed")