/
args.go
141 lines (117 loc) · 3.62 KB
/
args.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"gopkg.in/natefinch/lumberjack.v2"
"gopkg.in/yaml.v2"
"github.com/cyverse/ldap-irods-auth/commons"
log "github.com/sirupsen/logrus"
)
const (
ChildProcessArgument = "child_process"
)
func processArguments() (*commons.Config, io.WriteCloser, error, bool) {
logger := log.WithFields(log.Fields{
"package": "main",
"function": "processArguments",
})
var version bool
var help bool
var configFilePath string
config := commons.NewDefaultConfig()
// Parse parameters
flag.BoolVar(&version, "version", false, "Print client version information")
flag.BoolVar(&version, "v", false, "Print client version information (shorthand form)")
flag.BoolVar(&help, "h", false, "Print help")
flag.StringVar(&configFilePath, "config", "", "Set Config YAML File")
flag.BoolVar(&config.Foreground, "f", false, "Run in foreground")
flag.BoolVar(&config.ChildProcess, ChildProcessArgument, false, "")
flag.StringVar(&config.LogPath, "log", commons.LogFilePathDefault, "Set log file path")
flag.Parse()
if version {
info, err := commons.GetVersionJSON()
if err != nil {
logger.WithError(err).Error("failed to get client version info")
return nil, nil, err, true
}
fmt.Println(info)
return nil, nil, nil, true
}
if help {
flag.Usage()
return nil, nil, nil, true
}
var logWriter io.WriteCloser
if config.LogPath == "-" || len(config.LogPath) == 0 {
log.SetOutput(os.Stderr)
} else {
logWriter = getLogWriter(config.LogPath)
// use multi output - to output to file and stdout
mw := io.MultiWriter(os.Stderr, logWriter)
log.SetOutput(mw)
}
logger.Infof("Logging to %s", config.LogPath)
if len(configFilePath) == 0 {
// read from Environmental variables
envConfig, err := commons.NewConfigFromENV()
if err != nil {
logger.WithError(err).Error("failed to read Environmental Variables")
return nil, logWriter, err, true
}
envConfig.Foreground = config.Foreground
// overwrite
config = envConfig
} else if configFilePath == "-" {
// read from stdin
stdinReader := bufio.NewReader(os.Stdin)
yamlBytes, err := ioutil.ReadAll(stdinReader)
if err != nil {
logger.WithError(err).Error("failed to read STDIN")
return nil, logWriter, err, true
}
err = yaml.Unmarshal(yamlBytes, &config)
if err != nil {
return nil, logWriter, fmt.Errorf("failed to unmarshal YAML - %v", err), true
}
} else {
// read config
configFileAbsPath, err := filepath.Abs(configFilePath)
if err != nil {
logger.WithError(err).Errorf("failed to access the local yaml file %s", configFilePath)
return nil, logWriter, err, true
}
fileinfo, err := os.Stat(configFileAbsPath)
if err != nil {
logger.WithError(err).Errorf("failed to access the local yaml file %s", configFileAbsPath)
return nil, logWriter, err, true
}
if fileinfo.IsDir() {
logger.WithError(err).Errorf("local yaml file %s is not a file", configFileAbsPath)
return nil, logWriter, fmt.Errorf("local yaml file %s is not a file", configFileAbsPath), true
}
yamlBytes, err := ioutil.ReadFile(configFileAbsPath)
if err != nil {
logger.WithError(err).Errorf("failed to read the local yaml file %s", configFileAbsPath)
return nil, logWriter, err, true
}
err = yaml.Unmarshal(yamlBytes, &config)
if err != nil {
return nil, logWriter, fmt.Errorf("failed to unmarshal YAML - %v", err), true
}
}
return config, logWriter, nil, false
}
func getLogWriter(logPath string) io.WriteCloser {
return &lumberjack.Logger{
Filename: logPath,
MaxSize: 10, // 10MB
MaxBackups: 3,
MaxAge: 30, // 30 days
Compress: false,
}
}