diff --git a/Taskfile.yaml b/Taskfile.yaml index 7dfb079..cb803e4 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -11,7 +11,7 @@ tasks: - go vet . - go clean . - task: build - - stampy + - ./dist/stampy_linux_386/stampy silent: false go_build: @@ -20,10 +20,6 @@ tasks: - go build -o ./build . - ./build/stampy - rm_deb: - ignore_error: true - cmds: - - sudo dpkg -r stampy -y build: - goreleaser --snapshot --clean diff --git a/go.mod b/go.mod index aa51c08..d22f41d 100644 --- a/go.mod +++ b/go.mod @@ -1,18 +1,22 @@ -module stampy +module github.com/jelloeater/stampy go 1.22 require ( github.com/atotto/clipboard v0.1.4 github.com/beevik/ntp v1.4.3 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli/v2 v2.27.4 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 6d52675..7a49e1d 100644 --- a/go.sum +++ b/go.sum @@ -20,5 +20,7 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index f4c99d5..209b82a 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,6 @@ import ( "os" "time" - "github.com/atotto/clipboard" - "github.com/beevik/ntp" "github.com/urfave/cli/v2" ) @@ -17,46 +15,6 @@ var ( // Create by GoRelease at compile time //date = "unknown" ) -func writeDate(format string, timezone string, ntpServer string, diaryFormat bool) { - - if os.Getenv("STAMPY_TZ") != "" { - timezone = os.Getenv("STAMPY_TZ") - } - if os.Getenv("STAMPY_FORMAT") != "" { - format = os.Getenv("STAMPY_FORMAT") - } - if os.Getenv("STAMPY_NTP") != "" { - ntpServer = os.Getenv("STAMPY_NTP") - } - if diaryFormat { - format = "Monday January 2 2006 3:04PM" - } - - now := time.Time{} // Declare now outside the if-else block - - if timezone != "" { - now = time.Now() - } else { - loc, e := time.LoadLocation(timezone) - if e != nil { - log.Fatal(e) - } - now = time.Now().In(loc) - } - - if ntpServer != "" { // Override local time with NTP server - ntpTime, err := ntp.Time(ntpServer) - if err == nil { // Check for errors when getting NTP time - now = ntpTime - println("NTP from " + ntpServer) - } - } - timestamp := now.Format(format) - clip := timestamp - println(clip + " copied to clipboard") - _ = clipboard.WriteAll(clip) -} - func mainCliApp() error { authors := []*cli.Author{ { diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..0e2c723 --- /dev/null +++ b/main_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestWriteDate(t *testing.T) { + // Backup original environment variables + originalTZ := os.Getenv("STAMPY_TZ") + originalFormat := os.Getenv("STAMPY_FORMAT") + originalNTP := os.Getenv("STAMPY_NTP") + defer func() { + os.Setenv("STAMPY_TZ", originalTZ) + os.Setenv("STAMPY_FORMAT", originalFormat) + os.Setenv("STAMPY_NTP", originalNTP) + }() + + t.Run("Default behavior", func(t *testing.T) { + writeDate("", "", "", false) + // Add assertions based on expected output + }) + + t.Run("With environment variables", func(t *testing.T) { + os.Setenv("STAMPY_TZ", "America/New_York") + os.Setenv("STAMPY_FORMAT", "2006-01-02") + os.Setenv("STAMPY_NTP", "pool.ntp.org") + writeDate("", "", "", false) + // Add assertions based on expected output + }) + + t.Run("Diary format", func(t *testing.T) { + writeDate("", "", "", true) + // Add assertions based on expected output + }) + + t.Run("Error loading location", func(t *testing.T) { + os.Setenv("STAMPY_TZ", "Invalid/Timezone") + assert.Panics(t, func() { writeDate("", "", "", false) }) + }) +} diff --git a/worker.go b/worker.go new file mode 100644 index 0000000..7a46fed --- /dev/null +++ b/worker.go @@ -0,0 +1,48 @@ +package main + +import ( + "github.com/atotto/clipboard" + "github.com/beevik/ntp" + "log" + "os" + "time" +) + +func writeDate(format string, timezone string, ntpServer string, diaryFormat bool) { + + if os.Getenv("STAMPY_TZ") != "" { + timezone = os.Getenv("STAMPY_TZ") + } + if os.Getenv("STAMPY_FORMAT") != "" { + format = os.Getenv("STAMPY_FORMAT") + } + if os.Getenv("STAMPY_NTP") != "" { + ntpServer = os.Getenv("STAMPY_NTP") + } + if diaryFormat { + format = "Monday January 2 2006 3:04PM" + } + + now := time.Time{} // Declare now outside the if-else block + + if timezone != "" { + loc, err := time.LoadLocation(timezone) + if err != nil { + log.Panic(err) + } + now = time.Now().In(loc) + } else { + now = time.Now() + } + if ntpServer != "" { // Override local time with NTP server + ntpTime, err := ntp.Time(ntpServer) + if err == nil { // Check for errors when getting NTP time + now = ntpTime + println("NTP from " + ntpServer) + } + } + timestamp := now.Format(format) + clip := timestamp + println(clip + " copied to clipboard") + _ = clipboard.WriteAll(clip) +}