Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

feat(cli): accept -V and --version alongside the version subcommand (#6321)

Anand Hegde38584e8d6fe4
diff --git a/cmd/memos/main.go b/cmd/memos/main.go
index 432d75075..4e5c418e5 100644
--- a/cmd/memos/main.go
+++ b/cmd/memos/main.go
@@ -96,6 +96,14 @@ func init() {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
+ // POSIX/BSD users reach for `-V`, GNU users for `--version`, and Go/Rust
+ // users for the `version` subcommand. Register the flag before Cobra's
+ // default so the shorthand stays `-V` rather than Cobra's `-v`, and use a
+ // bare template so all three print exactly the same string.
+ rootCmd.Flags().BoolP("version", "V", false, "print the current Memos version")
+ rootCmd.Version = version.GetCurrentVersion()
+ rootCmd.SetVersionTemplate("{{.Version}}\n")
+
rootCmd.AddCommand(versionCmd)
}
diff --git a/cmd/memos/main_test.go b/cmd/memos/main_test.go
index 734e795a5..f3d73a2b0 100644
--- a/cmd/memos/main_test.go
+++ b/cmd/memos/main_test.go
@@ -1,10 +1,13 @@
package main
import (
+ "bytes"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
+
+ "github.com/usememos/memos/internal/version"
)
func TestServerFlagsAreNotInheritedBySubcommands(t *testing.T) {
@@ -24,3 +27,33 @@ func TestPrivateWebhookAllowlistSplitsFlagAndEnvironmentForms(t *testing.T) {
viper.Set(key, []string{"hooks.internal, 10.0.0.0/8", "192.168.1.10"})
require.Equal(t, []string{"hooks.internal", "10.0.0.0/8", "192.168.1.10"}, privateWebhookAllowlist())
}
+
+func TestVersionFlagUsesPosixShorthand(t *testing.T) {
+ flag := rootCmd.Flags().Lookup("version")
+ require.NotNil(t, flag)
+ // Cobra's own version flag would claim "v"; "V" is what POSIX/BSD users expect.
+ require.Equal(t, "V", flag.Shorthand)
+ require.Nil(t, rootCmd.Flags().ShorthandLookup("v"))
+}
+
+func TestVersionFormsPrintTheSameString(t *testing.T) {
+ require.Equal(t, version.GetCurrentVersion(), rootCmd.Version)
+
+ // Cobra handles the version flag before RunE, so this never starts a server.
+ run := func(args ...string) string {
+ var out bytes.Buffer
+ rootCmd.SetOut(&out)
+ rootCmd.SetArgs(args)
+ t.Cleanup(func() {
+ rootCmd.SetOut(nil)
+ rootCmd.SetArgs(nil)
+ require.NoError(t, rootCmd.Flags().Set("version", "false"))
+ })
+ require.NoError(t, rootCmd.Execute())
+ return out.String()
+ }
+
+ expected := version.GetCurrentVersion() + "\n"
+ require.Equal(t, expected, run("--version"))
+ require.Equal(t, expected, run("-V"))
+}