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
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:
- Open the project in GoLand; it will re-index automatically.
- Build and test: go build ./... go test -race ./...
-
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.
- 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.
- Use
applog.Info,applog.Warn,applog.Error,applog.Debug, andapplog.Debug2throughapplog.Debug5in CLI code. go-applogowns logger setup;go-app-runtimeowns thelog_levelsetting when settings support is enabled.- Standard log keys include app, version, commit, buildDate, pid, user, and error.
- Set log level with
--logging.levelor saved settings when settings support is enabled.
- Version is injected via -ldflags into cmd/app/consts.Version
- .teamcity/settings.kts contains a Build configuration:
- go mod tidy, go vet, go test -race, and a Linux/amd64 build with ldflags
- 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.