forked from tildetown/bbj2
		
	read config
This commit is contained in:
		
							parent
							
								
									9e0a4cac72
								
							
						
					
					
						commit
						056de14107
					
				
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							| @ -1,3 +1,5 @@ | ||||
| module git.tilde.town/tildetown/bbj2 | ||||
| 
 | ||||
| go 1.18 | ||||
| 
 | ||||
| require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b | ||||
|  | ||||
| @ -1,7 +1,96 @@ | ||||
| package main | ||||
| 
 | ||||
| import "fmt" | ||||
| import ( | ||||
| 	"flag" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"os" | ||||
| 
 | ||||
| 	yaml "gopkg.in/yaml.v3" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	defaultPort         = 7099 | ||||
| 	defaultInstanceName = "BBJ" | ||||
| 	defaultHost         = "127.0.0.1" | ||||
| ) | ||||
| 
 | ||||
| type Config struct { | ||||
| 	Admins       []string | ||||
| 	Port         int | ||||
| 	Host         string | ||||
| 	InstanceName string `yaml:"instance_name"` | ||||
| 	AllowAnon    bool | ||||
| 	Debug        bool | ||||
| } | ||||
| 
 | ||||
| type iostreams struct { | ||||
| 	Err io.Writer | ||||
| 	Out io.Writer | ||||
| } | ||||
| 
 | ||||
| type Opts struct { | ||||
| 	ConfigPath string | ||||
| 	IO         iostreams | ||||
| } | ||||
| 
 | ||||
| func main() { | ||||
| 	fmt.Println("bbj2 server") | ||||
| 	var configFlag = flag.String("config", "config.yml", "A path to a config file.") | ||||
| 	flag.Parse() | ||||
| 	opts := Opts{ | ||||
| 		ConfigPath: *configFlag, | ||||
| 		IO: iostreams{ | ||||
| 			Err: os.Stderr, | ||||
| 			Out: os.Stdout, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	err := _main(opts) | ||||
| 	if err != nil { | ||||
| 		fmt.Fprintf(os.Stderr, "failed: %s", err) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func _main(opts Opts) error { | ||||
| 	cfg, err := parseConfig(opts.ConfigPath) | ||||
| 	if err != nil { | ||||
| 		fmt.Fprintf(os.Stderr, "could not read config file '%s'", opts.ConfigPath) | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| 
 | ||||
| 	fmt.Printf("DBG %#v\n", cfg.InstanceName) | ||||
| 
 | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| func parseConfig(configPath string) (*Config, error) { | ||||
| 	cfgBytes, err := os.ReadFile(configPath) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("failed to read config file: %w", err) | ||||
| 	} | ||||
| 
 | ||||
| 	fmt.Printf("DBG %#v\n", string(cfgBytes)) | ||||
| 
 | ||||
| 	var cfg Config | ||||
| 
 | ||||
| 	if err := yaml.Unmarshal(cfgBytes, &cfg); err != nil { | ||||
| 		return nil, fmt.Errorf("failed to parse config: %w", err) | ||||
| 	} | ||||
| 
 | ||||
| 	fmt.Printf("DBG %#v\n", cfg) | ||||
| 
 | ||||
| 	if cfg.Port == 0 { | ||||
| 		cfg.Port = defaultPort | ||||
| 	} | ||||
| 
 | ||||
| 	if cfg.InstanceName == "" { | ||||
| 		cfg.InstanceName = defaultInstanceName | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	if cfg.Host == "" { | ||||
| 		cfg.Host = defaultHost | ||||
| 	} | ||||
| 
 | ||||
| 	return &cfg, nil | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user