Skip to content

Commit

Permalink
add --version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Oct 28, 2023
1 parent 83db9be commit f5e70be
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 16 deletions.
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.

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 f5e70be

Please sign in to comment.