Skip to content

Commit

Permalink
Cleanup code bumb version
Browse files Browse the repository at this point in the history
- Bumb version to v1.0.1
- Cleanup code
- Update readme
- Update record only on change.
  • Loading branch information
jlentink committed Jul 11, 2020
1 parent bcacd3a commit 54071c2
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ cmd/transip-dyndns/transip.toml
go.mod
go.sum
internal/gipify/get_test.go
/go-transip-dyndns.toml
/.idea
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
.PHONY: all golint vet fmt test coverage scan build linux macos windows clean
BUILT_HASH=$(shell git rev-parse --short HEAD)
BUILT_VERSION=v1.0.0
LDFLAGS=-ldflags "-w -s"
BUILT_VERSION=v1.0.1
LDFLAGS=-ldflags "-X main.ApplicationVersion=${BUILT_VERSION} -w -s"
TRAVISBUILD?=off

all: clean linting build

clean:
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ domain-entry = "subdomain"
domain-ttl = 60
```

## Binaries available for download
Binaries are available for download in multiple formats

* Windows (32/64 Bit)
* Linux Intel (32/64 Bit)
* Linux ARM (32/64 Bit) - Run directly on most routers
* MacOS 64 Bit

Download them [here](https://github.com/jlentink/go-transip-dyndns/releases/latest).

## No association with Transip
This tool has been created for me own comfort. There is no association with Transip. But thanks for there fine service!
This tool has been created for me own comfort. There is no association with Transip. But I would like to thank TransIP for there fine service!

## PHP version
[Previous version](https://github.com/jlentink/transip-dyndns) was build in PHP.
7 changes: 6 additions & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ func Create(cmd *cobra.Command, args []string) {
if err != nil {
logger.Get().Fatalf("Error getting IP address. (%s)", err.Error())
}
err = tld.InitTLD()
err = tld.InitTLD(config.Get().GetString("username"), config.Get().GetString("private-key"))
if err != nil {
logger.Get().Fatalf("Error accessing the API. please verify configuration (%s)", err.Error())
}
tld.SetRecordInformation(
config.Get().GetString("domain"),
config.Get().GetString("domain-entry"),
config.Get().GetInt("domain-ttl"),
)
_, err = tld.FindRecord()
if err == nil {
logger.Get().Fatalf("Record already exists. Use update from now on.")
Expand Down
13 changes: 10 additions & 3 deletions commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ func Update(cmd *cobra.Command, args []string) {
if err != nil {
logger.Get().Fatalf("Error getting IP address. (%s)", err.Error())
}
err = tld.InitTLD()
err = tld.InitTLD(config.Get().GetString("username"), config.Get().GetString("private-key"))
if err != nil {
logger.Get().Fatalf("Error accessing the API. please verify configuration (%s)", err.Error())
}
err = tld.UpdateRecord(IP)
tld.SetRecordInformation(
config.Get().GetString("domain"),
config.Get().GetString("domain-entry"),
config.Get().GetInt("domain-ttl"),
)
changed, err := tld.UpdateRecord(IP)
if err != nil {
logger.Get().Fatalf("Unable to create record. (%s)", err.Error())
} else {
} else if changed {
logger.Get().Infof("Updating record for %s.%s with ip %s.", config.Get().GetString("domain-entry"), config.Get().GetString("domain"), IP.IP)
} else {
logger.Get().Infof("Record is up to date %s.%s with ip %s.", config.Get().GetString("domain-entry"), config.Get().GetString("domain"), IP.IP)
}
}
8 changes: 7 additions & 1 deletion commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Validate the setup so see all is alright
func Validate(cmd *cobra.Command, args []string) {
fmt.Printf(" - Verify access to API.\n")
err := tld.InitTLD()
err := tld.InitTLD(config.Get().GetString("username"), config.Get().GetString("private-key"))
if err != nil {
emoji.Printf(":exclamation: Could not connect to API (%s)\n", err.Error()) // nolint: errcheck
emoji.Printf("Please go to https://www.transip.nl/cp/account/api/ and create a key pair. " + // nolint: errcheck
Expand All @@ -22,6 +22,12 @@ func Validate(cmd *cobra.Command, args []string) {
emoji.Printf(":+1: Connection successful.\n") // nolint: errcheck
}

tld.SetRecordInformation(
config.Get().GetString("domain"),
config.Get().GetString("domain-entry"),
config.Get().GetInt("domain-ttl"),
)

fmt.Printf(" - Verify access to domain\n") // nolint: errcheck
dom, err := tld.FindDomain()
if err != nil {
Expand Down
80 changes: 63 additions & 17 deletions internal/tld/tld.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tld

import (
"fmt"
"github.com/jlentink/go-transip-dyndns/internal/config"
"github.com/jlentink/go-transip-dyndns/internal/gipify"
"github.com/jlentink/go-transip-dyndns/internal/logger"
"github.com/transip/gotransip/v6"
Expand All @@ -11,20 +10,43 @@ import (
)

var _transip *repository.Client
var _tld string
var _entry string
var _ttl int

// SetDomainName Sets Domain name to use for up switching
func SetDomainName(domainName string) {
_tld = domainName
}

// SetEntry Sets Entry to update in domain
func SetEntry(entry string) {
_entry = entry
}

// SetTTL Sets TTL for entry
func SetTTL(ttl int) {
_ttl = ttl
}

// SetRecordInformation Sets all needed Record information in one go.
func SetRecordInformation(domainName, entry string, ttl int) {
SetDomainName(domainName)
SetEntry(entry)
SetTTL(ttl)
}

// InitTLD setup TransAPI client
func InitTLD() error {
func InitTLD(accountName, privateKeyPath string) error {
if _transip != nil {
return nil
}
transipClient, err := gotransip.NewClient(gotransip.ClientConfiguration{
AccountName: config.Get().GetString("username"),
PrivateKeyPath: config.Get().GetString("private-key"),
AccountName: accountName,
PrivateKeyPath: privateKeyPath,
})
_transip = &transipClient
return err
//logger.Get().Fatalf("Could not connect to transIP API. (%s)", err.Error())

}

// CreateRecord creates a record in the domain
Expand All @@ -42,16 +64,16 @@ func CreateRecord(ip *gipify.IP) error {

logger.Get().Debug("Create Record..\n")
repo := domain.Repository{Client: *_transip}
return repo.AddDNSEntry(config.Get().GetString("domain"), domain.DNSEntry{
Name: config.Get().GetString("domain-entry"),
Expire: config.Get().GetInt("domain-ttl"),
return repo.AddDNSEntry(_tld, domain.DNSEntry{
Name: _entry,
Expire: _ttl,
Type: recordType,
Content: ip.IP,
})
}

// UpdateRecord updates an existing record
func UpdateRecord(ip *gipify.IP) error {
func UpdateRecord(ip *gipify.IP) (bool, error) {
recordType := "A"

switch ip.Type {
Expand All @@ -65,32 +87,56 @@ func UpdateRecord(ip *gipify.IP) error {

logger.Get().Debug("Update Record..\n")
repo := domain.Repository{Client: *_transip}
return repo.UpdateDNSEntry(config.Get().GetString("domain"), domain.DNSEntry{
Name: config.Get().GetString("domain-entry"),
Expire: config.Get().GetInt("domain-ttl"),

change, err := isIPChanged(ip)
if err != nil {
return false, err
}

if !change {
logger.Get().Debug("IP is unchanged...\n")
return false, nil
}

return true, repo.UpdateDNSEntry(_tld, domain.DNSEntry{
Name: _entry,
Expire: _ttl,
Type: recordType,
Content: ip.IP,
})
}

func isIPChanged(ip *gipify.IP) (bool, error) {
record, err := FindRecord()

if err != nil {
return false, err
}

if record.Content != ip.IP {
return true, nil
}
return false, nil
}

// FindDomain Find domain in the API
func FindDomain() (domain.Domain, error) {
repo := domain.Repository{Client: *_transip}
return repo.GetByDomainName(config.Get().GetString("domain"))
return repo.GetByDomainName(_tld)
}

// FindRecord finds the record in the given domain
func FindRecord() (domain.DNSEntry, error) {
repo := domain.Repository{Client: *_transip}
entries, err := repo.GetDNSEntries(config.Get().GetString("domain"))
entries, err := repo.GetDNSEntries(_tld)
if err != nil {
return domain.DNSEntry{}, err
}

for _, entry := range entries {
if entry.Name == config.Get().GetString("domain-entry") {
if entry.Name == _entry {
return entry, nil
}
}
return domain.DNSEntry{}, fmt.Errorf("could not find entry (%s)", config.Get().GetString("domain-entry"))
return domain.DNSEntry{}, fmt.Errorf("could not find entry (%s)", _entry)
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

var (
rootCmd = &cobra.Command{
ApplicationVersion = "1.0.0"
rootCmd = &cobra.Command{
Use: "dyndns",
Short: "Update ip address on Transip DNS to current public ip ",
Long: "Use the current ip to update to a record in the TransIP dns.\nAllowing for easy updating when your ip changes.",
Version: "1.0.0",
Version: ApplicationVersion,
Run: commands.Update,
}
userName string
Expand Down

0 comments on commit 54071c2

Please sign in to comment.