From 9dd6658779220747dc233d5a0592474f2424eec7 Mon Sep 17 00:00:00 2001 From: Fabio Codiglioni Date: Sun, 2 Jan 2022 18:15:22 +0100 Subject: [PATCH] Remove sqlite - Move all source code to main.go - Restore .goreleaser.yml --- .goreleaser.yml | 47 ++++++++++++++++++++--------------------------- README.md | 2 +- args.go | 10 ---------- errors.go | 10 ---------- go.mod | 7 +------ go.sum | 24 ++---------------------- main.go | 36 +++++++++++++++++++++++++++++++++++- model.go | 44 -------------------------------------------- 8 files changed, 59 insertions(+), 121 deletions(-) delete mode 100644 args.go delete mode 100644 errors.go delete mode 100644 model.go diff --git a/.goreleaser.yml b/.goreleaser.yml index a9d46d1..7f086f5 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,33 +1,26 @@ before: hooks: - - go mod tidy + - go mod download -builds: - - id: linux - ldflags: - - -s -w -extldflags=-static - binary: ovh-ddns-update - goos: - - linux - goarch: - - amd64 - - 386 - - arm - - arm64 - goarm: - - 5 - - 6 - - 7 - - - id: macos - ldflags: - - -s -w - binary: ovh-ddns-update - goos: - - darwin - goarch: - - amd64 - - arm64 +build: + ldflags: + - -s -w + binary: ovh-ddns-update + goos: + - darwin + - linux + goarch: + - amd64 + - 386 + - arm + - arm64 + goarm: + - 5 + - 6 + - 7 + ignore: + - goos: darwin + goarch: 386 archives: - name_template: '{{ .Binary }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' diff --git a/README.md b/README.md index 29b365d..f485ca9 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ Positional arguments: OVHPASSWORD OVH DynHost identifier password Options: - --database DATABASE database file [default: .ovh-ddns-update.sqlite3] + --database DATABASE database file [default: .ovh-ddns-update.db] --help, -h display this help and exit ``` diff --git a/args.go b/args.go deleted file mode 100644 index d3ddd11..0000000 --- a/args.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -type ArgsType struct { - Database string `default:".ovh-ddns-update.sqlite3" help:"database file"` - Domain string `arg:"positional,required" help:"OVH DynHost domain"` - OvhId string `arg:"positional,required" help:"OVH DynHost identifier"` - OvhPassword string `arg:"positional,required" help:"OVH DynHost identifier password"` -} - -var Args ArgsType diff --git a/errors.go b/errors.go deleted file mode 100644 index 1a089ad..0000000 --- a/errors.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import log "github.com/sirupsen/logrus" - -func HandleError(err error) { - if err != nil { - log.SetReportCaller(true) - log.Fatal(err) - } -} diff --git a/go.mod b/go.mod index 95efee4..f58426b 100644 --- a/go.mod +++ b/go.mod @@ -6,14 +6,9 @@ require ( github.com/alexflint/go-arg v1.4.2 github.com/glendc/go-external-ip v0.1.0 github.com/sirupsen/logrus v1.8.1 - gorm.io/driver/sqlite v1.2.6 - gorm.io/gorm v1.22.4 ) require ( github.com/alexflint/go-scalar v1.0.0 // indirect - github.com/jinzhu/inflection v1.0.0 // indirect - github.com/jinzhu/now v1.1.4 // indirect - github.com/mattn/go-sqlite3 v1.14.9 // indirect - golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect ) diff --git a/go.sum b/go.sum index 8840688..8e69f06 100644 --- a/go.sum +++ b/go.sum @@ -2,35 +2,15 @@ github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/glendc/go-external-ip v0.1.0 h1:iX3xQ2Q26atAmLTbd++nUce2P5ht5P4uD4V7caSY/xg= github.com/glendc/go-external-ip v0.1.0/go.mod h1:CNx312s2FLAJoWNdJWZ2Fpf5O4oLsMFwuYviHjS4uJE= -github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= -github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= -github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= -github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E= -golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/driver/sqlite v1.2.6 h1:SStaH/b+280M7C8vXeZLz/zo9cLQmIGwwj3cSj7p6l4= -gorm.io/driver/sqlite v1.2.6/go.mod h1:gyoX0vHiiwi0g49tv+x2E7l8ksauLK0U/gShcdUsjWY= -gorm.io/gorm v1.22.3/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= -gorm.io/gorm v1.22.4 h1:8aPcyEJhY0MAt8aY6Dc524Pn+pO29K+ydu+e/cXSpQM= -gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk= diff --git a/main.go b/main.go index 267bd2c..e734ea6 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,20 @@ import ( "github.com/alexflint/go-arg" externalip "github.com/glendc/go-external-ip" log "github.com/sirupsen/logrus" + "io/fs" "net/http" + "os" ) +type ArgsType struct { + Database string `default:".ovh-ddns-update.db" help:"database file"` + Domain string `arg:"positional,required" help:"OVH DynHost domain"` + OvhId string `arg:"positional,required" help:"OVH DynHost identifier"` + OvhPassword string `arg:"positional,required" help:"OVH DynHost identifier password"` +} + +var Args ArgsType + func main() { arg.MustParse(&Args) log.SetFormatter(&log.TextFormatter{ @@ -15,7 +26,6 @@ func main() { FullTimestamp: true, }) log.SetLevel(log.InfoLevel) - SetupDB() currentIP := GetCurrentIP() previousIP := GetPreviousIP() if currentIP != previousIP { @@ -35,9 +45,33 @@ func GetCurrentIP() string { return ip.String() } +func GetPreviousIP() string { + data, err := os.ReadFile(Args.Database) + _, ok := err.(*fs.PathError) + if ok { + log.Warn("Previous IP not found") + return "not_found" + } else { + HandleError(err) + } + return string(data) +} + func UpdateDDNS(ip string) { url := fmt.Sprintf("https://%s:%s@www.ovh.com/nic/update?system=dyndns&hostname=%s&myip=%s", Args.OvhId, Args.OvhPassword, Args.Domain, ip) _, err := http.Get(url) HandleError(err) log.Infof("IP updated (%s)", ip) } + +func SaveCurrentIP(ip string) { + err := os.WriteFile(Args.Database, []byte(ip), 0644) + HandleError(err) +} + +func HandleError(err error) { + if err != nil { + log.SetReportCaller(true) + log.Fatal(err) + } +} diff --git a/model.go b/model.go deleted file mode 100644 index e8cfe59..0000000 --- a/model.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "errors" - log "github.com/sirupsen/logrus" - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "time" -) - -type Address struct { - ID uint8 `gorm:"primaryKey"` - Value string `gorm:"unique"` - UpdatedAt time.Time -} - -var DB *gorm.DB - -func SetupDB() { - var err error - DB, err = gorm.Open(sqlite.Open(Args.Database)) - HandleError(err) - err = DB.AutoMigrate(&Address{}) - HandleError(err) -} - -func GetPreviousIP() string { - var address Address - if result := DB.First(&address); result.Error != nil { - if errors.Is(result.Error, gorm.ErrRecordNotFound) { - log.Warning("Previous IP not found") - return "not_found" - } - HandleError(result.Error) - } - log.Infof("Previous IP is %s", address.Value) - return address.Value -} - -func SaveCurrentIP(ip string) { - address := Address{ID: 1, Value: ip} - result := DB.Save(&address) - HandleError(result.Error) -}