-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_setup.go
67 lines (61 loc) · 1.43 KB
/
config_setup.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
package conf
import (
"errors"
"fmt"
"log"
"os"
)
func configPreconditions(c *Config, opts ...Option) error {
const fname = "configPreconditions"
if c.errs != nil {
return fmt.Errorf("%s: previous error: %w", fname, c.errs)
}
if len(opts) == 0 {
return fmt.Errorf("%s: no options set", fname)
}
if len(c.commands) == 0 {
return fmt.Errorf("%s: no commands set", fname)
}
if v3() {
log.Printf("%s: completed\n", fname)
}
return nil
}
// ascertainCmdSet sets the program operating mode, either the default or that
// specified by the first argument if it is not a flag.
func ascertainCmdSet(c *Config) (set CMD, err error) {
const fname = "ascertainCmdSet"
if len(os.Args) > 1 && os.Args[1][0] != '-' {
if set, err = setCommand(c, os.Args[1]); err != nil {
// Avoid an error in the case when a argument is
// required and no flags nor operating commands
// have been given, this should not raise an
// error.
if v2() {
log.Printf("%s: default: set defined, file: %s\n",
fname, os.Args[1])
}
c.set = &c.commands[0]
set = 1
if errors.Is(err, ErrUnknownCMD) {
err = nil
}
return
}
if v2() {
log.Printf("%s: %s: set defined\n", fname, os.Args[1])
}
return
}
if len(c.commands) == 0 {
const event = "empty command set"
err = fmt.Errorf("%s: %s", fname, event)
return
}
c.set = &c.commands[0]
set = 1
if v2() {
log.Printf("%s: default: set defined\n", fname)
}
return
}