Skip to content

Commit

Permalink
fix: modify makefile and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
PoplarYang committed Sep 13, 2020
1 parent 89485bd commit b3f4c14
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 80 deletions.
147 changes: 84 additions & 63 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,91 +1,112 @@
default: help

HOST_GOLANG_VERSION := $(shell go version | cut -d ' ' -f3 | cut -c 3-)
MODULE := redis-trib
define baseinfo
@echo "Build start"
@echo "Build time: $(DATETIME)"
@echo "Build target: arch $(1) for $(2)"
@echo "GOVERSION: ${GOLANG_VERSION}"
@echo "GOPATH:" ${GOPATHS}
@echo
endef

PROG_NAME = redis-trib
DATETIME = $(shell date -u '+%Y-%m-%d_%H:%M:%S')
GITURL = https://github.com/PoplarYang/redis-trib
ifneq (,$(wildcard .git/.*))
COMMIT = $(shell git rev-parse HEAD 2> /dev/null || true)
VERSION = $(shell git describe --tags --abbrev=0 2> /dev/null)
VERSION = $(shell git describe --tags --abbrev=0 2> /dev/null)
BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
else
COMMIT = "unknown"
VERSION = "unknown"
BRANCH = "unkown"
endif

GO=go
GOLANG_VERSION = $(shell go version | cut -d ' ' -f3 | cut -c 3-)
#Replaces ":" (*nix), ";" (windows) with newline for easy parsing
GOPATHS=$(shell echo ${GOPATH} | tr ":" "\n" | tr ";" "\n")
GOPATHS = $(shell echo ${GOPATH} | tr ":" "\n" | tr ";" "\n")

