-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic unit tests and cleaned up package refs
- Loading branch information
1 parent
d2d5c12
commit 2f6a6f6
Showing
6 changed files
with
98 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |