Compare commits
3 Commits
9e0a4cac72
...
a87c941bb3
Author | SHA1 | Date |
---|---|---|
vilmibm | a87c941bb3 | |
vilmibm | 24b456c14d | |
vilmibm | 056de14107 |
|
@ -1,3 +1,5 @@
|
|||
*.swp
|
||||
#
|
||||
# ---> Go
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
|
|
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
|
||||
|
|
|
@ -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=
|
|
@ -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
|
||||
}
|
|
@ -1,7 +1,47 @@
|
|||
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() {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue