diff --git a/README.md b/README.md index ff38373..36bbbb4 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ treels [Flags] [Path] - `-t, --tree`: Tree view of the directory - `--no-icons`: Disable icons - `-r, --readable`: Show human-readable size for each file and directory +- `-v, --version`: Show treels version ## 📋 Example diff --git a/cmd/flag.go b/cmd/flag.go index a79cee9..4b8960e 100644 --- a/cmd/flag.go +++ b/cmd/flag.go @@ -12,4 +12,5 @@ func FlagDefinition(cmd *cobra.Command, flags *module.Flags) { cmd.PersistentFlags().BoolVarP(&flags.ShowTreeView, "tree", "t", false, "Tree view of the directory") cmd.PersistentFlags().BoolVar(&flags.HideIcon, "no-icons", false, "Disable icons") cmd.PersistentFlags().BoolVarP(&flags.ShowReadableSize, "readable", "r", false, "Show human-readable size for each file and directory") + cmd.PersistentFlags().BoolVarP(&flags.ShowVersion, "version", "v", false, "Show treels version") } diff --git a/cmd/root.go b/cmd/root.go index 4b771eb..bf38e16 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,8 @@ import ( "github.com/spf13/cobra" ) +const version = "v1.3.0" + // Execute func - runs the root command. func Execute() { if err := newRootCmd().Execute(); err != nil { @@ -25,7 +27,12 @@ func newRootCmd() *cobra.Command { Use: "treels [path]", Short: "⚡ Treels is a CLI tool crafted in Go, merging tree and ls commands with intuitive merging and beautification features.", Args: cobra.MaximumNArgs(1), - RunE: func(_ *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) error { + if flag.ShowVersion { + _, err := fmt.Fprintf(cmd.OutOrStdout(), "treels %s\n", version) + return err + } + options := module.Options{Flags: flag} if len(args) == 1 { options.Directory = args[0] diff --git a/cmd/root_test.go b/cmd/root_test.go index 73b5635..a74ecc7 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "io" "os" "path/filepath" @@ -58,6 +59,33 @@ func TestRootCmd_IconFlagsRemoved(t *testing.T) { } } +func TestRootCmd_VersionFlag(t *testing.T) { + tests := []struct { + name string + args []string + }{ + {name: "long flag", args: []string{"--version"}}, + {name: "short flag", args: []string{"-v"}}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var output bytes.Buffer + cmd := newRootCmd() + cmd.SetOut(&output) + cmd.SetArgs(tt.args) + + if err := cmd.Execute(); err != nil { + t.Fatalf("Execute() error = %v, want nil", err) + } + + if got := output.String(); got != "treels v1.3.0\n" { + t.Fatalf("Execute() output = %q, want version output", got) + } + }) + } +} + func TestRootCmd_ValidPathWithFlags(t *testing.T) { dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main"), 0o644); err != nil { diff --git a/module/types.go b/module/types.go index 41dacea..c98c064 100644 --- a/module/types.go +++ b/module/types.go @@ -7,6 +7,7 @@ type Flags struct { ShowTreeView bool HideIcon bool ShowReadableSize bool + ShowVersion bool } // Options struct - Contains configuration options for directory listing.