Skip to content
Merged
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
3 changes: 2 additions & 1 deletion cmd/tldr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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(
Expand Down
25 changes: 25 additions & 0 deletions exitcode/exitcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package exitcode defines the exit status codes used by tlgc.
package exitcode

const (
// Success indicates that the command completed sucessfully.
Success int = iota

// 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
)
17 changes: 17 additions & 0 deletions exitcode/exitcode_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 9 additions & 6 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -85,25 +86,26 @@ 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 0 on success and 1 on error.
// 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
}
}

if shouldAutoUpdate(cli) {
if err := a.autoUpdate(cli); err != nil {
logger.Error("failed to update cache: %v", err)
return 1
return exitcode.CacheUpdateFailure
}
}

Expand All @@ -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.
// Other failures return the corresponding [exitcode] value.
func (a *App) dispatch(cli *cmd.CLI) int {
switch {
case cli.Update:
Expand Down Expand Up @@ -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
}
}

Expand Down
8 changes: 5 additions & 3 deletions internal/app/clean.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 10 additions & 7 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 8 additions & 6 deletions internal/app/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,21 +17,22 @@ 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.
// 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 {
logger.Error(
"flag %s requires a single file",
termcolor.Sprint("bold blue", "--output"),
)
return 1
return exitcode.Default
}

failed := false
Expand All @@ -51,10 +53,10 @@ func (a *App) formatPages(cli *cmd.CLI) int {
}

if failed {
return 1
return exitcode.LintFailure
}

return 0
return exitcode.Success
}

// formatFile lints, reformats,
Expand Down
9 changes: 5 additions & 4 deletions internal/app/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions internal/app/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions internal/app/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,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 0 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
Expand Down Expand Up @@ -56,10 +57,10 @@ func (a *App) lintPages(cli *cmd.CLI) int {
}

if failed {
return 1
return exitcode.LintFailure
}

return 0
return exitcode.Success
}

// lintFile opens the page at path and runs every applicable lint rule
Expand Down
Loading
Loading