Skip to content

Commit

Permalink
Merge pull request #87 from shogo82148/add-version-flag
Browse files Browse the repository at this point in the history
add --version flag
  • Loading branch information
shogo82148 authored Oct 28, 2023
2 parents 83db9be + c97557b commit 599283e
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 20 deletions.
22 changes: 20 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,32 @@ before:
- go generate ./...

builds:
- env:
- id: rdsmysql
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/rdsmysql

binary: rdsmysql
ldflags:
- -s
- -w
- -X main.version={{.Version}}
- id: rdsmysqldump
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/rdsmysqldump
binary: rdsmysqldump
ldflags:
- -s
- -w
- -X main.version={{.Version}}
archives:
- format: tar.gz
# use zip for windows archives
Expand Down
5 changes: 5 additions & 0 deletions cmd/rdsmysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func main() {
}

func run(c *config.Config) int {
if c.Version {
showVersion()
return 0
}

dir, err := os.MkdirTemp("", "rdsmysql-")
if err != nil {
log.Fatal(err)
Expand Down
25 changes: 25 additions & 0 deletions cmd/rdsmysql/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"runtime"
"runtime/debug"
)

// the version is set by goreleaser
var version = ""

func getVersion() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}

func showVersion() {
fmt.Printf("rdsmysql version %s built with %s %s/%s\n", getVersion(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
5 changes: 5 additions & 0 deletions cmd/rdsmysqldump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func main() {
}

func run(c *config.Config) int {
if c.Version {
showVersion()
return 0
}

dir, err := os.MkdirTemp("", "rdsmysql-")
if err != nil {
log.Fatal(err)
Expand Down
25 changes: 25 additions & 0 deletions cmd/rdsmysqldump/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"runtime"
"runtime/debug"
)

// the version is set by goreleaser
var version = ""

func getVersion() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}

func showVersion() {
fmt.Printf("rdsmysql version %s built with %s %s/%s\n", getVersion(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
11 changes: 7 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ enable-cleartext-plugin

// Config is the configuration for connecting to mysql servers.
type Config struct {
User string
Host string
Port int
Args []string
User string
Host string
Port int
Version bool
Args []string
}

// Parse parses the args of mysql command.
Expand Down Expand Up @@ -92,6 +93,8 @@ func Parse(args []string) (*Config, error) {
return nil, errors.New("fail to parse port")
}
conf.Port = port
case "-V", "--version":
conf.Version = true
default:
conf.Args = append(conf.Args, args[i])
}
Expand Down
4 changes: 0 additions & 4 deletions internal/config/version.go

This file was deleted.

22 changes: 20 additions & 2 deletions v2/.goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,32 @@ before:
- go generate ./...

builds:
- env:
- id: rdsmysql
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/rdsmysql

binary: rdsmysql
ldflags:
- -s
- -w
- -X main.version={{.Version}}
- id: rdsmysqldump
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
main: ./cmd/rdsmysqldump
binary: rdsmysqldump
ldflags:
- -s
- -w
- -X main.version={{.Version}}
archives:
- format: tar.gz
# use zip for windows archives
Expand Down
5 changes: 5 additions & 0 deletions v2/cmd/rdsmysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func main() {
}

func run(c *config.Config) int {
if c.Version {
showVersion()
return 0
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
25 changes: 25 additions & 0 deletions v2/cmd/rdsmysql/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"runtime"
"runtime/debug"
)

// the version is set by goreleaser
var version = ""

func getVersion() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}

func showVersion() {
fmt.Printf("rdsmysql version %s built with %s %s/%s\n", getVersion(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
5 changes: 5 additions & 0 deletions v2/cmd/rdsmysqldump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func main() {
}

func run(c *config.Config) int {
if c.Version {
showVersion()
return 0
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
25 changes: 25 additions & 0 deletions v2/cmd/rdsmysqldump/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"runtime"
"runtime/debug"
)

// the version is set by goreleaser
var version = ""

func getVersion() string {
if version != "" {
return version
}
info, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
return info.Main.Version
}

func showVersion() {
fmt.Printf("rdsmysql version %s built with %s %s/%s\n", getVersion(), runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
11 changes: 7 additions & 4 deletions v2/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ enable-cleartext-plugin

// Config is the configuration for connecting to mysql servers.
type Config struct {
User string
Host string
Port int
Args []string
User string
Host string
Port int
Version bool
Args []string
}

// Parse parses the args of mysql command.
Expand Down Expand Up @@ -91,6 +92,8 @@ func Parse(args []string) (*Config, error) {
return nil, errors.New("fail to parse port")
}
conf.Port = port
case "-V", "--version":
conf.Version = true
default:
conf.Args = append(conf.Args, args[i])
}
Expand Down
4 changes: 0 additions & 4 deletions v2/internal/config/version.go

This file was deleted.

0 comments on commit 599283e

Please sign in to comment.