From ec42944787c3627bb494fa7ea17437e9de302865 Mon Sep 17 00:00:00 2001 From: daniyalumer Date: Fri, 23 Aug 2024 12:41:20 +0500 Subject: [PATCH 1/4] Updated The README.md To Include Proper Usage Documentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c0e18be..d090b4b 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. ```bash -dirtree +dirtree [directory] ``` ## Authors From 0eb7d04fcbc1e1eb44f66eb13b9dd9fbda3d09b9 Mon Sep 17 00:00:00 2001 From: daniyalumer Date: Fri, 23 Aug 2024 12:50:01 +0500 Subject: [PATCH 2/4] Changed The Usage Example Command +Used tree --help to view the usage example. +It uses tree [directory ...] so I updated our usage example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d090b4b..38117be 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ go install github.com/datumbrain/dirtree@latest 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. ```bash -dirtree [directory] +dirtree [directory ...] ``` ## Authors From 759d74653040cc3d81766217fcf103063e32ff61 Mon Sep 17 00:00:00 2001 From: daniyalumer Date: Tue, 27 Aug 2024 14:24:25 +0500 Subject: [PATCH 3/4] Added Support For -v Or --version Argument to dirtree +Created a local module version.go to return the version information +Version information includes Build Date and the hash of current git commit +Created a bash script to build the executable with -ldflags --- README.md | 4 ++-- dirtree.go | 30 +++++++++++++++++++++-------- internal/version/version.go | 38 +++++++++++++++++++++++++++++++++++++ runscript.sh | 9 +++++++++ 4 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 internal/version/version.go create mode 100755 runscript.sh diff --git a/README.md b/README.md index 38117be..5a952c5 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ go install github.com/datumbrain/dirtree@latest ## Usage -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. +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 [directory ...] +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..d1fda70 --- /dev/null +++ b/runscript.sh @@ -0,0 +1,9 @@ +#!/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 generate ./... +go build -o bin/main -ldflags="-X 'github.com/dirtree/internal/version.buildDate=${BUILD_DATE}'" *.go From e10f20a384f2b97c7880693a639bee3f3d5df4c3 Mon Sep 17 00:00:00 2001 From: daniyalumer Date: Tue, 27 Aug 2024 14:41:50 +0500 Subject: [PATCH 4/4] Removed Redundant Code From runscript.sh --- runscript.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/runscript.sh b/runscript.sh index d1fda70..1967b71 100755 --- a/runscript.sh +++ b/runscript.sh @@ -5,5 +5,4 @@ GIT_COMMIT="$(git rev-parse HEAD)" #VERSION="$(git describe --tags --abbrev=0 | tr -d "\n")" -go generate ./... -go build -o bin/main -ldflags="-X 'github.com/dirtree/internal/version.buildDate=${BUILD_DATE}'" *.go +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