-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
46 lines (40 loc) · 931 Bytes
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package util
import (
"context"
"github.com/go-kivik/kouch"
"github.com/spf13/pflag"
)
// CommonOptions parses options common to most or all commands.
func CommonOptions(ctx context.Context, scope kouch.TargetScope, flags *pflag.FlagSet) (*kouch.Options, error) {
o := kouch.NewOptions()
var err error
o.Target, err = kouch.NewTarget(ctx, scope, flags)
if err != nil {
return nil, err
}
if e := o.SetParam(flags, kouch.FlagRev); e != nil {
return nil, e
}
if e := setAutoRev(ctx, o, flags); e != nil {
return nil, e
}
return o, nil
}
func setAutoRev(ctx context.Context, o *kouch.Options, flags *pflag.FlagSet) error {
if flag := flags.Lookup(kouch.FlagAutoRev); flag == nil {
return nil
}
autoRev, err := flags.GetBool(kouch.FlagAutoRev)
if err != nil {
return err
}
if !autoRev {
return nil
}
rev, e := FetchRev(ctx, o)
if e != nil {
return e
}
o.Query().Set("rev", rev)
return nil
}