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",
"-v",
"-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",
"/home/vscode/bin/oh-my-posh",
"-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:
- -a
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:
- netgo
- osusergo
@ -27,12 +27,12 @@ builds:
- amd64
- arm64
- arm
- 386
- "386"
ignore:
- goos: darwin
goarch: 386
goarch: "386"
- goos: linux
goarch: 386
goarch: "386"
- goos: darwin
goarch: arm
- 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
}
env := &platform.Shell{
CmdFlags: &platform.Flags{
Version: cliVersion,
},
CmdFlags: &platform.Flags{},
}
env.Init()
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) {
env := &platform.Shell{
CmdFlags: &platform.Flags{
Config: config,
Version: cliVersion,
Config: config,
},
}
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) {
env := &platform.Shell{
CmdFlags: &platform.Flags{
Config: config,
Shell: shell.GENERIC,
Version: cliVersion,
Config: config,
Shell: shell.GENERIC,
},
}
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{
Config: config,
Migrate: true,
Version: cliVersion,
},
}
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) {
env := &platform.Shell{
CmdFlags: &platform.Flags{
Config: config,
Version: cliVersion,
Config: config,
},
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -3,9 +3,14 @@ package cli
import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/spf13/cobra"
)
var (
verbose bool
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
@ -13,10 +18,16 @@ var versionCmd = &cobra.Command{
Long: "Print the version number of oh-my-posh.",
Args: cobra.NoArgs,
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
versionCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "write verbose output")
RootCmd.AddCommand(versionCmd)
}

View file

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

View file

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

View file

@ -3,6 +3,7 @@ package segments
import (
"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/properties"
"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 {
current := u.env.Flags().Version
current := build.Version
latest := u.cachedLatest(current)
if len(latest) == 0 {
latest = u.checkUpdate(current)

View file

@ -5,8 +5,8 @@ import (
"fmt"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"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/upgrade"
@ -76,7 +76,7 @@ func TestUpgrade(t *testing.T) {
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
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)
env.On("HTTPRequest", upgrade.RELEASEURL).Return([]byte(json), tc.Error)

View file

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

View file

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