Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from suzuki-shunsuke/build/fix-mockserver-bina…
Browse files Browse the repository at this point in the history
…ry-version

build: fix mockserver binary version
  • Loading branch information
suzuki-shunsuke authored Sep 8, 2018
2 parents 2c5b960 + 4ce714f commit d717566
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 105 deletions.
61 changes: 61 additions & 0 deletions mockserver/exec/cmd/run.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
106 changes: 2 additions & 104 deletions mockserver/exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
61 changes: 61 additions & 0 deletions mockserver/exec/usecase/run.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions script/tag.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package graylog

// Version is the go-graylog's version.
const Version = "0.6.1"

0 comments on commit d717566

Please sign in to comment.