forked from robherley/snips.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.go
113 lines (85 loc) · 2.54 KB
/
flags.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package ssh
import (
"errors"
"flag"
"fmt"
"io"
"strings"
"time"
"github.com/robherley/snips.sh/internal/timeutil"
)
var (
ErrFlagRequied = errors.New("flag required")
ErrFlagParse = errors.New("parse error")
)
type UploadFlags struct {
*flag.FlagSet
Private bool
Extension string
TTL time.Duration
}
func (uf *UploadFlags) Parse(out io.Writer, args []string) error {
uf.FlagSet = flag.NewFlagSet("", flag.ContinueOnError)
uf.SetOutput(out)
uf.BoolVar(&uf.Private, "private", false, "only accessible via creator or signed urls (optional)")
uf.StringVar(&uf.Extension, "ext", "", "set the file extension (optional)")
addDurationFlag(uf.FlagSet, &uf.TTL, "ttl", 0, "lifetime of the signed url (optional)")
if err := uf.FlagSet.Parse(args); err != nil {
return err
}
if uf.TTL.Seconds() > 0 && !uf.Private {
return fmt.Errorf("%w: -private", ErrFlagRequied)
}
uf.Extension = strings.TrimPrefix(strings.ToLower(uf.Extension), ".")
return nil
}
type SignFlags struct {
*flag.FlagSet
TTL time.Duration
}
func (sf *SignFlags) Parse(out io.Writer, args []string) error {
sf.FlagSet = flag.NewFlagSet("", flag.ContinueOnError)
sf.SetOutput(out)
addDurationFlag(sf.FlagSet, &sf.TTL, "ttl", 0, "lifetime of the signed url")
if err := sf.FlagSet.Parse(args); err != nil {
return err
}
if sf.TTL.Seconds() == 0 {
return fmt.Errorf("%w: -ttl", ErrFlagRequied)
}
return nil
}
type DeleteFlags struct {
*flag.FlagSet
Force bool
}
func (df *DeleteFlags) Parse(out io.Writer, args []string) error {
df.FlagSet = flag.NewFlagSet("", flag.ContinueOnError)
df.SetOutput(out)
df.BoolVar(&df.Force, "f", false, "force delete without confirmation")
return df.FlagSet.Parse(args)
}
// durationFlagValue is a wrapper around time.Duration that implements the flag.Value interface using a custom parser.
type durationFlagValue time.Duration
// addDurationFlag adds a flag for a time.Duration to the given flag.FlagSet.
func addDurationFlag(fs *flag.FlagSet, p *time.Duration, name string, value time.Duration, usage string) {
*p = value
fs.Var((*durationFlagValue)(p), name, usage)
}
// Set implements the flag.Value interface.
func (d *durationFlagValue) Set(s string) error {
v, err := timeutil.ParseDuration(s)
if err != nil {
err = ErrFlagParse
}
*d = durationFlagValue(v)
return err
}
// Get implements the flag.Getter interface.
func (d *durationFlagValue) Get() any {
return time.Duration(*d)
}
// String implements the flag.Value interface.
func (d *durationFlagValue) String() string {
return (*time.Duration)(d).String()
}