@@ -44,6 +44,8 @@ func fileExists(name string) bool {
4444 return true
4545}
4646
47+ // findConfig locates a config file at the given locations with either a .conf or .toml extension
48+ // (file format must be TOML, however)
4749func findConfig (files ... string ) string {
4850 for _ , config := range files {
4951 if config != "" {
@@ -70,14 +72,6 @@ func homeConfPath() string {
7072 return ""
7173}
7274
73- func findClientConfig (path string ) string {
74- return findConfig (path , ".vproxy.conf" , homeConfPath (), "/usr/local/etc/vproxy.conf" , "/etc/vproxy.conf" )
75- }
76-
77- func findDaemonConfig (path string ) string {
78- return findConfig (path , homeConfPath (), "/usr/local/etc/vproxy.conf" , "/etc/vproxy.conf" )
79- }
80-
8175func loadConfigFile (path string ) (* Config , error ) {
8276 t , err := toml .LoadFile (path )
8377 if err != nil {
@@ -102,48 +96,21 @@ func cleanListenAddr(c *cli.Context) {
10296}
10397
10498func loadClientConfig (c * cli.Context ) error {
105- conf := findClientConfig (c .String ("config" ))
106- if cf := c .String ("config" ); c .IsSet ("config" ) && conf != cf {
107- log .Fatalf ("error: config file not found: %s\n " , cf )
108- }
109- if conf == "" {
110- return nil
111- }
112- verbose (c , "Loading config file %s" , conf )
113- config , err := loadConfigFile (conf )
114- if err != nil {
115- return err
116- }
117- if config != nil {
118- if v := (config .Client .Verbose || config .Verbose ); v && ! c .IsSet ("verbose" ) {
119- c .Lineage ()[1 ].Set ("verbose" , "true" )
120- verbose (c , "Loading config file %s" , conf )
121- verbose (c , "via conf: verbose=true" )
122- }
123- if v := config .Client .Host ; v != "" && ! c .IsSet ("host" ) {
124- verbose (c , "via conf: host=%s" , v )
125- c .Set ("host" , v )
126- }
127- if v := config .Client .HTTP ; v > 0 && ! c .IsSet ("http" ) {
128- verbose (c , "via conf: http=%d" , v )
129- c .Set ("http" , strconv .Itoa (v ))
130- }
131- if v := config .Client .Bind ; v != "" && ! c .IsSet ("bind" ) {
132- verbose (c , "via conf: bind=%s" , v )
133- c .Set ("bind" , v )
134- }
135- if v := config .Server .CaRootPath ; v != "" {
136- os .Setenv ("CAROOT_PATH" , v )
137- verbose (c , "via conf: CAROOT_PATH=%s" , v )
138- }
139- }
140- return nil
99+ conf := findConfigFile (c .String ("config" ), false )
100+ return loadConfig (c , conf )
141101}
142102
143103func loadDaemonConfig (c * cli.Context ) error {
144- conf := findClientConfig (c .String ("config" ))
145- if cf := c .String ("config" ); c .IsSet ("config" ) && conf != cf {
146- log .Fatalf ("error: config file not found: %s\n " , cf )
104+ conf := findConfigFile (c .String ("config" ), true )
105+ return loadConfig (c , conf )
106+ }
107+
108+ func loadConfig (c * cli.Context , conf string ) error {
109+ if c .IsSet ("config" ) {
110+ if cf := c .String ("config" ); conf != cf {
111+ // config flag was passed but file does not exist
112+ log .Fatalf ("error: config file not found: %s\n " , cf )
113+ }
147114 }
148115 if conf == "" {
149116 return nil
@@ -181,6 +148,29 @@ func loadDaemonConfig(c *cli.Context) error {
181148 os .Setenv ("CERT_PATH" , v )
182149 verbose (c , "via conf: CERT_PATH=%s" , v )
183150 }
151+
152+ // client configs
153+ if v := (config .Client .Verbose || config .Verbose ); v && ! c .IsSet ("verbose" ) {
154+ c .Lineage ()[1 ].Set ("verbose" , "true" )
155+ verbose (c , "Loading config file %s" , conf )
156+ verbose (c , "via conf: verbose=true" )
157+ }
158+ if v := config .Client .Host ; v != "" && ! c .IsSet ("host" ) {
159+ verbose (c , "via conf: host=%s" , v )
160+ c .Set ("host" , v )
161+ }
162+ if v := config .Client .HTTP ; v > 0 && ! c .IsSet ("http" ) {
163+ verbose (c , "via conf: http=%d" , v )
164+ c .Set ("http" , strconv .Itoa (v ))
165+ }
166+ if v := config .Client .Bind ; v != "" && ! c .IsSet ("bind" ) {
167+ verbose (c , "via conf: bind=%s" , v )
168+ c .Set ("bind" , v )
169+ }
170+ if v := config .Server .CaRootPath ; v != "" {
171+ os .Setenv ("CAROOT_PATH" , v )
172+ verbose (c , "via conf: CAROOT_PATH=%s" , v )
173+ }
184174 }
185175 cleanListenAddr (c )
186176 return nil
0 commit comments