From 0f3560a08b9944ef9c36e89cbb1003dca6b0f610 Mon Sep 17 00:00:00 2001 From: Moch Lutfi Date: Tue, 29 Sep 2020 08:29:24 +0700 Subject: [PATCH] feat: add goreleaser build --- .github/workflows/gorelease.yml | 27 +++++++++++++++++++++++++++ .goreleaser.yml | 21 +++++++++++++++++++++ main.go | 21 ++++++++++++++++----- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/gorelease.yml create mode 100644 .goreleaser.yml diff --git a/.github/workflows/gorelease.yml b/.github/workflows/gorelease.yml new file mode 100644 index 0000000..ec78b9c --- /dev/null +++ b/.github/workflows/gorelease.yml @@ -0,0 +1,27 @@ +name: Releaser +on: + push: + tags: + - '*' +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - + name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.15 + - + name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..9b94690 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,21 @@ +project_name: optgen +env: + - GO111MODULE=on +builds: + - + main: main.go + binary: optgen + ldflags: + - -s -w + - -X main.Version={{.Major}}.{{.Minor}}.{{.Patch}} + env: + - CGO_ENABLED=0 + goos: + - darwin + - linux + - windows + goarch: + - amd64 + hooks: + post: + - upx --brute "{{ .Path }}" \ No newline at end of file diff --git a/main.go b/main.go index e5a298a..fdf5174 100644 --- a/main.go +++ b/main.go @@ -17,8 +17,10 @@ import ( ) var ( - sourceFile, tagName, structName string - writeMode, allFields bool + sourceFile, tagName, structName string + writeMode, allFields, showVersion bool + + version string = "dev" funcMap = template.FuncMap{ "title": strings.Title, @@ -31,16 +33,25 @@ func initCLI() { flag.StringVar(&tagName, "tag", "opt", "custom tag") flag.StringVar(&structName, "name", "", "struct name") flag.BoolVar(&writeMode, "w", false, "enable write mode") + flag.BoolVar(&showVersion, "v", false, "show version") flag.BoolVar(&allFields, "all", false, "generate all fields") flag.Parse() +} + +func main() { + initCLI() + + if showVersion { + fmt.Printf("version: %s", version) + + return + } + const exampleMessage = "e.g: optgen -file sample-file.go -name Thing" if sourceFile == "" || structName == "" { log.Fatalf("Source file and struct name must be provided.\n%s", exampleMessage) } -} -func main() { - initCLI() source, err := ioutil.ReadFile(sourceFile) if err != nil { log.Fatal(err)