Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various incremental improvements #73

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func LoadConfig(tomlFile string, tzConfigs []string) (*Config, error) {
var keysDuplicated []string
for _, keys := range allKeymaps {
for _, key := range keys {
if _, used := keysUsed[key]; used == false {
if _, used := keysUsed[key]; !used {
keysUsed[key] = true
} else {
keysDuplicated = append(keysDuplicated, key)
Expand Down
3 changes: 1 addition & 2 deletions config_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -55,7 +54,7 @@ func LoadConfigEnv(tzConfigs []string, now time.Time) (*Config, error) {
// ReadZoneFromString from current time and a zoneConf string
func ReadZoneFromString(now time.Time, zoneConf string) (*Zone, error) {
names := strings.Split(zoneConf, ",")
dbName := strings.Trim(names[0], " ")
dbName := strings.TrimSpace(names[0])
var name string
if len(names) == 2 {
name = names[1]
Expand Down
1 change: 0 additions & 1 deletion config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
"path/filepath"
"time"

Expand Down
31 changes: 31 additions & 0 deletions config_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func TestDefaultConfigFile(t *testing.T) {
}
}

func TestEmptyConfigFile(t *testing.T) {
now := time.Now()
tomlPath := "./testdata/config_file/empty.toml"
_, err := LoadConfigFile(tomlPath, now)
if err != nil {
t.Fatalf("Unexpected error reading empty config %s: %v", tomlPath, err)
}
}

func TestExampleConfigFile(t *testing.T) {
now := time.Now()
tomlPath := "./example-conf.toml"
Expand All @@ -44,3 +53,25 @@ func TestExampleConfigFile(t *testing.T) {
t.Errorf("Expected at least 2 keys for open_web in %s, found %v", tomlPath, len(config.Keymaps.OpenWeb))
}
}

func TestInvalidConfigFile(t *testing.T) {
now := time.Now()
tomlPath := "./testdata/config_file/invalid.toml"

{
_, err := LoadConfigFile(tomlPath, now)
failUnlessExpectedError(t, err, "toml: expected character ]", "reading %s", tomlPath)
}

{
_, err := LoadConfig(tomlPath, nil)
failUnlessExpectedError(t, err, "toml: expected character ]", "reading %s", tomlPath)
}
}

func TestUnknownZoneConfigFile(t *testing.T) {
now := time.Now()
tomlPath := "./testdata/config_file/unknown_zone.toml"
_, err := LoadConfigFile(tomlPath, now)
failUnlessExpectedError(t, err, "unknown time zone !", "reading %s", tomlPath)
}
10 changes: 1 addition & 9 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package main

import (
"os"
"strings"
"testing"
"time"
Expand All @@ -37,19 +36,12 @@ func TestConfigKeysDuplicated(t *testing.T) {
}

func TestLoadConfig(t *testing.T) {
oldTzList, tzListWasSet := os.LookupEnv("TZ_LIST")
os.Unsetenv("TZ_LIST")

_ = NewTestingOsWrapper(t)
tomlPath := "./example-conf.toml"
_, err := LoadConfig(tomlPath, nil)
if err != nil {
t.Errorf("Could not read %s: %v", tomlPath, err)
}

if tzListWasSet {
os.Setenv("TZ_LIST", oldTzList)
SetupLogger()
}
}

func TestLoadConfigParser(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions example-conf.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[[zones]]
id = "NZ"
name = "NZ"

[[zones]]
id = "Australia/Sydney"
Expand All @@ -11,7 +10,7 @@ id = "Asia/Kolkata"
name = "Bangalore"

[[zones]]
id = "UTC"
id = ""
name = "UTC"

[keymaps]
Expand Down
1 change: 0 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"log"
"os"

tea "github.com/charmbracelet/bubbletea"
)
Expand Down
25 changes: 14 additions & 11 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ package main

import (
"fmt"
"os"
"math/rand"
"strings"
"testing"
)

func TestLogger(t *testing.T) {
oldDebug := os.Getenv("DEBUG")
t.Cleanup(func () {
if oldDebug != "1" {
os.Setenv("DEBUG", oldDebug)
SetupLogger()
}
})

os.Setenv("DEBUG", "1")
SetupLogger()

Expand All @@ -41,15 +47,17 @@ func TestLogger(t *testing.T) {
if !strings.Contains(lastLine, logMsg) {
t.Errorf("Missing log line in debug.log: %s", logMsg)
}

if oldDebug != "1" {
os.Setenv("DEBUG", oldDebug)
SetupLogger()
}
}

func TestNoLogger(t *testing.T) {
oldDebug, debugWasSet := os.LookupEnv("DEBUG")
t.Cleanup(func () {
if debugWasSet {
os.Setenv("DEBUG", oldDebug)
SetupLogger()
}
})

os.Unsetenv("DEBUG")
SetupLogger()

Expand All @@ -63,9 +71,4 @@ func TestNoLogger(t *testing.T) {
if strings.Contains(string(out), logMsg) {
t.Errorf("Log file debug.log contained forbidden string: %s", logMsg)
}

if debugWasSet {
os.Setenv("DEBUG", oldDebug)
SetupLogger()
}
}
63 changes: 51 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
package main

import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"runtime"
"slices"
"strconv"
"strings"
"time"

Expand All @@ -39,6 +40,29 @@ var (
hasDarkBackground = termenv.HasDarkBackground()
)

type whenValue struct {
when **int64
}

func (w whenValue) String() string {
if w.when != nil && *w.when != nil {
return strconv.FormatInt(**w.when, 10)
}
return ""
}

func (w whenValue) Set(s string) error {
if i, err := strconv.ParseInt(s, 0, 64); err == nil {
*w.when = &i
return nil
} else if t, err := time.Parse(time.RFC3339, s); err == nil {
u := t.Unix()
*w.when = &u
return nil
}
return errors.New("Could not parse integer (format: 123) or date-time (format: 2006-01-02T15:04:05+07:00)")
}

type tickMsg time.Time

// Send a tickMsg every minute, on the minute.
Expand Down Expand Up @@ -188,21 +212,22 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

func main() {
SetupLogger()
func parseMainArgs() *model {
logger.Println("Startup")

var when *int64
flag.Var(whenValue{&when}, "when", "date-time in seconds since unix epoch, or in ISO8601/RFC3339 format (disables -w)")
exitQuick := flag.Bool("q", false, "exit immediately")
showVersion := flag.Bool("v", false, "show version")
when := flag.Int64("when", 0, "time in seconds since unix epoch (disables -w)")
doSearch := flag.Bool("list", false, "[filter] list or search zones by name")
military := flag.Bool("m", false, "use 24-hour time")
watch := flag.Bool("w", false, "watch live, set time to now every minute")
flag.Parse()

if *showVersion == true {
fmt.Printf("tz %s\n", CurrentVersion)
if *showVersion {
fmt.Fprintf(os.Stdout(), "tz %s\n", CurrentVersion)
os.Exit(0)
return nil
}

if *doSearch {
Expand All @@ -211,14 +236,21 @@ func main() {
q = arg
}
results := SearchZones(strings.ToLower(q))
results.Print(os.Stdout)
if len(results) < 1 {
fmt.Fprintf(os.Stderr(), "Unknown time zone %s\n", q)
os.Exit(3)
return nil
}
results.Print(os.Stdout())
os.Exit(0)
return nil
}

config, err := LoadDefaultConfig(flag.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %s\n", err)
fmt.Fprintf(os.Stderr(), "Config error: %s\n", err)
os.Exit(2)
return nil
}

var initialModel = model{
Expand All @@ -232,15 +264,22 @@ func main() {
zoneStyle: AbbreviationZoneStyle,
}

if *when != 0 {
if when != nil {
initialModel.clock = *NewClockUnixTimestamp(*when)
}

initialModel.interactive = !*exitQuick && isatty.IsTerminal(os.Stdout.Fd())
initialModel.interactive = !*exitQuick && isatty.IsTerminal(os.Stdout().Fd())

p := tea.NewProgram(&initialModel)
return &initialModel
}

func main() {
SetupLogger()
initialModel := parseMainArgs()
p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
fmt.Fprintf(os.Stderr(), "Alas, there's been an error: %v", err)
os.Exit(1)
return
}
}
Loading