Initial commit
This commit is contained in:
commit
5d0a210e6d
16 changed files with 998 additions and 0 deletions
44
config/cli.go
Normal file
44
config/cli.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
)
|
||||
|
||||
// declareFlags registers flags for the structure Options.
|
||||
func (c *Config) declareFlags() {
|
||||
flag.StringVar(&c.BaseURL, "baseurl", c.BaseURL, "URL prepended to each URL")
|
||||
flag.StringVar(&c.Bind, "bind", c.Bind, "Bind port/socket")
|
||||
flag.StringVar(&c.DevProxy, "dev", c.DevProxy, "Use ui directory instead of embedded assets")
|
||||
|
||||
// Others flags are declared in some other files when they need specials configurations
|
||||
}
|
||||
|
||||
func Consolidated() (cfg *Config, err error) {
|
||||
// Define defaults options
|
||||
cfg = &Config{
|
||||
Bind: "127.0.0.1:8080",
|
||||
}
|
||||
|
||||
cfg.declareFlags()
|
||||
|
||||
// Then, overwrite that by what is present in the environment
|
||||
err = cfg.FromEnv()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Finaly, command line takes precedence
|
||||
err = cfg.parseCLI()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// parseCLI parse the flags and treats extra args as configuration filename.
|
||||
func (c *Config) parseCLI() error {
|
||||
flag.Parse()
|
||||
|
||||
return nil
|
||||
}
|
||||
28
config/config.go
Normal file
28
config/config.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DevProxy string
|
||||
Bind string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
// parseLine treats a config line and place the read value in the variable
|
||||
// declared to the corresponding flag.
|
||||
func (c *Config) parseLine(line string) (err error) {
|
||||
fields := strings.SplitN(line, "=", 2)
|
||||
orig_key := strings.TrimSpace(fields[0])
|
||||
value := strings.TrimSpace(fields[1])
|
||||
|
||||
key := strings.TrimPrefix(orig_key, "HATHORIS_")
|
||||
key = strings.Replace(key, "_", "-", -1)
|
||||
key = strings.ToLower(key)
|
||||
|
||||
err = flag.Set(key, value)
|
||||
|
||||
return
|
||||
}
|
||||
21
config/env.go
Normal file
21
config/env.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FromEnv analyzes all the environment variables to find each one
|
||||
// starting by HATHORIS_
|
||||
func (c *Config) FromEnv() error {
|
||||
for _, line := range os.Environ() {
|
||||
if strings.HasPrefix(line, "HATHORIS_") {
|
||||
err := c.parseLine(line)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error in environment (%q): %w", line, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue