Skip to content

Commit

Permalink
feat: bootstrap cli template
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Sep 24, 2023
1 parent 40a1786 commit 7b8e099
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cobra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
author: Emmanuel Gautier <emmanuel@cerberauth.com>
license: MIT License
22 changes: 22 additions & 0 deletions .docker/Dockerfile-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.21-bullseye AS builder

WORKDIR /go/src/github.com/emmanuelgautier/go-cli-template

COPY go.mod go.mod
COPY go.sum go.sum

ENV CGO_ENABLED 0
ENV GO111MODULE on

RUN go mod download

COPY . .

RUN go build -o /usr/bin/go-cli-template .

FROM gcr.io/distroless/static-debian11:nonroot AS runner

COPY --from=builder --chown=nonroot:nonroot /usr/bin/go-cli-template /usr/bin/go-cli-template

ENTRYPOINT ["go-cli-template"]
CMD ["go-cli-template"]
6 changes: 6 additions & 0 deletions .docker/Dockerfile-goreleaser
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM gcr.io/distroless/static-debian11:nonroot

COPY --chown=nonroot:nonroot go-cli-template /usr/bin/go-cli-template

ENTRYPOINT ["go-cli-template"]
CMD ["go-cli-template"]
84 changes: 84 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: CI

on:
push:
tags:
- "v*.*.*"
branches:
- main
pull_request:
branches:
- main

env:
GO_VERSION: '1.21'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Build
run: go build -v ./...

- name: Test
run: go test -race -coverprofile=coverage.out -covermode=atomic ./...

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3

publish:
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/')

permissions:
contents: write
packages: write
pull-requests: write

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- run: git fetch --force --tags

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# https://github.com/goreleaser/goreleaser/issues/1715#issuecomment-667002748
- name: Install Snapcraft
run: |
sudo snap install --classic snapcraft
mkdir -p $HOME/.cache/snapcraft/download
mkdir -p $HOME/.cache/snapcraft/stage-packages
- uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

dist/
82 changes: 82 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
before:
hooks:
- go mod tidy
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

checksum:
name_template: "checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next"

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"

# brews:
# - name: "{{ .ProjectName }}"
# description: "go-cli-template"
# license: "MIT"
# repository:
# owner: emmanuelgautier
# name: "{{ .ProjectName }}"
# goarm: 6
# test: |
# system "#{bin}/{{ .ProjectName }} help"
# homepage: https://github.com/emmanuelgautier/go-cli-template
# commit_author:
# name: emmanuelgautier
# email: youremail@domain.tld

# nfpms:
# - package_name: "{{ .ProjectName }}"
# vendor: Emmanuel Gautier
# homepage: https://github.com/emmanuelgautier/go-cli-template
# maintainer: Emmanuel Gautier <youremail@domain.tld>
# description: ""
# license: "MIT"
# formats:
# - apk
# - deb
# - rpm
# - termux.deb
# - archlinux

snapcrafts:
- title: go-cli-template
publish: true
summary: "go-cli-template"
description: "go-cli-template"
license: MIT

dockers:
- image_templates:
- "ghcr.io/emmanuelgautier/go-cli-template:{{ .Tag }}"
- "ghcr.io/emmanuelgautier/go-cli-template:v{{ .Major }}"
- "ghcr.io/emmanuelgautier/go-cli-template:v{{ .Major }}.{{ .Minor }}"
- "ghcr.io/emmanuelgautier/go-cli-template:latest"
dockerfile: .docker/Dockerfile-goreleaser
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"recommendations": [
"golang.go",
"streetsidesoftware.code-spell-checker",
"redhat.vscode-yaml"
]
}
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Go CLI Template

This is a simple Go CLI (Command Line Interface) template that you can use as a starting point for building your own command-line applications in Go. It provides a basic structure and some common features to help you get started quickly.

## Features

- Command-line argument parsing using the [cobra](https://pkg.go.dev/github.com/spf13/cobra) package.
- Simple subcommand support.
- Github Actions workflow
- GoReleaser preconfigured for Docker build, Github Release binaries (multi-archi) builds and snapcraft publishing.

## Usage

1. Clone or download this repository:

```bash
git clone https://github.com/emmanuelgautier/go-cli-template.git
cd go-cli-template
```

2. Build the CLI tool:

```bash
go build -o go-cli-template
```

3. Run the CLI tool with the `--help` flag to see the available commands:

```bash
./go-cli-template --help
```

You should see output similar to the following:

```
A simple Go CLI template.
Usage:
go-cli-template [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
hello Prints a friendly greeting
help Help about any command
Flags:
-h, --help help for go-cli-template
Use "go-cli-template [command] --help" for more information about a command.
```

4. Run the `hello` subcommand:

```bash
./go-cli-template hello --name YourName
```

Replace `YourName` with your actual name. This command will print a greeting.

## License

This Go CLI template is open-source and available under the MIT License. Feel free to use it as a starting point for your own CLI applications. Contributions and improvements are welcome!

## Author

[Emmanuel Gautier](https://www.emmanuelgautier.com/)
46 changes: 46 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

func NewRootCmd() (cmd *cobra.Command) {
var rootCmd = &cobra.Command{
Use: "go-cli-template",
Short: "A simple Go CLI template.",
}

var helloCmd = &cobra.Command{
Use: "hello",
Short: "Prints a friendly greeting",
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
if name == "" {
fmt.Println("Hello, World!")
} else {
fmt.Printf("Hello, %s!\n", name)
}
},
}

helloCmd.Flags().StringP("name", "n", "", "Specify a name")

rootCmd.AddCommand(helloCmd)

return rootCmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
c := NewRootCmd()

c.AddCommand()

if err := c.Execute(); err != nil {
os.Exit(1)
}
}
Binary file added go-cli-template
Binary file not shown.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/emmanuelgautier/go-cli-template

go 1.20

require github.com/spf13/cobra v1.7.0

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/emmanuelgautier/go-cli-template/cmd"

func main() {
cmd.Execute()
}
5 changes: 5 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"github>emmanuelgautier/renovate-config"
]
}

0 comments on commit 7b8e099

Please sign in to comment.