# See Golang issue re: '-trimpath': https://github.com/golang/go/issues/13809
GO_GCFLAGS=$(shell \
set -- ${GOPATHS}; \
GO_GCFLAGS=$(shell \
set -- ${GOPATHS}; \
echo "-gcflags=-trimpath=$${1}"; \
)

GO_ASMFLAGS=$(shell \
set -- ${GOPATHS}; \
GO_ASMFLAGS=$(shell \
set -- ${GOPATHS}; \
echo "-asmflags=-trimpath=$${1}"; \
)

#export GOPATH := $(shell cd ./ && pwd)/vendor:$(GOPATH)

## Make bin for $MODULE.
bin:
@echo "GOVERSION: ${HOST_GOLANG_VERSION}"
@echo "GOPATH:" $$GOPATH
go build ${GO_GCFLAGS} ${GO_ASMFLAGS} -i -ldflags "-X main.gitCommit=${COMMIT} -X main.version=${VERSION}" -o ${MODULE} .

## Build debug trace for $MODULE.
GO_LDFLAGS="-extldflags '-static' -s -w \
-X main.version=$(VERSION) \
-X main.gitURL=$(GITURL) \
-X main.gitCommit=$(COMMIT) \
-X main.Branch=$(BRANCH) \
-X main.BuildTime=$(DATETIME)"

DARWIN-AMD64 = $(PROG_NAME).$(VERSION).darwin-amd64
LINUX-386 = $(PROG_NAME).$(VERSION).linux-386
LINUX-AMD64 = $(PROG_NAME).$(VERSION).linux-amd64
LINUX-ARM64 = $(PROG_NAME).$(VERSION).linux-arm64
LINUX-MIPS64EL = $(PROG_NAME).$(VERSION).linux-mips64el
WIN-386 = $(PROG_NAME).$(VERSION).windows-386.exe
WIN-AMD64 = $(PROG_NAME).$(VERSION).windows-amd64.exe

## Make linux windows darwin bins for $PROG_NAME
all: linux windows darwin

## Make for darwin (arch: amd64)
darwin:
$(call baseinfo,amd64,darwin)
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(DARWIN-AMD64)

## Make for linux (arch: 386, amd64, arm, mips64le)
linux:
$(call baseinfo,386,linux)
CGO_ENABLED=0 GOOS=linux GOARCH=386 $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(LINUX-386)
$(call baseinfo,amd64,linux)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(LINUX-AMD64)
$(call baseinfo,arm,linux)
CGO_ENABLED=0 GOOS=linux GOARCH=arm $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(LINUX-ARM64)
$(call baseinfo,mips64le,linux)
CGO_ENABLED=0 GOOS=linux GOARCH=mips64le $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(LINUX-MIPS64EL)

## Make for windows (arch: 386, amd64)
windows:
$(call baseinfo,386,windows)
CGO_ENABLED=0 GOOS=windows GOARCH=386 $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(WIN-386)
$(call baseinfo,amd64,windows)
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build $(GO_GCFLAGS) $(GO_ASMFLAGS) -ldflags $(GO_LDFLAGS) -o $(WIN-AMD64)

## Build debug trace for $PROG_NAME.
debug:
@echo "GOVERSION: ${HOST_GOLANG_VERSION}"
@echo "GOPATH:" $$GOPATH
go build -n -v -i -ldflags "-X main.gitCommit=${COMMIT} -X main.version=${VERSION}" -o ${MODULE} .

## Make static link bin for $MODULE.
static-bin:
@echo "GOVERSION:" ${HOST_GOLANG_VERSION}
@echo "GOPATH:" $$GOPATH
go build ${GO_GCFLAGS} ${GO_ASMFLAGS} -i -ldflags "-w -extldflags -static -X main.gitCommit=${COMMIT} -X main.version=${VERSION}" -o ${MODULE} .
$(GO) build -n -v -i -ldflags "-X main.gitCommit=${COMMIT} -X main.version=${VERSION}" -o ${MODULE} .

## Get dep tool for managing dependencies for Go projects.
dep:
go get -u github.com/golang/dep/cmd/dep

## Get dep tool and init project.
depinit: dep
dep init
dep ensure

## Get vet go tools.
vet:
go get golang.org/x/tools/cmd/vet

## Validate this go project.
validate:
script/validate-gofmt
#go vet ./...
deps:
$(GO) mod download

## Run test case for this go project.
test:
go test $(go list ./... | grep -v '/vendor/')
$(GO) test $($(GO) list ./...)

## Clean everything (including stray volumes).
## Clean the project
clean:
# find . -name '*.created' -exec rm -f {} +
-rm -rf var
-rm -f ${MODULE}
$(GO) clean
-rm $(DARWIN-AMD64) $(LINUX-386) $(LINUX-AMD64) $(LINUX-ARM64) $(LINUX-MIPS64EL) $(WIN-386) $(WIN-AMD64) 2> /dev/null || true

## Print help
help: # Some kind of magic from https://gist.github.com/rcmachado/af3db315e31383502660
$(info Available targets)
@awk '/^[a-zA-Z\-\_0-9]+:/ { \
nb = sub( /^## /, "", helpMsg ); \
if(nb == 0) { \
helpMsg = $$0; \
nb = sub( /^[^:]*:.* ## /, "", helpMsg ); \
} \
if (nb) { \
h = sub( /[^ ]*MODULE/, "'${MODULE}'", helpMsg ); \
printf " \033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \
} \
} \
{ helpMsg = $$0 }' \
@awk '/^[a-zA-Z\-_0-9]+:/ { \
nb = sub( /^## /, "", helpMsg ); \
if(nb == 0) { \
helpMsg = $$0; \
nb = sub( /^[^:]*:.* ## /, "", helpMsg ); \
} \
if (nb) { \
h = sub( /[^ ]*PROG_NAME/, "'${PROG_NAME}'", helpMsg ); \
printf " \033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \
} \
} \
{ helpMsg = $$0 }' \
width=$$(grep -o '^[a-zA-Z_0-9]\+:' $(MAKEFILE_LIST) | wc -L 2> /dev/null) \
$(MAKEFILE_LIST)

18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ Dependencies are handled by [govendor][], simple install it and type `govendor a

#### Restore project env in first build
```console
$ git clone https://github.com/soarpenguin/redis-trib.git
$ git clone https://github.com/PoplarYang/redis-trib.git
$ cd redis-trib
$ make govendor
$ make bin
$ PROG=./redis-trib source ./autocomplete/bash_autocomplete
$ make deps
```

#### Build the code
```console
$ cd redis-trib
$ make bin
$ make all
```

## Usage
Expand All @@ -42,11 +40,12 @@ USAGE:
redis-trib [global options] command [command options] [arguments...]

VERSION:
v0.1.0
commit: 533d96aba4b0e73649dcf81209156230698666aa
v0.2.1
commit: 89485bd15e7fd42d365a66b4cc87339461e718c7
giturl: https://github.com/PoplarYang/redis-trib

AUTHOR(S):
soarpenguin <soarpenguin@gmail.com>
AUTHOR:
PoplarYang <echohiyang@foxmail.com>

COMMANDS:
add-node, add add a new redis node to existed cluster.
Expand All @@ -60,6 +59,7 @@ COMMANDS:
rebalance rebalance the redis cluster.
reshard reshard the redis cluster.
set-timeout set timeout configure for redis cluster.
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--debug enable debug output for logging
Expand Down
2 changes: 1 addition & 1 deletion clusternode.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (cn *ClusterNode) FlushNodeConfig() {
cn.info.slots[slot] = AssignedHashSlot
_, err := cn.ClusterAddSlots(slot)
if err != nil {
logrus.Printf("ClusterAddSlots slot: %s with error %s", slot, err)
logrus.Printf("ClusterAddSlots slot: %d with error %s", slot, err)
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var createCommand = cli.Command{
Value: 0,
Usage: `Slave number for every master created, the default value is none.
$ redis-trib create <--replicas 1> <host1:port1 ... hostN:portN>`,
$ redis-trib create <--replicas 1> <--password ""> <host1:port1 ... hostN:portN>`,
},
cli.StringFlag{
Name: "password, a",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/soarpenguin/redis-trib
module github.com/PoplarYang/redis-trib

go 1.13

Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var version = ""
// and will be populated by the Makefile
var gitCommit = ""

// gitUrl will be the hash that the binary was built from
// and will be populated by the Makefile
var gitURL = ""

const (
// name holds the name of this program
name = "redis-trib"
Expand Down Expand Up @@ -110,13 +114,19 @@ func commandNotFound(c *cli.Context, command string) {

// makeVersionString returns a multi-line string describing the runtime version.
func makeVersionString() string {
v := []string{
version,
v := []string{}
if version != "" {
v = append(v, version)
}

if gitCommit != "" {
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
}

if gitURL != "" {
v = append(v, fmt.Sprintf("giturl: %s", gitURL))
}

return strings.Join(v, "\n")
}

Expand All @@ -128,8 +138,8 @@ func main() {
app.Usage = usage
app.Version = makeVersionString()
app.Flags = runtimeFlags
app.Author = "soarpenguin"
app.Email = "soarpenguin@gmail.com"
app.Author = "PoplarYang"
app.Email = "echohiyang@foxmail.com"
app.EnableBashCompletion = true
app.CommandNotFound = runtimeCommandNotFound
app.Before = runtimeBeforeSubcommands
Expand Down
2 changes: 1 addition & 1 deletion rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (rt *RedisTrib) RebalanceClusterCmd(context *cli.Context) error {
}

if numSlots > 0 {
logrus.Printf("Moving %d slots from %s to %s", numSlots, src.String(), dst.String())
logrus.Printf("Moving %f slots from %s to %s", numSlots, src.String(), dst.String())

// Actaully move the slots.
// TODO: add move slot code.
Expand Down

0 comments on commit b3f4c14

Please sign in to comment.