From 1e1dc0a1a3e10c50ffb1b35dd6b286852ee94a75 Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Tue, 27 Oct 2026 11:45:15 +0530 Subject: [PATCH 1/8] feat: Package exitcode --- cmd/tldr/main.go | 3 ++- exitcode/exitcode.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 exitcode/exitcode.go diff --git a/cmd/tldr/main.go b/cmd/tldr/main.go index fbded0e..7339a5e 100644 --- a/cmd/tldr/main.go +++ b/cmd/tldr/main.go @@ -4,6 +4,7 @@ import ( "os" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/app" "github.com/TheRootDaemon/tlgc/logger" ) @@ -12,7 +13,7 @@ func main() { cli, err := cmd.Parse() if err != nil { logger.Error("%v", err) - os.Exit(1) + os.Exit(exitcode.InvalidArguments) } logger.SetDefault( diff --git a/exitcode/exitcode.go b/exitcode/exitcode.go new file mode 100644 index 0000000..874a260 --- /dev/null +++ b/exitcode/exitcode.go @@ -0,0 +1,10 @@ +// Package exitcode defines the exit status codes used by tlgc. +package exitcode + +const ( + // Success indicates that the command completed sucessfully. + Success = 0 + + // InvalidArguments indicates that the command was invoked with invalid arguments. + InvalidArguments = 1 +) From a52622d9286ececde59860f5aad0e83fde044fc2 Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Tue, 27 Oct 2026 13:11:44 +0530 Subject: [PATCH 2/8] feat: Add exitcode --- exitcode/exitcode.go | 3 +++ internal/app/app.go | 11 +++++++---- internal/app/update.go | 8 +++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/exitcode/exitcode.go b/exitcode/exitcode.go index 874a260..6fab3f9 100644 --- a/exitcode/exitcode.go +++ b/exitcode/exitcode.go @@ -7,4 +7,7 @@ const ( // InvalidArguments indicates that the command was invoked with invalid arguments. InvalidArguments = 1 + + // CacheUpdateFailure indicates that updating the cache failed. + CacheUpdateFailure = 2 ) diff --git a/internal/app/app.go b/internal/app/app.go index f8ad6fc..c8dc435 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,6 +5,7 @@ import ( "os" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/config" "github.com/TheRootDaemon/tlgc/locale" "github.com/TheRootDaemon/tlgc/logger" @@ -89,7 +90,8 @@ func New(opts ...Option) *App { // Otherwise the command is delegated // to dispatch, whose result becomes the exit code. // -// It returns 0 on success and 1 on error. +// It returns [exitcode.Success] on success, +// or the corresponding error-specific exitcode on failure. func (a *App) Run(cli *cmd.CLI) int { needsConfig := !cli.GenConfig && !cli.ConfigPath && !cli.ShowVersion && !cli.ShowHelp @@ -103,7 +105,7 @@ func (a *App) Run(cli *cmd.CLI) int { if shouldAutoUpdate(cli) { if err := a.autoUpdate(cli); err != nil { logger.Error("failed to update cache: %v", err) - return 1 + return exitcode.CacheUpdateFailure } } @@ -119,7 +121,8 @@ func (a *App) Run(cli *cmd.CLI) int { // win over the bare page-lookup that handles positional arguments in cli.Page. // When no case matches, the command is a no-op and returns 0. // -// It returns 0 on success and 1 on error. +// It returns [exitcode.Success] on success, +// or the corresponding error-specific exitcode on failure. func (a *App) dispatch(cli *cmd.CLI) int { switch { case cli.Update: @@ -153,7 +156,7 @@ func (a *App) dispatch(cli *cmd.CLI) int { case len(cli.Page) > 0: return a.lookupAndRenderPage(cli) default: - return 0 + return exitcode.Success } } diff --git a/internal/app/update.go b/internal/app/update.go index 950b19d..c563f6d 100644 --- a/internal/app/update.go +++ b/internal/app/update.go @@ -4,6 +4,7 @@ import ( "context" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/internal/config" "github.com/TheRootDaemon/tlgc/internal/upstream" @@ -12,7 +13,7 @@ import ( // updateCache downloads the latest tldr-pages // for the configured languages. -// Returns 0 on success, 1 on error. +// Returns [exitcode.Success] on success, [exitcode.CacheUpdateFailure] on error. func (a *App) updateCache(cli *cmd.CLI) int { c := cache.New() languages := a.resolveLanguages(cli.Languages) @@ -20,9 +21,10 @@ func (a *App) updateCache(cli *cmd.CLI) int { if err := c.Update(context.Background(), languages, client); err != nil { logger.Error("failed to update cache: %v", err) - return 1 + return exitcode.CacheUpdateFailure } - return 0 + + return exitcode.Success } // shouldAutoUpdate reports whether a stale cache maybe updated From c1cc1c8f97cf9fc80c96cdbd1e4cdfbcd6b4843f Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Tue, 27 Oct 2026 13:39:35 +0530 Subject: [PATCH 3/8] feat: Add exitcode --- exitcode/exitcode.go | 9 ++++++--- internal/app/format.go | 7 ++++--- internal/app/lint.go | 5 +++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/exitcode/exitcode.go b/exitcode/exitcode.go index 6fab3f9..c977de8 100644 --- a/exitcode/exitcode.go +++ b/exitcode/exitcode.go @@ -3,11 +3,14 @@ package exitcode const ( // Success indicates that the command completed sucessfully. - Success = 0 + Success int = iota // InvalidArguments indicates that the command was invoked with invalid arguments. - InvalidArguments = 1 + InvalidArguments // CacheUpdateFailure indicates that updating the cache failed. - CacheUpdateFailure = 2 + CacheUpdateFailure + + // LintFailure indicates that the tldr page has lint error(s). + LintFailure ) diff --git a/internal/app/format.go b/internal/app/format.go index a7debe9..5871fc5 100644 --- a/internal/app/format.go +++ b/internal/app/format.go @@ -6,6 +6,7 @@ import ( "path/filepath" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/lint" "github.com/TheRootDaemon/tlgc/logger" "github.com/TheRootDaemon/tlgc/termcolor" @@ -16,8 +17,8 @@ import ( // back to the source file, or to a single output file // depending on the flags. // -// It returns 0 when every page formatted without lint errors and 1 -// otherwise. +// It returns [exitcode.Success] when every page formatted without lint errors +// and 1 otherwise. func (a *App) formatPages(cli *cmd.CLI) int { files, err := collectFiles(cli.Page) if err != nil { @@ -51,7 +52,7 @@ func (a *App) formatPages(cli *cmd.CLI) int { } if failed { - return 1 + return exitcode.LintFailure } return 0 diff --git a/internal/app/lint.go b/internal/app/lint.go index 4c2ca5e..04e3645 100644 --- a/internal/app/lint.go +++ b/internal/app/lint.go @@ -7,6 +7,7 @@ import ( "strconv" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/lint" "github.com/TheRootDaemon/tlgc/logger" "github.com/TheRootDaemon/tlgc/termcolor" @@ -21,7 +22,7 @@ type lintViolation struct { // lintPages runs the linter over every page reachable // from the CLI paths and reports each violation to stderr. // -// It returns 0 when every file passed without violations +// It returns [exitcode.Success] when every file passed without violations // and 1 otherwise; // a failure to collect or open any file is logged and also yields 1. func (a *App) lintPages(cli *cmd.CLI) int { @@ -56,7 +57,7 @@ func (a *App) lintPages(cli *cmd.CLI) int { } if failed { - return 1 + return exitcode.LintFailure } return 0 From 51674f0538f12deab8aaefd7232192d6ad945d3c Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Wed, 28 Oct 2026 11:09:21 +0530 Subject: [PATCH 4/8] feat: Add additional exitcodes --- exitcode/exitcode.go | 9 +++++++++ exitcode/exitcode_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 exitcode/exitcode_test.go diff --git a/exitcode/exitcode.go b/exitcode/exitcode.go index c977de8..3b1d410 100644 --- a/exitcode/exitcode.go +++ b/exitcode/exitcode.go @@ -8,9 +8,18 @@ const ( // InvalidArguments indicates that the command was invoked with invalid arguments. InvalidArguments + // Default indicates a general IO, file, or other error. + Default + // CacheUpdateFailure indicates that updating the cache failed. CacheUpdateFailure // LintFailure indicates that the tldr page has lint error(s). LintFailure + + // ConfigParseFailure indicates that the configuration file could not be parsed. + ConfigParseFailure + + // PageNotFound indicates that the requested page could not be found. + PageNotFound ) diff --git a/exitcode/exitcode_test.go b/exitcode/exitcode_test.go new file mode 100644 index 0000000..e63a135 --- /dev/null +++ b/exitcode/exitcode_test.go @@ -0,0 +1,17 @@ +package exitcode + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExitCodes(t *testing.T) { + assert.Equal(t, 0, Success) + assert.Equal(t, 1, InvalidArguments) + assert.Equal(t, 2, Default) + assert.Equal(t, 3, CacheUpdateFailure) + assert.Equal(t, 4, LintFailure) + assert.Equal(t, 5, ConfigParseFailure) + assert.Equal(t, 6, PageNotFound) +} From b8228e665b23fdd1d60dd4d83378d20253951b64 Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Wed, 28 Oct 2026 11:11:21 +0530 Subject: [PATCH 5/8] chore: Use appropriate exitocodes --- internal/app/format.go | 11 ++++++----- internal/app/format_test.go | 9 +++++---- internal/app/lint.go | 10 +++++----- internal/app/lint_test.go | 9 +++++---- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/internal/app/format.go b/internal/app/format.go index 5871fc5..7615726 100644 --- a/internal/app/format.go +++ b/internal/app/format.go @@ -17,13 +17,14 @@ import ( // back to the source file, or to a single output file // depending on the flags. // -// It returns [exitcode.Success] when every page formatted without lint errors -// and 1 otherwise. +// It returns [exitcode.Success] when every page formatted without lint errors. +// Lint violations return [exitcode.LintFailure]. +// Other failures return [exitcode.Default]. func (a *App) formatPages(cli *cmd.CLI) int { files, err := collectFiles(cli.Page) if err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } if cli.Output != "" && len(files) != 1 { @@ -31,7 +32,7 @@ func (a *App) formatPages(cli *cmd.CLI) int { "flag %s requires a single file", termcolor.Sprint("bold blue", "--output"), ) - return 1 + return exitcode.Default } failed := false @@ -55,7 +56,7 @@ func (a *App) formatPages(cli *cmd.CLI) int { return exitcode.LintFailure } - return 0 + return exitcode.Success } // formatFile lints, reformats, diff --git a/internal/app/format_test.go b/internal/app/format_test.go index 566ce4a..27408fa 100644 --- a/internal/app/format_test.go +++ b/internal/app/format_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -36,7 +37,7 @@ func TestFormatPages(t *testing.T) { path := writePage(t, t.TempDir(), "bad.md", "x tar\n> no\n") return []string{path}, &cmd.CLI{} }, - wantCode: 1, + wantCode: exitcode.LintFailure, wantStderr: []string{"bad.md"}, check: func(t *testing.T, _ string, _ *cmd.CLI, stdout string) { assert.NotEmpty(t, stdout) @@ -49,7 +50,7 @@ func TestFormatPages(t *testing.T) { path := writePage(t, t.TempDir(), "bad.md", "x tar\n> no\n") return []string{path}, &cmd.CLI{InPlace: true} }, - wantCode: 1, + wantCode: exitcode.LintFailure, check: func(t *testing.T, path string, _ *cmd.CLI, _ string) { got, err := os.ReadFile(path) require.NoError(t, err) @@ -98,7 +99,7 @@ func TestFormatPages(t *testing.T) { p2 := writePage(t, dir, "b.md", validPage) return []string{p1, p2}, &cmd.CLI{Output: filepath.Join(dir, "out.md")} }, - wantCode: 1, + wantCode: exitcode.Default, }, { name: "unparseable_content_refrains", @@ -107,7 +108,7 @@ func TestFormatPages(t *testing.T) { path := writePage(t, t.TempDir(), "tar.md", " \n") return []string{path}, &cmd.CLI{} }, - wantCode: 1, + wantCode: exitcode.LintFailure, wantStderr: []string{"refraining from formatting"}, check: func(t *testing.T, path string, _ *cmd.CLI, _ string) { got, err := os.ReadFile(path) diff --git a/internal/app/lint.go b/internal/app/lint.go index 04e3645..605cf76 100644 --- a/internal/app/lint.go +++ b/internal/app/lint.go @@ -22,14 +22,14 @@ type lintViolation struct { // lintPages runs the linter over every page reachable // from the CLI paths and reports each violation to stderr. // -// It returns [exitcode.Success] when every file passed without violations -// and 1 otherwise; -// a failure to collect or open any file is logged and also yields 1. +// It returns [exitcode.Success] when every file passed without violations. +// Lint violations return [exitcode.LintFailure]. +// Other failures return [exitcode.Default]. func (a *App) lintPages(cli *cmd.CLI) int { files, err := collectFiles(cli.Page) if err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } failed := false @@ -60,7 +60,7 @@ func (a *App) lintPages(cli *cmd.CLI) int { return exitcode.LintFailure } - return 0 + return exitcode.Success } // lintFile opens the page at path and runs every applicable lint rule diff --git a/internal/app/lint_test.go b/internal/app/lint_test.go index e34c807..366f07e 100644 --- a/internal/app/lint_test.go +++ b/internal/app/lint_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/lint" "github.com/stretchr/testify/assert" ) @@ -34,7 +35,7 @@ func TestLintPages(t *testing.T) { path := writePage(t, t.TempDir(), "bad.md", "x tar\n> no\n") return []string{path}, &cmd.CLI{} }, - wantCode: 1, + wantCode: exitcode.LintFailure, wantStderr: []string{"bad.md"}, }, { @@ -53,7 +54,7 @@ func TestLintPages(t *testing.T) { path := writePage(t, t.TempDir(), "page.txt", validPage) return []string{path}, &cmd.CLI{} }, - wantCode: 1, + wantCode: exitcode.LintFailure, wantStderr: []string{"page.txt"}, }, { @@ -62,14 +63,14 @@ func TestLintPages(t *testing.T) { path := writePage(t, t.TempDir(), "bad.md", "x tar\n> no\n") return []string{path}, &cmd.CLI{Ignore: []string{"TLDR106"}} }, - wantCode: 0, + wantCode: exitcode.Success, }, { name: "nonexistent_path_returns_one", setup: func(t *testing.T) ([]string, *cmd.CLI) { return []string{filepath.Join(t.TempDir(), "nope")}, &cmd.CLI{} }, - wantCode: 1, + wantCode: exitcode.Default, }, } From bc13db99f74ca03f2f65ef169fbfe6f52ba5794e Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Wed, 28 Oct 2026 11:13:47 +0530 Subject: [PATCH 6/8] chore: Use appropriate exitocodes --- internal/app/clean.go | 8 +++++--- internal/app/config.go | 17 ++++++++++------- internal/app/update.go | 3 ++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/internal/app/clean.go b/internal/app/clean.go index 4042638..17fddda 100644 --- a/internal/app/clean.go +++ b/internal/app/clean.go @@ -1,17 +1,19 @@ package app import ( + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/logger" ) // cleanCache interactively removes all cached entries. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) cleanCache() int { c := cache.New() if err := c.Clean(a.Stdin); err != nil { logger.Error("failed to clean cache: %v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } diff --git a/internal/app/config.go b/internal/app/config.go index 6eebd4f..992d96a 100644 --- a/internal/app/config.go +++ b/internal/app/config.go @@ -3,32 +3,35 @@ package app import ( "fmt" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/config" "github.com/TheRootDaemon/tlgc/logger" ) // genConfig prints a default configuration file to stdout. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) genConfig() int { cfg, err := config.DefaultConfig() if err != nil { logger.Error("failed to generate config: %v", err) - return 1 + return exitcode.Default } if _, err := fmt.Fprint(a.Stdout, cfg); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } // configPath prints the configuration file path to stdout. -// Returns 0 on success. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) configPath() int { if _, err := fmt.Fprintln(a.Stdout, config.ConfigPath()); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } diff --git a/internal/app/update.go b/internal/app/update.go index c563f6d..4c1632b 100644 --- a/internal/app/update.go +++ b/internal/app/update.go @@ -13,7 +13,8 @@ import ( // updateCache downloads the latest tldr-pages // for the configured languages. -// Returns [exitcode.Success] on success, [exitcode.CacheUpdateFailure] on error. +// It returns [exitcode.Success] on success, +// [exitcode.CacheUpdateFailure] on error. func (a *App) updateCache(cli *cmd.CLI) int { c := cache.New() languages := a.resolveLanguages(cli.Languages) From 5c395b104c9757ece653a57980c0bc38eae72bab Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Wed, 28 Oct 2026 11:14:15 +0530 Subject: [PATCH 7/8] chore: Use appropriate exitocodes --- internal/app/app.go | 12 ++++++------ internal/app/info.go | 16 +++++++++------- internal/app/list.go | 37 +++++++++++++++++++++---------------- internal/app/page.go | 30 ++++++++++++++++++++---------- internal/app/search.go | 10 ++++++---- 5 files changed, 62 insertions(+), 43 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index c8dc435..41fd187 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -86,19 +86,19 @@ func New(opts ...Option) *App { // (printing help, the version, or config-file paths). // // A configuration load failure is logged -// and turns into an exit code of 1. +// and turns into [exitcode.ConfigParseFailure]. // Otherwise the command is delegated // to dispatch, whose result becomes the exit code. // -// It returns [exitcode.Success] on success, -// or the corresponding error-specific exitcode on failure. +// It returns [exitcode.Success] on success. +// Other failures return the corresponding [exitcode] value. func (a *App) Run(cli *cmd.CLI) int { needsConfig := !cli.GenConfig && !cli.ConfigPath && !cli.ShowVersion && !cli.ShowHelp if needsConfig { if err := config.Initialize(); err != nil { logger.Error("failed to load config: %v", err) - return 1 + return exitcode.ConfigParseFailure } } @@ -121,8 +121,8 @@ func (a *App) Run(cli *cmd.CLI) int { // win over the bare page-lookup that handles positional arguments in cli.Page. // When no case matches, the command is a no-op and returns 0. // -// It returns [exitcode.Success] on success, -// or the corresponding error-specific exitcode on failure. +// It returns [exitcode.Success] on success. +// Other failures return the corresponding [exitcode] value. func (a *App) dispatch(cli *cmd.CLI) int { switch { case cli.Update: diff --git a/internal/app/info.go b/internal/app/info.go index 312ff6d..38643cc 100644 --- a/internal/app/info.go +++ b/internal/app/info.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/format" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/logger" @@ -12,36 +13,37 @@ import ( // cacheInfo prints detailed cache metadata including directory, // age, auto-update status, mirror, platforms, and per-language breakdown. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) cacheInfo() int { c := cache.New() info, err := c.Info() if err != nil { logger.Error("failed to get cache info: %v", err) - return 1 + return exitcode.Default } if err := a.printCacheHeader(info); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } if err := a.printAutoUpdate(info); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } if err := a.printMirror(info); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } if err := a.printLanguages(info); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } // printCacheHeader prints the cache directory and last update time. diff --git a/internal/app/list.go b/internal/app/list.go index 5a64c3f..3475abc 100644 --- a/internal/app/list.go +++ b/internal/app/list.go @@ -4,12 +4,14 @@ import ( "fmt" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/logger" ) // listPages lists pages for a specific platform from the cache. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) listPages(cli *cmd.CLI) int { c := cache.New() p := a.resolvePlatform(cli.Platform) @@ -17,72 +19,75 @@ func (a *App) listPages(cli *cmd.CLI) int { pages, err := c.ListFor(p, languages) if err != nil { logger.Error("failed to list pages: %v", err) - return 1 + return exitcode.Default } for _, page := range pages { if _, err := fmt.Fprintln(a.Stdout, page); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } } - return 0 + return exitcode.Success } // listAllPages lists all cached pages across all platforms. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) listAllPages(cli *cmd.CLI) int { c := cache.New() languages := a.resolveLanguages(cli.Languages) pages, err := c.ListAll(languages) if err != nil { logger.Error("failed to list pages: %v", err) - return 1 + return exitcode.Default } for _, page := range pages { if _, err := fmt.Fprintln(a.Stdout, page); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } } - return 0 + return exitcode.Success } // listPlatforms lists all available platforms from the cache. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) listPlatforms() int { c := cache.New() platforms, err := c.ListPlatforms() if err != nil { logger.Error("failed to list platforms: %v", err) - return 1 + return exitcode.Default } for _, p := range platforms { if _, err := fmt.Fprintln(a.Stdout, p); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } } - return 0 + return exitcode.Success } // listLanguages lists all available languages from the cache. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) listLanguages() int { c := cache.New() languages, err := c.ListLanguages() if err != nil { logger.Error("failed to list languages: %v", err) - return 1 + return exitcode.Default } for _, l := range languages { if _, err := fmt.Fprintln(a.Stdout, l); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } } - return 0 + return exitcode.Success } diff --git a/internal/app/page.go b/internal/app/page.go index 25ce076..d1430ab 100644 --- a/internal/app/page.go +++ b/internal/app/page.go @@ -8,6 +8,7 @@ import ( "github.com/TheRootDaemon/tlgc/browser" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/internal/config" "github.com/TheRootDaemon/tlgc/internal/render" @@ -17,7 +18,8 @@ import ( ) // lookupAndRenderPage finds a page by name and renders it to the terminal. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return the corresponding [exitcode] value. func (a *App) lookupAndRenderPage(cli *cmd.CLI) int { p := a.resolvePlatform(cli.Platform) langs := a.resolveLanguages(cli.Languages) @@ -27,7 +29,7 @@ func (a *App) lookupAndRenderPage(cli *cmd.CLI) int { results, err := c.Find(query, p, langs) if err != nil { logger.Error("failed to find page: %v", err) - return 1 + return exitcode.Default } pagePath, renderPlatform, err := a.selectPage( @@ -37,7 +39,10 @@ func (a *App) lookupAndRenderPage(cli *cmd.CLI) int { ) if err != nil { logger.Error("%v", err) - return 1 + if strings.Contains(err.Error(), "page not found") { + return exitcode.PageNotFound + } + return exitcode.Default } page, err := loadPage(pagePath) @@ -55,7 +60,8 @@ func (a *App) lookupAndRenderPage(cli *cmd.CLI) int { // browsePage looks up a page and opens it in the default web browser. // It does not render the page content to the terminal. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return the corresponding [exitcode] value. func (a *App) browsePage(cli *cmd.CLI) int { p := a.resolvePlatform(cli.Platform) langs := a.resolveLanguages(cli.Languages) @@ -65,13 +71,16 @@ func (a *App) browsePage(cli *cmd.CLI) int { results, err := c.Find(query, p, langs) if err != nil { logger.Error("failed to find page: %v", err) - return 1 + return exitcode.Default } pagePath, _, err := a.selectPage(results, query, p) if err != nil { logger.Error("%v", err) - return 1 + if strings.Contains(err.Error(), "page not found") { + return exitcode.PageNotFound + } + return exitcode.Default } url := render.BuildViewURL(pagePath) @@ -90,19 +99,20 @@ func (a *App) browsePage(cli *cmd.CLI) int { // renderLocalFile reads a local tldr markdown file, validates it, // and renders it to the terminal. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) renderLocalFile(cli *cmd.CLI) int { page, err := loadPage(cli.Render) if err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } if err := a.renderPage(cli, "", page); err != nil { logger.Error("failed to render: %v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } // renderPage renders a parsed TLDR page diff --git a/internal/app/search.go b/internal/app/search.go index e2dacef..5ad3f04 100644 --- a/internal/app/search.go +++ b/internal/app/search.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/TheRootDaemon/tlgc/cmd" + "github.com/TheRootDaemon/tlgc/exitcode" "github.com/TheRootDaemon/tlgc/internal/cache" "github.com/TheRootDaemon/tlgc/logger" "github.com/TheRootDaemon/tlgc/platform" @@ -12,7 +13,8 @@ import ( ) // searchPages searches cached pages for the given query. -// Returns 0 on success, 1 on error. +// It returns [exitcode.Success] on success. +// Other failures return [exitcode.Default]. func (a *App) searchPages(cli *cmd.CLI) int { c := cache.New() p := cli.Platform @@ -24,15 +26,15 @@ func (a *App) searchPages(cli *cmd.CLI) int { results, err := c.Search(cli.Search, p, languages) if err != nil { logger.Error("search failed: %v", err) - return 1 + return exitcode.Default } if err := a.printSearchResults(results, cli.Search); err != nil { logger.Error("%v", err) - return 1 + return exitcode.Default } - return 0 + return exitcode.Success } // printSearchResults renders the search results as a table, From 08c6da6d86c536050b081a4c918eed0747f90f83 Mon Sep 17 00:00:00 2001 From: TheRootDaemon Date: Wed, 28 Oct 2026 11:55:08 +0530 Subject: [PATCH 8/8] chore: Update man --- tldr.1 | 23 +++++++++++++++++++---- tldr.md | 21 ++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/tldr.1 b/tldr.1 index dec36ab..4a515a4 100644 --- a/tldr.1 +++ b/tldr.1 @@ -5,7 +5,7 @@ .nh .ad l .\" Begin generated content: -.TH "TLGC" "1" "2026\-09\-04" +.TH "TLGC" "1" "2026\-10\-28" .PP .SH NAME .PP @@ -203,13 +203,28 @@ both the full locale and the bare language code are tried.\& When set (any value), disables colored output.\& Equivalent to \fI\-\-color never\fR.\& .PP -.SH EXIT STATUS +.SH EXIT CODES .PP 0 -Success.\& +(Success) Command completed successfully.\& .PP 1 -An error occurred (invalid arguments, cache failure, config error, page not found, etc.\&).\& +(InvalidArguments) Invalid command\-line arguments.\& +.PP +2 +(Default) An unspecified error occurred usually IO, file and other errors.\& +.PP +3 +(CacheUpdateFailure) Failed to update the cache.\& +.PP +4 +(LintFailure) Lint or formatting violations were found.\& +.PP +5 +(ConfigParseFailure) Failed to parse the configuration file.\& +.PP +6 +(PageNotFound) The requested tldr page was not found.\& .PP .SH EXAMPLES .PP diff --git a/tldr.md b/tldr.md index 8483fc0..3aa920d 100644 --- a/tldr.md +++ b/tldr.md @@ -191,13 +191,28 @@ _NO_COLOR_ When set (any value), disables colored output. Equivalent to _--color never_. -# EXIT STATUS +# EXIT CODES 0 -Success. +(Success) Command completed successfully. 1 -An error occurred (invalid arguments, cache failure, config error, page not found, etc.). +(InvalidArguments) Invalid command-line arguments. + +2 +(Default) An unspecified error occurred usually IO, file and other errors. + +3 +(CacheUpdateFailure) Failed to update the cache. + +4 +(LintFailure) Lint or formatting violations were found. + +5 +(ConfigParseFailure) Failed to parse the configuration file. + +6 +(PageNotFound) The requested tldr page was not found. # EXAMPLES