From cf07dd4fba4a76b659fa206bea0b4b3d115c8062 Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 15:06:09 +0300 Subject: [PATCH 1/7] go: use os.ReadDir in place of ioutil.ReadDir (deprecated) --- cache.go | 4 +--- notesium.go | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index a2b471e..d383aeb 100644 --- a/cache.go +++ b/cache.go @@ -3,7 +3,6 @@ package main import ( "bufio" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -42,8 +41,7 @@ func populateCache(dir string) { } noteCache = make(map[string]*Note) - - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { log.Fatalf("could not read directory: %s\n", err) } diff --git a/notesium.go b/notesium.go index 770a932..0362a96 100644 --- a/notesium.go +++ b/notesium.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "io/fs" - "io/ioutil" "log" "net" "net/http" @@ -241,7 +240,7 @@ func notesiumLinks(dir string, opts linksOptions, w io.Writer) { } func notesiumLines(dir string, opts linesOptions, w io.Writer) { - files, err := ioutil.ReadDir(dir) + files, err := os.ReadDir(dir) if err != nil { log.Fatalf("Could not read directory: %s\n", err) } From c1db3d608582777345d7374ee08b443be49f6fb3 Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 15:11:08 +0300 Subject: [PATCH 2/7] go: rename vars to remove underscores (idiomatic) --- sort.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sort.go b/sort.go index 99443a1..7164488 100644 --- a/sort.go +++ b/sort.go @@ -42,8 +42,8 @@ func getSortedNotes(sortBy string) []*Note { func sortLinesByField(lines []string, separator string, fieldIndex int) { sort.Slice(lines, func(i, j int) bool { - sub_i := strings.SplitN(lines[i], separator, fieldIndex+1)[fieldIndex] - sub_j := strings.SplitN(lines[j], separator, fieldIndex+1)[fieldIndex] - return sub_i < sub_j + subI := strings.SplitN(lines[i], separator, fieldIndex+1)[fieldIndex] + subJ := strings.SplitN(lines[j], separator, fieldIndex+1)[fieldIndex] + return subI < subJ }) } From 73bb23ff7f1bf8def952a6dabd62371ba557cef7 Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 15:25:45 +0300 Subject: [PATCH 3/7] go: rename vars for initialism (idiomatic) --- notesium.go | 2 +- runtime.go | 4 ++-- tests/version.bats | 2 +- version.go | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/notesium.go b/notesium.go index 0362a96..ca2e39e 100644 --- a/notesium.go +++ b/notesium.go @@ -565,7 +565,7 @@ func notesiumVersion(opts versionOptions, w io.Writer) { fmt.Fprintf(w, "platform:%s/%s\n", runtime.GOOS, runtime.GOARCH) fmt.Fprintf(w, "latest.version:%s\n", latest.Version) fmt.Fprintf(w, "latest.published:%s\n", latest.PublishedAt) - fmt.Fprintf(w, "latest.release:%s\n", latest.HtmlUrl) + fmt.Fprintf(w, "latest.release:%s\n", latest.HTMLURL) } return } diff --git a/runtime.go b/runtime.go index 586ea0b..4cc7fea 100644 --- a/runtime.go +++ b/runtime.go @@ -16,7 +16,7 @@ type BuildInfo struct { GitVersion string `json:"gitversion"` Buildtime string `json:"buildtime"` GoVersion string `json:"goversion"` - LatestReleaseUrl string `json:"latest-release-url"` + LatestReleaseURL string `json:"latest-release-url"` } type MemoryInfo struct { @@ -55,7 +55,7 @@ func GetRuntimeInfo(dir string, webOpts webOptions) RuntimeResponse { GitVersion: gitversion, Buildtime: buildtime, GoVersion: runtime.Version(), - LatestReleaseUrl: latestReleaseUrl, + LatestReleaseURL: latestReleaseURL, }, Memory: MemoryInfo{ MemoryAlloc: bytesToHumanReadable(memStats.Alloc), diff --git a/tests/version.bats b/tests/version.bats index d5f2cbf..5b28207 100644 --- a/tests/version.bats +++ b/tests/version.bats @@ -8,7 +8,7 @@ _gobuild() { go build -o /tmp/notesium-test-version/$gitversion -ldflags " -X main.gitversion=$gitversion \ -X main.buildtime=2024-01-02T01:02:03Z \ - -X main.latestReleaseUrl=http://127.0.0.1:8882/latest.json" + -X main.latestReleaseURL=http://127.0.0.1:8882/latest.json" } _mock_latest_release() { diff --git a/version.go b/version.go index 81be9c7..f4827ed 100644 --- a/version.go +++ b/version.go @@ -12,12 +12,12 @@ import ( // 1:semver (2:major 3:minor 4:patch 5:prerelease 6:prereleaseV) 7:commits 8:hash 9:dirty var gitVersionRegex = regexp.MustCompile(`^v((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-(alpha|beta|rc)(?:\.(0|[1-9]\d*))?)?)-(0|[1-9]\d*)-g([0-9a-fA-F]+)(-dirty)?$`) -var latestReleaseUrl = "https://api.github.com/repos/alonswartz/notesium/releases/latest" +var latestReleaseURL = "https://api.github.com/repos/alonswartz/notesium/releases/latest" type releaseInfo struct { Version string `json:"-"` TagName string `json:"tag_name"` - HtmlUrl string `json:"html_url"` + HTMLURL string `json:"html_url"` PublishedAt string `json:"published_at"` } @@ -43,7 +43,7 @@ func getVersion(gitVersion string) string { func getLatestReleaseInfo() (releaseInfo, error) { var release releaseInfo - req, err := http.NewRequest("GET", latestReleaseUrl, nil) + req, err := http.NewRequest("GET", latestReleaseURL, nil) if err != nil { return release, fmt.Errorf("error creating request: %s", err) } @@ -66,7 +66,7 @@ func getLatestReleaseInfo() (releaseInfo, error) { return release, fmt.Errorf("error decoding response: %s", err) } - if release.TagName == "" || release.HtmlUrl == "" || release.PublishedAt == "" { + if release.TagName == "" || release.HTMLURL == "" || release.PublishedAt == "" { return release, fmt.Errorf("missing required field in response") } From a0b9974f9bf4bafe7be97d0b81b497fe59a8c177 Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 15:51:14 +0300 Subject: [PATCH 4/7] go: replace unused param with _ in apiRuntime --- api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api.go b/api.go index 160c532..c117ce1 100644 --- a/api.go +++ b/api.go @@ -292,7 +292,7 @@ func apiNote(dir string, w http.ResponseWriter, r *http.Request, readOnly bool) w.Write(jsonResponse) } -func apiRuntime(dir string, w http.ResponseWriter, r *http.Request, opts webOptions) { +func apiRuntime(dir string, w http.ResponseWriter, _ *http.Request, opts webOptions) { runtimeResponse := GetRuntimeInfo(dir, opts) jsonResponse, err := json.Marshal(runtimeResponse) if err != nil { From aaadb6fc5ab6eccb55db7d43bed8e9af6a7fdff6 Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 15:59:57 +0300 Subject: [PATCH 5/7] go: replace empty interface with any --- api.go | 2 +- options.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api.go b/api.go index c117ce1..36e4287 100644 --- a/api.go +++ b/api.go @@ -249,7 +249,7 @@ func apiNote(dir string, w http.ResponseWriter, r *http.Request, readOnly bool) noteCache = nil populateCache(dir) - response := map[string]interface{}{ + response := map[string]any{ "Filename": filename, "Deleted": true, } diff --git a/options.go b/options.go index 5edca00..f6979dd 100644 --- a/options.go +++ b/options.go @@ -58,7 +58,7 @@ Environment: type Command struct { Name string - Options interface{} + Options any } type newOptions struct { From b8c93e1a092614995bc0bf6e0b824c5ee9e4e8ea Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 16:05:01 +0300 Subject: [PATCH 6/7] go: replace HasPrefix/TrimPrefix with CutPrefix --- filter.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/filter.go b/filter.go index 636ab6e..52f0f50 100644 --- a/filter.go +++ b/filter.go @@ -64,9 +64,8 @@ func evaluateFilterQuery(query string, input string) (bool, error) { } } matches = matches && orMatch - } else if strings.HasPrefix(token, "!") { + } else if term, ok := strings.CutPrefix(token, "!"); ok { // NOT logic - term := strings.TrimPrefix(token, "!") if strings.Contains(input, term) { matches = false break From 54ceee850fdc615f9ad560943af964c2d538739b Mon Sep 17 00:00:00 2001 From: Alon Swartz Date: Thu, 24 Jul 2025 16:13:38 +0300 Subject: [PATCH 7/7] go: simplify option parsing with tagged switch --- options.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/options.go b/options.go index f6979dd..42715e5 100644 --- a/options.go +++ b/options.go @@ -272,10 +272,10 @@ func parseOptions(args []string) (Command, error) { case "stats": opts := statsOptions{} for _, opt := range args[1:] { - switch { - case opt == "--color": + switch opt { + case "--color": opts.color = defaultColor() - case opt == "--table": + case "--table": opts.table = true default: return Command{}, fmt.Errorf("unrecognized option: %s", opt) @@ -356,10 +356,10 @@ func parseOptions(args []string) (Command, error) { case "version": opts := versionOptions{} for _, opt := range args[1:] { - switch { - case opt == "--verbose": + switch opt { + case "--verbose": opts.verbose = true - case opt == "--check": + case "--check": opts.check = true default: return Command{}, fmt.Errorf("unrecognized option: %s", opt)