diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 76d7f8d..e339219 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,11 +34,11 @@ jobs: echo "The current tag is: ${{ steps.taggerDryRun.outputs.tag }}" - name: Build run: | - GOOS=linux GOARCH=amd64 go build -o build/goastgen-linux - GOOS=linux GOARCH=arm64 go build -o build/goastgen-linux-arm64 - GOOS=windows GOARCH=amd64 go build -o build/goastgen-windows.exe - GOOS=darwin GOARCH=amd64 go build -o build/goastgen-macos - GOOS=darwin GOARCH=arm64 go build -o build/goastgen-macos-arm64 + GOOS=linux GOARCH=amd64 go build -o build/goastgen-linux -ldflags "-X main.Version=${{ steps.taggerDryRun.outputs.tag }}" + GOOS=linux GOARCH=arm64 go build -o build/goastgen-linux-arm64 -ldflags "-X main.Version=${{ steps.taggerDryRun.outputs.tag }}" + GOOS=windows GOARCH=amd64 go build -o build/goastgen-windows.exe -ldflags "-X main.Version=${{ steps.taggerDryRun.outputs.tag }}" + GOOS=darwin GOARCH=amd64 go build -o build/goastgen-macos -ldflags "-X main.Version=${{ steps.taggerDryRun.outputs.tag }}" + GOOS=darwin GOARCH=arm64 go build -o build/goastgen-macos-arm64 -ldflags "-X main.Version=${{ steps.taggerDryRun.outputs.tag }}" - name: Set next release version id: taggerFinal uses: anothrNick/github-tag-action@1.61.0 diff --git a/README.md b/README.md new file mode 100644 index 0000000..555a77d --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# AST generator + +This utility generates Abstract Syntax Tree (AST) for .go files in JSON format. + +If you pass the root folder of the go project, it will iterate through all `.go` files from project directory +and generate ast in JSON format for each `.go` file. + +## Usage + +## Building + +Run below command from within root folder of the cloned repository. + +```bash + go build -o build/goastgen +``` + +This will generate native binary for your local machine inside `build` folder + +## Getting Help + +```bash +build/goastgen -help + +Usage: + goastgen [falgs] + +Flags: + -help + print the usage + -out string + Out put location of ast (default ".ast") + -version + print the version +``` + +## Example + +### Single file +1. Generate AST with single `.go` file path without passing `-out` flag to indicate ast json out location. + +```bash +$ goastgen / + +e.g +$ goastgen /path/src/hello.go + +It should generate the AST in JSON format at + +/path/src/.ast/hello.go.json +``` + +2. Generate AST with single `.go` file with `-out` flag + +```bash +$ goastgen -out / + +e.g +$ goastgen -out /tmp/randompath /path/src/hello.go + +It should generate the AST in JSON format at + +/tmp/randompath/hello.go.json +``` + +### Complete project directory + +```bash +/path/repository + - hello.go + - anotherfile.go + - somepkg + - somelib.go +``` +1. Generate AST with above root directory of the go project without passing `-out` flag +```bash +$ goastgen + +e.g. +$ goastgen /path/repository + +It should generate AST in JSON fromat for each .go file at following location + +/path/repository/.ast + - hello.go.json + - anotherfile.go.json + - somepkg + - somelib.go.json +``` + +2. Generate AST with above root directory of the go project with `-out` flag + +```bash +$ goastgen -out + +e.g. +$ goastgen -out /temp/out/ /path/repository + +It should generate AST in JSON fromat for each .go file at following location + +/temp/out/ + - hello.go.json + - anotherfile.go.json + - somepkg + - somelib.go.json +``` \ No newline at end of file diff --git a/main.go b/main.go index 505564d..116eeb5 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "strings" ) +var Version = "dev" + func main() { out, inputPath := parseArguments() processRequest(out, inputPath) @@ -58,15 +60,23 @@ func parseArguments() (string, string) { var ( out string inputPath string = "" + version bool + help bool ) flag.StringVar(&out, "out", ".ast", "Out put location of ast") + flag.BoolVar(&version, "version", false, "print the version") + flag.BoolVar(&help, "help", false, "print the usage") flag.Parse() + if version { + fmt.Println(Version) + os.Exit(0) + } // Check if positional arguments exist if flag.NArg() > 0 { // Retrieve positional arguments inputPath = flag.Arg(0) } - if inputPath == "" { + if inputPath == "" || help { fmt.Println("Usage:") fmt.Println("\tgoastgen [falgs] ") fmt.Println()