Skip to content

dan-sherwin/go_cli_template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go_cli_template — Go template for CLI applications

Template repository URL: http://scm.dev.dsherwin.net/dsherwin/go_cli_template

This repository is a reusable template for building Go CLI applications. It provides:

  • CLI commands via Kong
  • Application logging through github.com/dan-sherwin/go-applog
  • Runtime logging/settings integration through github.com/dan-sherwin/go-app-runtime when settings are enabled
  • Optional settings persistence via app_settings
  • TeamCity CI setup
  • A bootstrap tool to safely rename the module and app for a new project

Quick start (using the bootstrap tool)

First and foremost, you need to run go mod tidy to initialize the module.

Run the bootstrap helper to safely rename the module and runtime app name.

Examples:

  • Create a new app named your_app (sets both module path and APPNAME to your_app): go run ./dev/bootstrap your_app
  • Create a new app but set an explicit module path: go run ./dev/bootstrap your_app -module scm.dev.dsherwin.net/yourteam/your_app
  • Create a new app without DB-backed settings support: go run ./dev/bootstrap your_app -settings=false
  • Preview planned changes without modifying files: go run ./dev/bootstrap your_app -dry-run

What the bootstrap tool does:

  • Updates go.mod module path and rewrites Go imports that reference the old module path (AST-safe).
  • Sets const APPNAME in cmd/app/consts/consts.go.
  • Lets you override the Go module path independently from the runtime app name.
  • Optionally removes the DB-backed settings integration from the generated app.
  • Updates .teamcity/settings.kts:
    • param("app.name", "...")
    • the project description ("CI for ...")
    • ldflags import paths to match the new module
  • Generates a new README.md for your project.
  • Runs go mod tidy

After bootstrapping:

  1. Open the project in GoLand; it will re-index automatically.
  2. Build and test: go build ./... go test -race ./...

Building and running locally

  • macOS/Linux (dev): go build -o ./dist/cli_app ./cmd ./dist/cli_app --help

  • Linux production build (as in TeamCity): GOOS=linux GOARCH=amd64 CGO_ENABLED=0
    go build -ldflags "-X 'scm.dev.dsherwin.net/dsherwin/go_cli_template/cmd/app/consts.Version=0.1.0'" -o ./dist/cli_app ./cmd

The binary exposes a CLI with commands registered under cmd/app/commands. See internal/foo for examples of adding a command and a setting.

Settings

  • When enabled, persistent settings are stored under the user's config directory in an app-specific folder.
  • The CLI does not change the working directory on startup, so relative paths continue to resolve from the shell's current directory.

Logging

  • Use applog.Info, applog.Warn, applog.Error, applog.Debug, and applog.Debug2 through applog.Debug5 in CLI code.
  • go-applog owns logger setup; go-app-runtime owns the log_level setting when settings support is enabled.
  • Standard log keys include app, version, commit, buildDate, pid, user, and error.
  • Set log level with --logging.level or saved settings when settings support is enabled.

Versioning

  • Version is injected via -ldflags into cmd/app/consts.Version

TeamCity CI/CD

  • .teamcity/settings.kts contains a Build configuration:
    • go mod tidy, go vet, go test -race, and a Linux/amd64 build with ldflags

Template notes

  • Keep runtime app name centralized in cmd/app/consts/consts.go (APPNAME), default is "cli_app".
  • An example package exists at internal/foo to demonstrate settings and commands integration.
  • Prefer the bootstrap tool to rename this template rather than search/replace.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors