diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0e97aaa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM golang:alpine as builder + +RUN apk add --no-cache \ + git + +WORKDIR /app + +ARG COMMIT=latest + +RUN GOBIN=/app go install git.siteop.biz/shoopea/gottdad@$COMMIT + +FROM alpine:latest + +RUN apk add --no-cache \ + libstdc++ + +WORKDIR /app/ + +# Copy the Pre-built binary file from the previous stage +COPY --from=builder /app/gotdadd . + +# Command to run the executable +CMD ["./gotdadd"] \ No newline at end of file diff --git a/config.go b/config.go index 3046ba8..133d971 100644 --- a/config.go +++ b/config.go @@ -66,7 +66,7 @@ func (c *Config) Load(path string) error { b, err := ioutil.ReadFile(path) if err != nil { - return err + return c.Save(path) } err = json.Unmarshal(b, &c) @@ -74,7 +74,7 @@ func (c *Config) Load(path string) error { return err } - return nil + return c.Save(path) } // Save config values to a given file diff --git a/go.mod b/go.mod index b5c94d5..63cb165 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.siteop.biz/shoopea/gottdad go 1.16 require ( + github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133 github.com/tidwall/pretty v1.2.0 gopkg.in/tucnak/telebot.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index ffd1e07..e84da31 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133 h1:h6FO/Da7rdYqJbRYMW9f+SMBWnJVguWh+0ERefW8zp8= +github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133/go.mod h1:pyYc5lldRtL0l5YitYVv1dLKuC0qhMfAfiR7BLsN2pA= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/main.go b/main.go index 84c6533..bcd6979 100644 --- a/main.go +++ b/main.go @@ -5,15 +5,20 @@ import ( "time" _ "embed" + + "github.com/ianschenck/envflag" ) var ( - cfg *Config - srv *ServerTTD - bot *Bot + cfg *Config + srv *ServerTTD + bot *Bot + configFile string - configFlag = flag.String("config", "config.json", "config file") + configFlag = flag.String("config", "", "config file") initFlag = flag.Bool("init", false, "init config") + configEnv = envflag.String("CONFIG", "", "config file") + initEnv = envflag.Bool("INIT", false, "init config") ) func main() { @@ -21,14 +26,23 @@ func main() { flag.Parse() + if *configFlag != "" { + configFile = *configFlag + } else if *configEnv != "" { + configFile = *configEnv + } else { + configFile = "config.json" + } + cfg = &Config{} - if *initFlag { + if *initFlag || *initEnv { + logInfoWarn("Initializing configuration..") err = cfg.Init() failError(err, "Cannot init config") - err = cfg.Save(*configFlag) + err = cfg.Save(configFile) failError(err, "Cannot save config") } else { - err = cfg.Load(*configFlag) + err = cfg.Load(configFile) failError(err, "Cannot open config") } diff --git a/ttd.go b/ttd.go index a3e999b..58e88a4 100644 --- a/ttd.go +++ b/ttd.go @@ -143,7 +143,7 @@ func (s *ServerTTD) HeartBeat(stop chan struct{}) { } else if s.Status.Paused && !s.NeedPause() { s.Unpause() } - cfg.Save(*configFlag) + cfg.Save(configFile) time.Sleep(updateHeartBeat) } } diff --git a/version.go b/version.go index 7c3c473..e70234c 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "5fad9f0" -var buildstamp = "2021-11-06_15:33:27" -var commits = "157" -var version = "5fad9f0-b157 - 2021-11-06_15:33:27" +var githash = "1fba5c6" +var buildstamp = "2021-11-06_16:13:18" +var commits = "158" +var version = "1fba5c6-b158 - 2021-11-06_16:13:18"