diff --git a/README.md b/README.md index c0e18be..5a952c5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/dirtree.go b/dirtree.go index e72054c..2d2bf63 100644 --- a/dirtree.go +++ b/dirtree.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" + "github.com/datumbrain/dirtree/internal/version" gitignore "github.com/sabhiram/go-gitignore" ) @@ -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 { diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..312b9ba --- /dev/null +++ b/internal/version/version.go @@ -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), + } +} diff --git a/runscript.sh b/runscript.sh new file mode 100755 index 0000000..1967b71 --- /dev/null +++ b/runscript.sh @@ -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