-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
114 lines (93 loc) · 2.81 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
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
// Copyright 2021 Adam Chalkley
//
// https://github.com/atc0005/query-meta
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package config
import (
"fmt"
"os"
"github.com/alexflint/go-arg"
"github.com/atc0005/query-meta/internal/caller"
)
// see `constants.go`, `logging.go` for other related values
// version reflects the application version. This is overridden via Makefile
// for release builds.
var version = "dev build"
// Version emits application name, version and repo location.
func Version() string {
return fmt.Sprintf("%s %s (%s)", myAppName, version, myAppURL)
}
// String implements the Stringer interface in order to display all
// initialized (user-provided or default) values.
func (c Config) String() string {
return fmt.Sprintf(
"{ Host: %v, "+
"Port: %v, "+
"Instance: %v, "+
"Username: %v, "+
"Password: %v, "+
"Database: %v, "+
"EncryptMode: %v, "+
"TrustCert: %v, "+
"LogLevel: %v, "+
"DBTable: %v, "+
"DBQueryCountAllRecords: %v, "+
"DBQueryCountInactiveRecords: %v, "+
"DBQueryRetrieveActivePatronRecords: %v, "+
c.DBServerHost(),
c.DBServerPort(),
c.DBServerInstance(),
c.DBServerUsername(),
"REDACTED",
c.DBName(),
c.DBServerEncryptMode(),
c.DBServerTrustCert(),
c.LogLevel(),
c.DBTable(),
c.DBQueryCountAllRecords(),
c.DBQueryCountInactiveRecords(),
c.DBQueryRetrieveActivePatronRecords(),
)
}
// Version reuses the package-level Version function to emit version
// information and associated branding details whenever the user specifies the
// `--version` flag. The application exits after displaying this information.
func (c Config) Version() string {
return Version() + "\n"
}
// Description emits branding information whenever the user specifies the `-h`
// flag. The application uses this as a header prior to displaying available
// CLI flag options.
func (c Config) Description() string {
return fmt.Sprintf("\n%s", myAppDescription)
}
// New is a factory function that produces a new Config object based on user
// provided flag and where applicable, default values.
func New() (*Config, error) {
myFuncName := caller.GetFuncName()
var config Config
// Bundle the returned `*.arg.Parser` for potential later use.
config.flagParser = arg.MustParse(&config)
// Attempt to load requested config file if specified
if config.ConfigFile != nil && *config.ConfigFile != "" {
if err := config.loadConfigFile(*config.ConfigFile); err != nil {
return nil, fmt.Errorf(
"%s: failed to load config file: %w",
myFuncName,
err,
)
}
}
if err := config.validate(); err != nil {
config.flagParser.WriteUsage(os.Stderr)
return nil, fmt.Errorf(
"%s: failed to validate configuration: %w",
myFuncName,
err,
)
}
config.configureLogging()
return &config, nil
}