feat: add version -v

resolves #4014
This commit is contained in:
Jan De Dobbeleer 2023-07-02 20:53:50 +02:00 committed by Jan De Dobbeleer
parent 68782e9b1d
commit d005bae4f4
22 changed files with 55 additions and 57 deletions

4
.vscode/tasks.json vendored
View file

@ -23,7 +23,7 @@
"build", "build",
"-v", "-v",
"-ldflags", "-ldflags",
"\"-s -w -X 'main.Version=development-$(git --no-pager log -1 --pretty=%h-%s)' -extldflags '-static'\"" "\"-s -w -X 'github.com/jandedobbeleer/oh-my-posh/src/build.Version=development-$(git --no-pager log -1 --pretty=%h-%s)' -extldflags '-static'\""
] ]
}, },
{ {
@ -54,7 +54,7 @@
"-o", "-o",
"/home/vscode/bin/oh-my-posh", "/home/vscode/bin/oh-my-posh",
"-ldflags", "-ldflags",
"\"-s -w -X 'main.Version=development-$(git --no-pager log -1 --pretty=%h-%s)' -extldflags '-static'\"" "\"-s -w -X 'github.com/jandedobbeleer/oh-my-posh/src/build.Version=development-$(git --no-pager log -1 --pretty=%h-%s)' -extldflags '-static'\""
] ]
}, },
{ {

View file

@ -12,7 +12,7 @@ builds:
flags: flags:
- -a - -a
ldflags: ldflags:
- -s -w -X main.Version={{.Version}} -extldflags "-static" - -s -w -X github.com/jandedobbeleer/oh-my-posh/src/build.Version={{ .Version }} -X github.com/jandedobbeleer/oh-my-posh/src/build.Date={{ .Date }} -extldflags "-static"
tags: tags:
- netgo - netgo
- osusergo - osusergo
@ -27,12 +27,12 @@ builds:
- amd64 - amd64
- arm64 - arm64
- arm - arm
- 386 - "386"
ignore: ignore:
- goos: darwin - goos: darwin
goarch: 386 goarch: "386"
- goos: linux - goos: linux
goarch: 386 goarch: "386"
- goos: darwin - goos: darwin
goarch: arm goarch: arm
- goos: windows - goos: windows

6
src/build/version.go Normal file
View file

@ -0,0 +1,6 @@
package build
var (
Date string
Version string
)

View file

@ -35,9 +35,7 @@ You can do the following:
return return
} }
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{},
Version: cliVersion,
},
} }
env.Init() env.Init()
defer env.Close() defer env.Close()

View file

@ -36,8 +36,7 @@ Exports the ~/myconfig.omp.json config file to toml and prints the result to std
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Config: config, Config: config,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -49,9 +49,8 @@ Exports the config to an image file using customized output options.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Config: config, Config: config,
Shell: shell.GENERIC, Shell: shell.GENERIC,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -43,7 +43,6 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Config: config, Config: config,
Migrate: true, Migrate: true,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -36,8 +36,7 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Config: config, Config: config,
Version: cliVersion,
}, },
} }

View file

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/jandedobbeleer/oh-my-posh/src/ansi" "github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/engine" "github.com/jandedobbeleer/oh-my-posh/src/engine"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/shell" "github.com/jandedobbeleer/oh-my-posh/src/shell"
@ -22,12 +23,11 @@ var debugCmd = &cobra.Command{
startTime := time.Now() startTime := time.Now()
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Config: config, Config: config,
Debug: true, Debug: true,
PWD: pwd, PWD: pwd,
Shell: shellName, Shell: shellName,
Version: cliVersion, Plain: plain,
Plain: plain,
}, },
} }
env.Init() env.Init()
@ -47,7 +47,7 @@ var debugCmd = &cobra.Command{
Writer: writer, Writer: writer,
Plain: plain, Plain: plain,
} }
fmt.Print(eng.PrintDebug(startTime, cliVersion)) fmt.Print(eng.PrintDebug(startTime, build.Version))
}, },
} }

View file

