Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
Expand Down
28 changes: 28 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions module/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Flags struct {
ShowTreeView bool
HideIcon bool
ShowReadableSize bool
ShowVersion bool
}

// Options struct - Contains configuration options for directory listing.
Expand Down
Loading