-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
75 lines (62 loc) · 1.96 KB
/
config.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
package main
import (
"fmt"
"os"
"time"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
// Config stores the configuration from cli flags and environment variables.
type appConfig struct {
MaxAgeOfImages time.Duration
SweeperTime time.Duration
DangleSafeDuration time.Duration
Quiet bool
Version string
}
// NewConfig initializes a Config object from the cli flags and environment variables.
func newAppConfig(args []string) (appConfig, error) {
config := appConfig{Version: version}
app := kingpin.New("docker-gc", "The missing docker garbage collector.")
app.Writer(os.Stdout)
app.HelpFlag.Short('h')
app.Author("Christian Höltje")
app.Version(config.Version)
app.VersionFlag.Short('v')
app.
Flag("max-image-age", "How old to allow images to be before deletion. (Env: DOCKER_GC_MAX_IMAGE_AGE)").
Short('m').
Default("168h").
OverrideDefaultFromEnvar("DOCKER_GC_MAX_IMAGE_AGE").
DurationVar(&config.MaxAgeOfImages)
app.
Flag("sweeper-time", "How much time between running checks to delete images. (Env: DOCKER_GC_SWEEPER_TIME)").
Short('s').
Default("15m").
OverrideDefaultFromEnvar("DOCKER_GC_SWEEPER_TIME").
DurationVar(&config.SweeperTime)
app.
Flag("dangle-safe-duration", "How old should a dangle image be before deletion. (Env: DOCKER_GC_DANGLE_SAFE_DURATION)").
Short('d').
Default("30m").
OverrideDefaultFromEnvar("DOCKER_GC_DANGLE_SAFE_DURATION").
DurationVar(&config.DangleSafeDuration)
app.
Flag("quiet", "Don't show any output. (Env: DOCKER_GC_QUIET)").
Short('q').
Default("false").
OverrideDefaultFromEnvar("DOCKER_GC_QUIET").
BoolVar(&config.Quiet)
command, err := app.Parse(args)
if err != nil {
app.Usage(nil)
return appConfig{}, err
}
if command != "" {
app.Usage(nil)
return appConfig{}, fmt.Errorf("No arguments expected")
}
if config.SweeperTime < (4 * time.Second) {
app.Fatalf("You must set the sweeper-time to greater or equal than 4s")
}
return config, nil
}