Make progress on configs

Amolith created

Change summary

config.go   | 28 ++++++++++++++++++++++++++++
config.yaml |  2 ++
go.mod      |  5 ++++-
go.sum      |  2 ++
main.go     | 15 +++++++++++++--
5 files changed, 49 insertions(+), 3 deletions(-)

Detailed changes

config.go 🔗

@@ -0,0 +1,28 @@
+package main
+
+import (
+	"fmt"
+	"io/ioutil"
+	"log"
+	"os"
+
+	yaml "gopkg.in/yaml.v2"
+)
+
+func parseConfig(configFile *string) {
+	configBytes, err := ioutil.ReadFile(*configFile)
+	if err != nil {
+		log.Println("Config file note found, writing default values to config.yaml")
+		writeDefaultConfig()
+		os.Exit(0)
+	}
+	config := make(map[interface{}]interface{})
+	err = yaml.Unmarshal(configBytes, &config)
+	for k, v := range config {
+		fmt.Printf("%s -> %s\n", k, v)
+	}
+}
+
+// TODO
+func writeDefaultConfig() {
+}

config.yaml 🔗

@@ -0,0 +1,2 @@
+listen: 127.0.0.1:8275
+access_token: mRp3cf9Zxv445DFACsRnaZKW26oehZujBhNneTFLnjXHE7E6Ia

go.mod 🔗

@@ -2,4 +2,7 @@ module git.sr.ht/~amolith/umu
 
 go 1.17
 
-require github.com/spf13/pflag v1.0.5 // indirect
+require (
+	github.com/spf13/pflag v1.0.5 // indirect
+	gopkg.in/yaml.v2 v2.4.0
+)

go.sum 🔗

@@ -64,3 +64,5 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

main.go 🔗

@@ -13,14 +13,25 @@ import (
 var templates embed.FS
 
 var (
-	flagListen *string = flag.StringP("listen", "l", "127.0.0.1:2857", "Host and port umu will listen on")
-	flagConfig *string = flag.StringP("config", "c", "config.yaml", "Path to config file")
+	flagListen      *string = flag.StringP("listen", "l", "127.0.0.1:2857", "Host and port umu will listen on")
+	flagConfig      *string = flag.StringP("config", "c", "config.yaml", "Path to config file")
+	flagAccessToken *string = flag.StringP("accesstoken", "a", "CHANGEME", "Access token, best defined in config.yaml")
 )
 
+type config struct {
+	listen      string
+	config      string
+	accessToken string
+}
+
 func init() {
 	flag.Parse()
 	fmt.Println("Listening on", *flagListen)
 	fmt.Println("Reading config at", *flagConfig)
+	parseConfig(flagConfig)
+	if *flagAccessToken == "CHANGEME" {
+		log.Fatalln("Access token *must* be defined, either by flag or config file.")
+	}
 }
 
 func main() {