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
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ builds:
ldflags:
- -s -w
- -X main.version={{.Version}}
- -X main.commit={{.ShortCommit}}
- -X main.date={{.Date}}

archives:
- formats:
Expand Down
15 changes: 2 additions & 13 deletions cmd/atl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,10 @@ import (
"github.com/enthus-appdev/atl-cli/internal/iostreams"
)

// Build information set by ldflags
var (
version = "dev"
commit = "none"
date = "unknown"
)
var version = "dev"

func main() {
buildInfo := cmd.BuildInfo{
Version: version,
Commit: commit,
Date: date,
}

ios := iostreams.System()
code := cmd.Execute(ios, buildInfo)
code := cmd.Execute(ios, version)
os.Exit(code)
}
58 changes: 40 additions & 18 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"runtime/debug"

"github.com/spf13/cobra"

Expand All @@ -13,16 +14,9 @@ import (
"github.com/enthus-appdev/atl-cli/internal/iostreams"
)

// BuildInfo contains version and build information.
type BuildInfo struct {
Version string
Commit string
Date string
}

// Execute runs the root command and returns an exit code.
func Execute(ios *iostreams.IOStreams, buildInfo BuildInfo) int {
rootCmd := NewRootCmd(ios, buildInfo)
func Execute(ios *iostreams.IOStreams, version string) int {
rootCmd := NewRootCmd(ios, version)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(ios.ErrOut, "Error: %s\n", err)
return 1
Expand All @@ -31,7 +25,8 @@ func Execute(ios *iostreams.IOStreams, buildInfo BuildInfo) int {
}

// NewRootCmd creates the root command for the CLI.
func NewRootCmd(ios *iostreams.IOStreams, buildInfo BuildInfo) *cobra.Command {
func NewRootCmd(ios *iostreams.IOStreams, version string) *cobra.Command {
commit, date := vcsInfo()
cmd := &cobra.Command{
Use: "atl",
Short: "Atlassian CLI - Work with Jira and Confluence from the command line",
Expand All @@ -47,12 +42,12 @@ Environment variables:
ATL_DEBUG=1 Enable debug logging (shows API requests/responses)`,
SilenceUsage: true,
SilenceErrors: true,
Version: buildInfo.Version,
Version: version,
}

// Set custom version template
cmd.SetVersionTemplate(fmt.Sprintf("atl version %s\ncommit: %s\nbuilt: %s\n",
buildInfo.Version, buildInfo.Commit, buildInfo.Date))
cmd.SetVersionTemplate(fmt.Sprintf("atl version %s\ncommit: %s\nbuilt: %s\n",
version, commit, date))

// Set I/O streams
cmd.SetIn(ios.In)
Expand All @@ -65,25 +60,52 @@ Environment variables:
cmd.AddCommand(boardCmd.NewCmdBoard(ios))
cmd.AddCommand(confluenceCmd.NewCmdConfluence(ios))
cmd.AddCommand(configCmd.NewCmdConfig(ios))
cmd.AddCommand(newVersionCmd(ios, buildInfo))
cmd.AddCommand(newVersionCmd(ios, version, commit, date))
cmd.AddCommand(newCompletionCmd(ios))

return cmd
}

// newVersionCmd creates the version command.
func newVersionCmd(ios *iostreams.IOStreams, buildInfo BuildInfo) *cobra.Command {
func newVersionCmd(ios *iostreams.IOStreams, version, commit, date string) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintf(ios.Out, "atl version %s\n", buildInfo.Version)
fmt.Fprintf(ios.Out, "commit: %s\n", buildInfo.Commit)
fmt.Fprintf(ios.Out, "built: %s\n", buildInfo.Date)
fmt.Fprintf(ios.Out, "atl version %s\n", version)
fmt.Fprintf(ios.Out, "commit: %s\n", commit)
fmt.Fprintf(ios.Out, "built: %s\n", date)
},
}
}

func vcsInfo() (commit, date string) {
commit, date = "unknown", "unknown"
var modified bool
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
if len(s.Value) > 7 {
commit = s.Value[:7]
} else {
commit = s.Value
}
case "vcs.time":
date = s.Value
case "vcs.modified":
modified = s.Value == "true"
}
}
if modified {
commit += "-dirty"
}
return
}
Comment thread
fank marked this conversation as resolved.

// newCompletionCmd creates the completion command for shell autocompletion.
func newCompletionCmd(ios *iostreams.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Expand Down
Loading