add docker

This commit is contained in:
shoopea 2021-11-07 00:13:45 +08:00
parent 1fba5c61cc
commit eaa1a7fed2
7 changed files with 54 additions and 14 deletions

23
Dockerfile Normal file
View File

@ -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"]

View File

@ -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

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

22
main.go
View File

@ -5,15 +5,20 @@ import (
"time"
_ "embed"
"github.com/ianschenck/envflag"
)
var (
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")
}

2
ttd.go
View File

@ -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)
}
}

View File

@ -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"