Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support For -v Or --version Argument to dirtree #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# dirtree

This package prints the directory tree of the current directory respecting your `.gitignore` file.
This package prints the directory tree of the current directory, or the directory tree of the directory path specified as a command line argument respecting your `.gitignore` file.

## Installation

Expand All @@ -12,10 +12,10 @@ go install github.com/datumbrain/dirtree@latest

## Usage

Go to any directory and run the following command to print the directory tree
Run the command with the path of the directory as a command line argument to print the directory tree of specified directory. If no argument is specified then the directory tree of the current directory will be printed. To print the version of dirtree use -v or --version as an argument.

```bash
dirtree
dirtree [directory | -v | --version]
```

## Authors
Expand Down
30 changes: 22 additions & 8 deletions dirtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path/filepath"

"github.com/datumbrain/dirtree/internal/version"
gitignore "github.com/sabhiram/go-gitignore"
)

Expand All @@ -17,21 +18,34 @@ func main() {
fmt.Println(".")
case 2:
rootDir = os.Args[1]
_, err := os.Stat(rootDir)
if os.IsNotExist(err) {
fmt.Println("directory does not exist")
os.Exit(1)
}
if err != nil {
fmt.Println(fmt.Errorf("error: %v", err))
if rootDir == "-v" || rootDir == "--version" {
PrintVersionInfo()
os.Exit(1)
} else {
_, err := os.Stat(rootDir)
if os.IsNotExist(err) {
fmt.Println("directory does not exist")
os.Exit(1)
}
if err != nil {
fmt.Println(fmt.Errorf("error: %v", err))
os.Exit(1)
}
}
default:
fmt.Println("Usage: dirtree [directory]")
fmt.Println("Usage: dirtree [directory | -v | --version]")
os.Exit(1)
}

printTree(rootDir, "", nil)

}

func PrintVersionInfo() {
versionInfo := version.Get()
fmt.Println(versionInfo.GitVersion)
fmt.Println(versionInfo.BuildDate)
fmt.Println(versionInfo.GitCommit)
}

func shouldIgnore(path string, ignoreMatchers []*gitignore.GitIgnore) bool {
Expand Down
38 changes: 38 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package version

import (
"fmt"
"runtime"
"time"
)

var (
//x.y.z
//x - major release
//y - minor release
//z - build number
gitVersion = "v1.1.0-dirtree"
gitCommit = ""
Time = time.Now()
buildDate = Time.String()
)

type VersionInfo struct {
GitVersion string `json:"gitVersion" yaml:"gitVersion"`
GitCommit string `json:"gitCommit" yaml:"gitCommit"`
BuildDate string `json:"buildDate" yaml:"buildDate"`
GoVersion string `json:"goVersion" yaml:"goVersion"`
Compiler string `json:"compiler" yaml:"compiler"`
Program string `json:"program" yaml:"program"`
}

func Get() *VersionInfo {
return &VersionInfo{
GitVersion: gitVersion,
GitCommit: gitCommit,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Program: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}
8 changes: 8 additions & 0 deletions runscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/zsh

BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
GIT_COMMIT="$(git rev-parse HEAD)"
#VERSION="$(git describe --tags --abbrev=0 | tr -d "\n")"


go build -o bin/main -ldflags="-X 'github.com/dirtree/internal/version.buildDate=${BUILD_DATE}' -X 'github.com/dirtree/internal/version.gitCommit=${GIT_COMMIT}'" *.go