move around

trunk
vilmibm 2022-04-10 22:59:26 -05:00
parent 24b456c14d
commit a87c941bb3
3 changed files with 55 additions and 49 deletions

4
go.sum 100644
View File

@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -0,0 +1,51 @@
package main
import (
"fmt"
"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 `yaml:"allow_anon"`
Debug bool
}
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)
}
var cfg Config
if err := yaml.Unmarshal(cfgBytes, &cfg); err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
if cfg.Port == 0 {
cfg.Port = defaultPort
}
if cfg.InstanceName == "" {
cfg.InstanceName = defaultInstanceName
}
if cfg.Host == "" {
cfg.Host = defaultHost
}
return &cfg, nil
}

View File

@ -5,25 +5,8 @@ import (
"fmt" "fmt"
"io" "io"
"os" "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 { type iostreams struct {
Err io.Writer Err io.Writer
Out io.Writer Out io.Writer
@ -62,35 +45,3 @@ func _main(opts Opts) error {
return nil 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
}