From 4ce714f1a1d6b4de3212174068673b9919b27347 Mon Sep 17 00:00:00 2001 From: Suzuki Shunsuke Date: Sat, 8 Sep 2018 17:36:06 +0900 Subject: [PATCH] build: fix mockserver binary version --- mockserver/exec/cmd/run.go | 61 +++++++++++++++++++ mockserver/exec/main.go | 106 +-------------------------------- mockserver/exec/usecase/run.go | 61 +++++++++++++++++++ package.json | 3 +- script/tag.sh | 37 ++++++++++++ version.go | 4 ++ 6 files changed, 167 insertions(+), 105 deletions(-) create mode 100644 mockserver/exec/cmd/run.go create mode 100644 mockserver/exec/usecase/run.go create mode 100644 script/tag.sh create mode 100644 version.go diff --git a/mockserver/exec/cmd/run.go b/mockserver/exec/cmd/run.go new file mode 100644 index 00000000..f9825b53 --- /dev/null +++ b/mockserver/exec/cmd/run.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "flag" + "fmt" + + log "github.com/sirupsen/logrus" + + "github.com/suzuki-shunsuke/go-graylog" + "github.com/suzuki-shunsuke/go-graylog/mockserver/exec/usecase" +) + +var help string + +func init() { + help = fmt.Sprintf(` +graylog-mock-server - Run Graylog mock server. + +USAGE: + graylog-mock-server [options] + +VERSION: + %s + +OPTIONS: + --port value port number. If you don't set this option, a free port is assigned and the assigned port number is output to the console when the mock server runs. + --log-level value the log level of logrus which the mock server uses internally. (default: "info") + --data value data file path. When the server runs data of the file is loaded and when data of the server is changed data is saved at the file. If this option is not set, no data is loaded and saved. + --help, -h show help + --version, -v print the version +`, graylog.Version) +} + +// Run runs a mock server. +func Run() { + var portFlag = flag.Int( + "port", 0, + "port number. If you don't set this option, a free port is assigned and the assigned port number is output to the console when the mock server runs.") + var dataFlag = flag.String( + "data", "", + "data file path. When the server runs data of the file is loaded and when data of the server is changed data is saved at the file. If this option is not set, no data is loaded and saved.") + var logLevelFlag = flag.String( + "log-level", "info", + `the log level of logrus which the mock server uses internally. (default: "info")`) + var helpFlag = flag.Bool("help", false, "Show help.") + var versionFlag = flag.Bool("version", false, "Print the version.") + flag.Parse() + + if *helpFlag { + fmt.Println(help) + return + } + if *versionFlag { + fmt.Println(graylog.Version) + return + } + + if err := usecase.Run(*dataFlag, *logLevelFlag, *portFlag); err != nil { + log.Fatal(err) + } +} diff --git a/mockserver/exec/main.go b/mockserver/exec/main.go index 36f0aeb0..aa415de9 100644 --- a/mockserver/exec/main.go +++ b/mockserver/exec/main.go @@ -5,111 +5,9 @@ package main import ( - "flag" - "fmt" - "os" - "os/signal" - "syscall" - - "github.com/pkg/errors" - log "github.com/sirupsen/logrus" - "github.com/suzuki-shunsuke/go-graylog/mockserver" - "github.com/suzuki-shunsuke/go-graylog/mockserver/store/plain" + "github.com/suzuki-shunsuke/go-graylog/mockserver/exec/cmd" ) -const version = "0.5.1" - -var help string - -func init() { - help = fmt.Sprintf(` -graylog-mock-server - Run Graylog mock server. - -USAGE: - graylog-mock-server [options] - -VERSION: - %s - -OPTIONS: - --port value port number. If you don't set this option, a free port is assigned and the assigned port number is output to the console when the mock server runs. - --log-level value the log level of logrus which the mock server uses internally. (default: "info") - --data value data file path. When the server runs data of the file is loaded and when data of the server is changed data is saved at the file. If this option is not set, no data is loaded and saved. - --help, -h show help - --version, -v print the version -`, version) -} - -func action(dataPath, logLevel string, port int) error { - var ( - server *mockserver.Server - err error - ) - if port == 0 { - server, err = mockserver.NewServer( - "", plain.NewStore(dataPath)) - } else { - server, err = mockserver.NewServer( - fmt.Sprintf(":%d", port), plain.NewStore(dataPath)) - } - if err != nil { - return errors.Wrap(err, "failed to create a mock server") - } - lvl, err := log.ParseLevel(logLevel) - if err != nil { - return fmt.Errorf( - `invalid log-level %s. -log-level must be any of debug|info|warn|error|fatal|panic`, logLevel) - } - - server.Logger().SetLevel(lvl) - if err := server.Load(); err != nil { - return errors.Wrap(err, fmt.Sprintf("failed to load data at %s", dataPath)) - } - server.Start() - defer server.Close() - server.Logger().Infof( - "Start Graylog mock server: %s\nCtrl + C to stop server", server.Endpoint()) - signalChan := make(chan os.Signal, 1) - signal.Notify( - signalChan, syscall.SIGHUP, syscall.SIGINT, - syscall.SIGTERM, syscall.SIGQUIT) - exitChan := make(chan int) - go func() { - for { - <-signalChan - exitChan <- 0 - } - }() - - <-exitChan - return nil -} - func main() { - var portFlag = flag.Int( - "port", 0, - "port number. If you don't set this option, a free port is assigned and the assigned port number is output to the console when the mock server runs.") - var dataFlag = flag.String( - "data", "", - "data file path. When the server runs data of the file is loaded and when data of the server is changed data is saved at the file. If this option is not set, no data is loaded and saved.") - var logLevelFlag = flag.String( - "log-level", "info", - `the log level of logrus which the mock server uses internally. (default: "info")`) - var helpFlag = flag.Bool("help", false, "Show help.") - var versionFlag = flag.Bool("version", false, "Print the version.") - flag.Parse() - - if *helpFlag { - fmt.Println(help) - return - } - if *versionFlag { - fmt.Println(version) - return - } - - if err := action(*dataFlag, *logLevelFlag, *portFlag); err != nil { - log.Fatal(err) - } + cmd.Run() } diff --git a/mockserver/exec/usecase/run.go b/mockserver/exec/usecase/run.go new file mode 100644 index 00000000..e2d82e39 --- /dev/null +++ b/mockserver/exec/usecase/run.go @@ -0,0 +1,61 @@ +package usecase + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + + "github.com/suzuki-shunsuke/go-graylog/mockserver" + "github.com/suzuki-shunsuke/go-graylog/mockserver/store/plain" +) + +// Run runs a mock server. +func Run(dataPath, logLevel string, port int) error { + var ( + server *mockserver.Server + err error + ) + if port == 0 { + server, err = mockserver.NewServer( + "", plain.NewStore(dataPath)) + } else { + server, err = mockserver.NewServer( + fmt.Sprintf(":%d", port), plain.NewStore(dataPath)) + } + if err != nil { + return errors.Wrap(err, "failed to create a mock server") + } + lvl, err := log.ParseLevel(logLevel) + if err != nil { + return fmt.Errorf( + `invalid log-level %s. +log-level must be any of debug|info|warn|error|fatal|panic`, logLevel) + } + + server.Logger().SetLevel(lvl) + if err := server.Load(); err != nil { + return errors.Wrap(err, fmt.Sprintf("failed to load data at %s", dataPath)) + } + server.Start() + defer server.Close() + server.Logger().Infof( + "Start Graylog mock server: %s\nCtrl + C to stop server", server.Endpoint()) + signalChan := make(chan os.Signal, 1) + signal.Notify( + signalChan, syscall.SIGHUP, syscall.SIGINT, + syscall.SIGTERM, syscall.SIGQUIT) + exitChan := make(chan int) + go func() { + for { + <-signalChan + exitChan <- 0 + } + }() + + <-exitChan + return nil +} diff --git a/package.json b/package.json index 47f2210d..7a0b8009 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "fmt": "bash script/fmt.sh", "coverage": "bash script/coverage.sh", "install-local-terraform": "bash script/install_local_terraform.sh", - "commitlint-travis": "commitlint-travis" + "commitlint-travis": "commitlint-travis", + "tag": "bash script/tag.sh" }, "devDependencies": { "@commitlint/cli": "^6.1.3", diff --git a/script/tag.sh b/script/tag.sh new file mode 100644 index 00000000..9dbdee0f --- /dev/null +++ b/script/tag.sh @@ -0,0 +1,37 @@ +# Usage +# bash script/tag.sh v0.6.1 + +if [ $# -gt 1 ]; then + echo "too many arguments" > /dev/stderr + echo 'Usage tag.sh $TAG' > /dev/stderr + exit 1 +fi + +if [ $# -lt 1 ]; then + echo "TAG argument is required" > /dev/stderr + echo 'Usage tag.sh $TAG' > /dev/stderr + exit 1 +fi + +TAG=$1 +echo "TAG: $TAG" +VERSION=${TAG#v} + +if [ "$TAG" = "$VERSION" ]; then + echo "TAG must start with 'v'" + exit 1 +fi + +echo "cd `dirname $0`/.." +cd `dirname $0`/.. + +echo "create version.go" +cat << EOS > version.go +package domain + +// Version is the go-graylog's version. +const Version = "$VERSION" +EOS + +git add version.go +npm run release -- --release-as $TAG diff --git a/version.go b/version.go new file mode 100644 index 00000000..6bb47acd --- /dev/null +++ b/version.go @@ -0,0 +1,4 @@ +package graylog + +// Version is the go-graylog's version. +const Version = "0.6.1"