Skip to content

Quick Start

The quick start guide describes how to set up version with the most common way of creating CLIs in Go. It uses Cobra and GoReleaser.

Register the version command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
    "os"

    "github.com/spf13/cobra"

    "go.szostok.io/version/extension"
)

// NewRoot returns a root cobra.Command for the whole CLI
func NewRoot() *cobra.Command {
    cmd := &cobra.Command{
        Use:   "example",
        Short: "An example CLI built with github.com/spf13/cobra",
    }

    cmd.AddCommand(
        // 1. Register the 'version' command
        extension.NewVersionCobraCmd(
            // 2. Explicitly enable upgrade notice
            extension.WithUpgradeNotice("repo-owner", "repo-name"),
        ),
    )

    return cmd
}

func main() {
    if err := NewRoot().Execute(); err != nil {
        os.Exit(1)
    }
}

This way you get a fully working <cli> version command.

GoReleaser versioning info with -ldflags

# .goreleaser.yaml
builds:
  - # .. your settings ..

    ldflags:
      - -s -w
      - -X go.szostok.io/version.version={{.Version}}
      - -X go.szostok.io/version.buildDate={{.Date}}

Summary

As you saw, in the blink of an eye, you got a powerful version command! However, this only scratches the surfaces of possible configuration options.

You can easily customize the version command and adjust it to give you exactly what you need. For more details, see the documentation:

Back to top