Compare commits

...

3 Commits

Author SHA1 Message Date
vilmibm a87c941bb3 move around 2022-04-10 22:59:26 -05:00
vilmibm 24b456c14d ignore swp 2022-04-10 22:59:10 -05:00
vilmibm 056de14107 read config 2022-04-10 22:45:33 -05:00
5 changed files with 101 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.swp
#
# ---> Go # ---> Go
# Binaries for programs and plugins # Binaries for programs and plugins
*.exe *.exe

2
go.mod
View File

@ -1,3 +1,5 @@
module git.tilde.town/tildetown/bbj2 module git.tilde.town/tildetown/bbj2
go 1.18 go 1.18
require gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

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

@ -1,7 +1,47 @@
package main package main
import "fmt" import (
"flag"
"fmt"
"io"
"os"
)
type iostreams struct {
Err io.Writer
Out io.Writer
}
type Opts struct {
ConfigPath string
IO iostreams
}
func main() { 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
} }