-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (70 loc) · 2.37 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
package goncvoters
import (
"log"
"os"
"path/filepath"
yaml "gopkg.in/yaml.v2"
)
// ---------------------------------------------------------------------
// Type definitions
// ---------------------------------------------------------------------
type configuration struct {
SelectedColumns []string `yaml:"selected_columns"`
SanitizeColumns []string `yaml:"sanitize_columns"`
Tables []string `yaml:"tables"`
}
// ---------------------------------------------------------------------
// Constants and variables
// ---------------------------------------------------------------------
const PACKAGE_NAME = "go-ncvoters"
var Configuration *configuration
var OptQuiet bool
func init() {
Configuration = newConfiguration(getConfigurationFileName())
}
// ---------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------
// getConfigurationFileName returns the fully qualified path to the
// config.yaml file.
func getConfigurationFileName() string {
// Get the configuration file directory
configDir, _ := os.UserConfigDir()
filename := filepath.Join(configDir, PACKAGE_NAME, "config.yaml")
return filename
}
// newConfiguration creates a new selected columns object and loads it
// from a configuration file. This is an internal methods that is
// called from init().
func newConfiguration(filename string) *configuration {
p := new(configuration)
// Read the configuration file
configData, err := os.ReadFile(filename)
if err != nil {
log.Printf("%s file not found", filename)
return nil
}
err = yaml.Unmarshal(configData, p)
if err != nil {
log.Println(err)
return nil
}
return p
}
// ---------------------------------------------------------------------
// Methods
// ---------------------------------------------------------------------
// GetColumnNames returns the list of selected column names
func (config *configuration) GetColumnNames() []string {
return config.SelectedColumns
}
// GetSanitizeColumns returns the list of columns that need to be
// sanitized (i.e., have multiple embedded whitespace characters
// replaced with a single space).
func (config *configuration) GetSanitizeColumns() []string {
return config.SanitizeColumns
}
// GetTables returns the list of SQL for additional tables
func (config *configuration) GetTables() []string {
return config.Tables
}