-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
81 lines (70 loc) · 2.12 KB
/
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
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
// Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/docopt/docopt-go"
)
// Conf : Type to represent options
type Conf struct {
Oneshot bool
OutputFilePath string
SleepInterval time.Duration
}
func (conf *Conf) getConfFromArgv(argv []string) {
usage := fmt.Sprintf(`%[1]s:
Usage:
%[1]s [--oneshot | --sleep-interval=<seconds>] [--output-file=<file> | -o <file>]
%[1]s -h | --help
%[1]s --version
Options:
-h --help Show this help message and exit
--version Display version and exit
--oneshot Label once and exit
--sleep-interval=<seconds> Time to sleep between labeling [Default: 60s]
-o <file> --output-file=<file> Path to output file
[Default: /etc/kubernetes/node-feature-discovery/features.d/hfd]`,
Bin)
opts, err := docopt.ParseArgs(usage, argv[1:], Bin+" "+Version)
if err != nil {
log.Fatal("Error while parsing command line options: ", err)
}
conf.Oneshot, err = opts.Bool("--oneshot")
if err != nil {
log.Fatal("Error while parsing command line options: ", err)
}
sleepIntervalString, err := opts.String("--sleep-interval")
if err != nil {
log.Fatal("Error while parsing command line options: ", err)
}
conf.OutputFilePath, err = opts.String("--output-file")
if err != nil {
log.Fatal("Error while parsing command line options: ", err)
}
conf.SleepInterval, err = time.ParseDuration(sleepIntervalString)
if err != nil {
log.Fatal("Invalid value for --sleep-interval option: ", err)
}
return
}
func (conf *Conf) getConfFromEnv() {
val, ok := os.LookupEnv("HFD_ONESHOT")
if ok && strings.EqualFold(val, "true") {
conf.Oneshot = true
}
sleepIntervalString, ok := os.LookupEnv("HFD_SLEEP_INTERVAL")
if ok {
var err error
conf.SleepInterval, err = time.ParseDuration(sleepIntervalString)
if err != nil {
log.Fatal("Invalid value from env for sleep-interval option: ", err)
}
}
outputFilePathTmp, ok := os.LookupEnv("HFD_OUTPUT_FILE")
if ok {
conf.OutputFilePath = outputFilePathTmp
}
}