diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 047726d4..8156a75a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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'\"" ] }, { diff --git a/src/.goreleaser.yml b/src/.goreleaser.yml index 60468592..b75996dc 100644 --- a/src/.goreleaser.yml +++ b/src/.goreleaser.yml @@ -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 diff --git a/src/build/version.go b/src/build/version.go new file mode 100644 index 00000000..77f67d88 --- /dev/null +++ b/src/build/version.go @@ -0,0 +1,6 @@ +package build + +var ( + Date string + Version string +) diff --git a/src/cli/cache.go b/src/cli/cache.go index 7f74e2ae..820b2c04 100644 --- a/src/cli/cache.go +++ b/src/cli/cache.go @@ -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() diff --git a/src/cli/config_export.go b/src/cli/config_export.go index 6c0bef12..e224085b 100644 --- a/src/cli/config_export.go +++ b/src/cli/config_export.go @@ -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() diff --git a/src/cli/config_export_image.go b/src/cli/config_export_image.go index dea2fe66..402289da 100644 --- a/src/cli/config_export_image.go +++ b/src/cli/config_export_image.go @@ -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() diff --git a/src/cli/config_migrate.go b/src/cli/config_migrate.go index eeb20052..b4f130dc 100644 --- a/src/cli/config_migrate.go +++ b/src/cli/config_migrate.go @@ -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() diff --git a/src/cli/config_migrate_glyphs.go b/src/cli/config_migrate_glyphs.go index 463c613f..7336b1be 100644 --- a/src/cli/config_migrate_glyphs.go +++ b/src/cli/config_migrate_glyphs.go @@ -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, }, } diff --git a/src/cli/debug.go b/src/cli/debug.go index babceb0a..f1b0ff43 100644 --- a/src/cli/debug.go +++ b/src/cli/debug.go @@ -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)) }, } diff --git a/src/cli/enable.go b/src/cli/enable.go index 62a359f4..02d66f75 100644 --- a/src/cli/enable.go +++ b/src/cli/enable.go @@ -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() diff --git a/src/cli/get.go b/src/cli/get.go index 9810cc7b..c244a22a 100644 --- a/src/cli/get.go +++ b/src/cli/get.go @@ -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() diff --git a/src/cli/init.go b/src/cli/init.go index 8fd4056b..456b8fd6 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -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() diff --git a/src/cli/notice.go b/src/cli/notice.go index 70a5bc9e..800b3dd5 100644 --- a/src/cli/notice.go +++ b/src/cli/notice.go @@ -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() diff --git a/src/cli/print.go b/src/cli/print.go index 420959f4..2a13959d 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -62,7 +62,6 @@ var printCmd = &cobra.Command{ Primary: args[0] == "primary", Cleared: cleared, NoExitCode: noExitCode, - Version: cliVersion, } eng := engine.New(flags) diff --git a/src/cli/root.go b/src/cli/root.go index 682b340f..4a426cc5 100644 --- a/src/cli/root.go +++ b/src/cli/root.go @@ -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) } diff --git a/src/cli/version.go b/src/cli/version.go index 15bda543..10ad501c 100644 --- a/src/cli/version.go +++ b/src/cli/version.go @@ -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) } diff --git a/src/main.go b/src/main.go index f9bd7aae..4206a3ad 100644 --- a/src/main.go +++ b/src/main.go @@ -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() } diff --git a/src/platform/shell.go b/src/platform/shell.go index be5f33ea..f60d1b52 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -70,7 +70,6 @@ type Flags struct { HasTransient bool PromptCount int Cleared bool - Version string TrueColor bool NoExitCode bool } diff --git a/src/segments/upgrade.go b/src/segments/upgrade.go index fa750075..e0e37b35 100644 --- a/src/segments/upgrade.go +++ b/src/segments/upgrade.go @@ -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) diff --git a/src/segments/upgrade_test.go b/src/segments/upgrade_test.go index c5ba6299..21996bd3 100644 --- a/src/segments/upgrade_test.go +++ b/src/segments/upgrade_test.go @@ -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) diff --git a/src/upgrade/notice.go b/src/upgrade/notice.go index 8ad87955..1693bc16 100644 --- a/src/upgrade/notice.go +++ b/src/upgrade/notice.go @@ -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 } diff --git a/src/upgrade/notice_test.go b/src/upgrade/notice_test.go index fe7921c0..94c9446f 100644 --- a/src/upgrade/notice_test.go +++ b/src/upgrade/notice_test.go @@ -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)