@ -44,8 +44,7 @@ func init() { //nolint:gochecknoinits
func toggleFeature(cmd *cobra.Command, feature string, enable bool) { func toggleFeature(cmd *cobra.Command, feature string, enable bool) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Shell: shellName, Shell: shellName,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -46,8 +46,7 @@ This command is used to get the value of the following variables:
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Shell: shellName, Shell: shellName,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -55,11 +55,10 @@ func init() { //nolint:gochecknoinits
func runInit(shellName string) { func runInit(shellName string) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{
Shell: shellName, Shell: shellName,
Config: config, Config: config,
Strict: strict, Strict: strict,
Manual: manual, Manual: manual,
Version: cliVersion,
}, },
} }
env.Init() env.Init()

View file

@ -16,9 +16,7 @@ var noticeCmd = &cobra.Command{
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
env := &platform.Shell{ env := &platform.Shell{
CmdFlags: &platform.Flags{ CmdFlags: &platform.Flags{},
Version: cliVersion,
},
} }
env.Init() env.Init()
defer env.Close() defer env.Close()

View file

@ -62,7 +62,6 @@ var printCmd = &cobra.Command{
Primary: args[0] == "primary", Primary: args[0] == "primary",
Cleared: cleared, Cleared: cleared,
NoExitCode: noExitCode, NoExitCode: noExitCode,
Version: cliVersion,
} }
eng := engine.New(flags) eng := engine.New(flags)

View file

@ -5,15 +5,13 @@ import (
"os" "os"
"strings" "strings"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
config string config string
displayVersion bool displayVersion bool
// Version number of oh-my-posh
cliVersion string
) )
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
@ -29,15 +27,14 @@ on getting started, have a look at the docs at https://ohmyposh.dev`,
return return
} }
if displayVersion { if displayVersion {
fmt.Println(cliVersion) fmt.Println(build.Version)
return return
} }
_ = cmd.Help() _ = cmd.Help()
}, },
} }
func Execute(version string) { func Execute() {
cliVersion = version
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)
} }

View file

@ -3,9 +3,14 @@ package cli
import ( import (
"fmt" "fmt"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
verbose bool
)
// versionCmd represents the version command // versionCmd represents the version command
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
@ -13,10 +18,16 @@ var versionCmd = &cobra.Command{
Long: "Print the version number of oh-my-posh.", Long: "Print the version number of oh-my-posh.",
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cliVersion) if !verbose {
fmt.Println(build.Version)
return
}
fmt.Println("Version: ", build.Version)
fmt.Println("Date: ", build.Date)
}, },
} }
func init() { //nolint:gochecknoinits func init() { //nolint:gochecknoinits
versionCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "write verbose output")
RootCmd.AddCommand(versionCmd) RootCmd.AddCommand(versionCmd)
} }

View file

@ -2,10 +2,6 @@ package main
import "github.com/jandedobbeleer/oh-my-posh/src/cli" import "github.com/jandedobbeleer/oh-my-posh/src/cli"
var (
Version = "development"
)
func main() { func main() {
cli.Execute(Version) cli.Execute()
} }

View file

@ -70,7 +70,6 @@ type Flags struct {
HasTransient bool HasTransient bool
PromptCount int PromptCount int
Cleared bool Cleared bool
Version string
TrueColor bool TrueColor bool
NoExitCode bool NoExitCode bool
} }

View file

@ -3,6 +3,7 @@ package segments
import ( import (
"encoding/json" "encoding/json"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties" "github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade" "github.com/jandedobbeleer/oh-my-posh/src/upgrade"
@ -32,7 +33,7 @@ func (u *Upgrade) Init(props properties.Properties, env platform.Environment) {
} }
func (u *Upgrade) Enabled() bool { func (u *Upgrade) Enabled() bool {
current := u.env.Flags().Version current := build.Version
latest := u.cachedLatest(current) latest := u.cachedLatest(current)
if len(latest) == 0 { if len(latest) == 0 {
latest = u.checkUpdate(current) latest = u.checkUpdate(current)

View file

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/mock" "github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties" "github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade" "github.com/jandedobbeleer/oh-my-posh/src/upgrade"
@ -76,7 +76,7 @@ func TestUpgrade(t *testing.T) {
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache) cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything) cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
env.On("Flags").Return(&platform.Flags{Version: tc.CurrentVersion}) build.Version = tc.CurrentVersion
json := fmt.Sprintf(`{"tag_name":"v%s"}`, tc.LatestVersion) json := fmt.Sprintf(`{"tag_name":"v%s"}`, tc.LatestVersion)
env.On("HTTPRequest", upgrade.RELEASEURL).Return([]byte(json), tc.Error) env.On("HTTPRequest", upgrade.RELEASEURL).Return([]byte(json), tc.Error)

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
) )
@ -75,7 +76,7 @@ func Notice(env platform.Environment) (string, bool) {
oneWeek := 10080 oneWeek := 10080
env.Cache().Set(CACHEKEY, latest, oneWeek) env.Cache().Set(CACHEKEY, latest, oneWeek)
version := fmt.Sprintf("v%s", env.Flags().Version) version := fmt.Sprintf("v%s", build.Version)
if latest == version { if latest == version {
return "", false return "", false
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/mock" "github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -33,9 +34,7 @@ func TestCanUpgrade(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("Flags").Return(&platform.Flags{ build.Version = tc.CurrentVersion
Version: tc.CurrentVersion,
})
cache := &mock.MockedCache{} cache := &mock.MockedCache{}
cache.On("Get", CACHEKEY).Return("", tc.Cache) cache.On("Get", CACHEKEY).Return("", tc.Cache)
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything) cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)