55 "fmt"
66 "log"
77 "reflect"
8+ "strings"
89 "time"
910
1011 "github.com/heetch/confita"
@@ -15,45 +16,67 @@ import (
1516
1617// Config is the exporter CLI configuration.
1718type Config struct {
18- PIHoleProtocol string `config:"pihole_protocol"`
19- PIHoleHostname string `config:"pihole_hostname"`
20- PIHolePort uint16 `config:"pihole_port"`
21- PIHolePassword string `config:"pihole_password"`
22- PIHoleApiToken string `config:"pihole_api_token"`
23- Port string `config:"port"`
19+ PIHoleProtocol string `config:"pihole_protocol"`
20+ PIHoleHostname string `config:"pihole_hostname"`
21+ PIHolePort uint16 `config:"pihole_port"`
22+ PIHolePassword string `config:"pihole_password"`
23+ PIHoleApiToken string `config:"pihole_api_token"`
24+ }
25+
26+ type EnvConfig struct {
27+ PIHoleProtocol []string `config:"pihole_protocol"`
28+ PIHoleHostname []string `config:"pihole_hostname"`
29+ PIHolePort []uint16 `config:"pihole_port"`
30+ PIHolePassword []string `config:"pihole_password"`
31+ PIHoleApiToken []string `config:"pihole_api_token"`
32+ Port uint16 `config:"port"`
2433 Interval time.Duration `config:"interval"`
2534}
2635
27- func getDefaultConfig () * Config {
28- return & Config {
29- PIHoleProtocol : "http" ,
30- PIHoleHostname : "127.0.0.1" ,
31- PIHolePort : 80 ,
32- PIHolePassword : "" ,
33- PIHoleApiToken : "" ,
34- Port : " 9617" ,
36+ func getDefaultEnvConfig () * EnvConfig {
37+ return & EnvConfig {
38+ PIHoleProtocol : [] string { "http" } ,
39+ PIHoleHostname : [] string { "127.0.0.1" } ,
40+ PIHolePort : [] uint16 { 80 } ,
41+ PIHolePassword : [] string {} ,
42+ PIHoleApiToken : [] string {} ,
43+ Port : 9617 ,
3544 Interval : 10 * time .Second ,
3645 }
3746}
3847
3948// Load method loads the configuration by using both flag or environment variables.
40- func Load () * Config {
49+ func Load () ( * EnvConfig , [] Config ) {
4150 loaders := []backend.Backend {
4251 env .NewBackend (),
4352 flags .NewBackend (),
4453 }
4554
4655 loader := confita .NewLoader (loaders ... )
4756
48- cfg := getDefaultConfig ()
57+ cfg := getDefaultEnvConfig ()
4958 err := loader .Load (context .Background (), cfg )
5059 if err != nil {
5160 panic (err )
5261 }
5362
5463 cfg .show ()
5564
56- return cfg
65+ return cfg , cfg .Split ()
66+ }
67+
68+ func (c * Config ) String () string {
69+ ref := reflect .ValueOf (c )
70+ fields := ref .Elem ()
71+
72+ buffer := make ([]string , fields .NumField (), fields .NumField ())
73+ for i := 0 ; i < fields .NumField (); i ++ {
74+ valueField := fields .Field (i )
75+ typeField := fields .Type ().Field (i )
76+ buffer [i ] = fmt .Sprintf ("%s=%v" , typeField .Name , valueField .Interface ())
77+ }
78+
79+ return fmt .Sprintf ("<Config@%X %s>" , & c , strings .Join (buffer , ", " ))
5780}
5881
5982//Validate check if the config is valid
@@ -64,6 +87,45 @@ func (c Config) Validate() error {
6487 return nil
6588}
6689
90+ func (c EnvConfig ) Split () []Config {
91+ result := make ([]Config , 0 , len (c .PIHoleHostname ))
92+
93+ for i , hostname := range c .PIHoleHostname {
94+ config := Config {
95+ PIHoleHostname : hostname ,
96+ PIHoleProtocol : c .PIHoleProtocol [i ],
97+ PIHolePort : c .PIHolePort [i ],
98+ }
99+
100+ if c .PIHoleApiToken != nil {
101+ if len (c .PIHoleApiToken ) == 1 {
102+ if c .PIHoleApiToken [0 ] != "" {
103+ config .PIHoleApiToken = c .PIHoleApiToken [0 ]
104+ }
105+ } else if len (c .PIHoleApiToken ) > 1 {
106+ if c .PIHoleApiToken [i ] != "" {
107+ config .PIHoleApiToken = c .PIHoleApiToken [i ]
108+ }
109+ }
110+ }
111+
112+ if c .PIHolePassword != nil {
113+ if len (c .PIHolePassword ) == 1 {
114+ if c .PIHolePassword [0 ] != "" {
115+ config .PIHolePassword = c .PIHolePassword [0 ]
116+ }
117+ } else if len (c .PIHolePassword ) > 1 {
118+ if c .PIHolePassword [i ] != "" {
119+ config .PIHolePassword = c .PIHolePassword [i ]
120+ }
121+ }
122+ }
123+
124+ result = append (result , config )
125+ }
126+ return result
127+ }
128+
67129func (c Config ) hostnameURL () string {
68130 s := fmt .Sprintf ("%s://%s" , c .PIHoleProtocol , c .PIHoleHostname )
69131 if c .PIHolePort != 0 {
@@ -82,7 +144,7 @@ func (c Config) PIHoleLoginURL() string {
82144 return c .hostnameURL () + "/admin/index.php?login"
83145}
84146
85- func (c Config ) show () {
147+ func (c EnvConfig ) show () {
86148 val := reflect .ValueOf (& c ).Elem ()
87149 log .Println ("------------------------------------" )
88150 log .Println ("- PI-Hole exporter configuration -" )
@@ -95,14 +157,14 @@ func (c Config) show() {
95157 if typeField .Name != "PIHolePassword" && typeField .Name != "PIHoleApiToken" {
96158 log .Println (fmt .Sprintf ("%s : %v" , typeField .Name , valueField .Interface ()))
97159 } else {
98- showAuthenticationMethod (typeField .Name , valueField .String ())
160+ showAuthenticationMethod (typeField .Name , valueField .Len ())
99161 }
100162 }
101163 log .Println ("------------------------------------" )
102164}
103165
104- func showAuthenticationMethod (name , value string ) {
105- if len ( value ) > 0 {
166+ func showAuthenticationMethod (name string , length int ) {
167+ if length > 0 {
106168 log .Println (fmt .Sprintf ("Pi-Hole Authentication Method : %s" , name ))
107169 }
108170}
0 commit comments