Skip to content

Commit

Permalink
[feat:#32][cmd]: add commands
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingCrush authored and stone1100 committed May 3, 2022
1 parent e135bf5 commit 1f8ee08
Show file tree
Hide file tree
Showing 84 changed files with 9,815 additions and 183 deletions.
25 changes: 17 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
.PHONY: help build test deps pb clean

# use the latest git tag as release-version
GIT_TAG_NAME=$(shell git tag --sort=-creatordate|head -n 1)
BUILD_TIME=$(shell date "+%Y-%m-%dT%H:%M:%S%z")
LD_FLAGS=-ldflags="-X github.com/eleme/lindb/cmd/lind.version=$(GIT_TAG_NAME) -X github.com/eleme/lindb/cmd/lind.buildTime=$(BUILD_TIME)"

# Ref: https://gist.github.com/prwhite/8168133
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} \
/^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

build-frontend: clean-build
cd web/ && make web_build

GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
build: ## Build executable files. (Args: GOOS=$(go env GOOS) GOARCH=$(go env GOARCH))
cd web/ && make web_build
env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o 'bin/broker' $(LDFLAGS) ./cmd/broker/
# env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o 'bin/cli' $(LDFLAGS) ./cmd/cli/
env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o 'bin/stroage' $(LDFLAGS) ./cmd/storage/
build: clean-build ## Build executable files. (Args: GOOS=$(go env GOOS) GOARCH=$(go env GOARCH))
env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o 'bin/lind' $(LD_FLAGS) ./cmd/

build-all: build-frontend build ## Build executable files with front-end files inside.

GOLANGCI_LINT_VERSION ?= "latest"

generate:
generate: ## go generate
go list ./... | grep -v '/vendor/' | xargs go generate

test: ## Run test cases. (Args: GOLANGCI_LINT_VERSION=latest)
Expand All @@ -37,9 +42,13 @@ deps: ## Update vendor.
pb: ## generate pb file.
./generate_pb.sh

clean: ## Clean up useless files.
rm -rf bin
clean-build:
rm -f bin/lind
cd web/ && make web_clean


clean: ## Clean up useless files.
$(clean-build)
find . -type f -name '*.out' -exec rm -f {} +
find . -type f -name '.DS_Store' -exec rm -f {} +
find . -type f -name '*.test' -exec rm -f {} +
Expand Down
10 changes: 0 additions & 10 deletions broker/config.go

This file was deleted.

4 changes: 2 additions & 2 deletions broker/rest/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net/http"

"github.com/eleme/lindb/broker"
"github.com/eleme/lindb/config"

rice "github.com/GeertJohan/go.rice"

Expand All @@ -28,7 +28,7 @@ var rs = routes{

// NewRouter returns a new router with a panic handler and a static server
// handler.
func NewRouter(config *broker.Config) *mux.Router {
func NewRouter(config *config.BrokerConfig) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range rs {
router.
Expand Down
71 changes: 0 additions & 71 deletions cmd/broker/main.go

This file was deleted.

39 changes: 0 additions & 39 deletions cmd/cli/cli.go

This file was deleted.

86 changes: 86 additions & 0 deletions cmd/lind/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package lind

import (
"fmt"
"net/http"
_ "net/http/pprof" // for profiling
"os"

"github.com/eleme/lindb/broker/rest"
"github.com/eleme/lindb/config"

"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
)

var (
brokerCfgPath = ""
brokerDebug = false
)

const (
brokerCfgName = "broker.toml"
defaultBrokerCfgFile = cfgFilePath + "/" + brokerCfgName
)

// newBrokerCmd returns a new broker-cmd
func newBrokerCmd() *cobra.Command {
brokerCmd := &cobra.Command{
Use: "broker",
Aliases: []string{"bro"},
Short: "The compute layer of LinDB",
}
runBrokerCmd.PersistentFlags().StringVar(&brokerCfgPath, "config", "",
fmt.Sprintf("broker config file path, default is %s", defaultBrokerCfgFile))
runBrokerCmd.PersistentFlags().BoolVar(&brokerDebug, "debug", false,
"profiling Go programs with pprof")
brokerCmd.AddCommand(
runBrokerCmd,
initializeBrokerConfigCmd,
)
return brokerCmd
}

var runBrokerCmd = &cobra.Command{
Use: "run",
Short: "starts the broker",
RunE: serveBroker,
}

// initialize config for broker
var initializeBrokerConfigCmd = &cobra.Command{
Use: "initialize-config",
Short: "initialize a new broker-config by steps",
Run: func(cmd *cobra.Command, args []string) {
// todo: @codingcrush
},
}

// serveBroker runs the broker
func serveBroker(cmd *cobra.Command, args []string) error {
ctx := newCtxWithSignals()
go func() {
<-ctx.Done()
os.Exit(0)
}()

if brokerCfgPath == "" {
brokerCfgPath = defaultBrokerCfgFile
}
if _, err := os.Stat(brokerCfgPath); err != nil {
return fmt.Errorf("config file doesn't exist, see how to initialize the config by `lind broker -h`")
}
fmt.Printf("load config file: %v successfully\n", brokerCfgPath)

brokerConfig := config.BrokerConfig{}
if _, err := toml.DecodeFile(brokerCfgPath, &brokerConfig); err != nil {
return err
}
fmt.Printf("HTTP server listening on: %d\n", brokerConfig.HTTP.Port)

router := rest.NewRouter(&brokerConfig)
if err := http.ListenAndServe(fmt.Sprintf(":%d", brokerConfig.HTTP.Port), router); err != nil {
return err
}
return nil
}
47 changes: 47 additions & 0 deletions cmd/lind/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lind

import (
"fmt"
"runtime"

"github.com/spf13/cobra"
)

// These variables are populated via the Go linker.
var (
// release version, ldflags
version string
// binary build-time, ldflags
buildTime string
)

const (
defaultVersion = "alpha"
)

func printVersion() {
var releaseVersion = defaultVersion
if version != "" {
releaseVersion = version
}
fmt.Printf("LinDB %v, BuildDate: %v\n", releaseVersion, buildTime)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of LinDB",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
},
}

var envCmd = &cobra.Command{
Use: "env",
Short: "Print environment info of LinDB",
Run: func(cmd *cobra.Command, args []string) {
printVersion()
fmt.Printf("GOOS=%q\n", runtime.GOOS)
fmt.Printf("GOARCH=%q\n", runtime.GOARCH)
fmt.Printf("GOVERSION=%q\n", runtime.Version())
},
}
37 changes: 37 additions & 0 deletions cmd/lind/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lind

import (
"github.com/spf13/cobra"
)

const (
// default config file location of LinDB
cfgFilePath = "/etc/lindb"

linDBText = `
__ _ ____ ____
/ / (_) ____ / __ \ / __ )
/ / / / / __ \ / / / / / __ |
/ /___ / / / / / / / /_/ / / /_/ /
/_____//_/ /_/ /_/ /_____/ /_____/
LinDB is a scalable, distributed, high performance, high availability Time Series Database, produced by Eleme-CI.
Complete documentation is available at https://github.com/eleme/lindb
`
)

// RootCmd command of cobra
var RootCmd = &cobra.Command{
Use: "lind",
Short: "lind is the main command, used to control LinDB",
Long: linDBText,
}

func init() {
RootCmd.AddCommand(
envCmd,
versionCmd,
newStorageCmd(),
newBrokerCmd(),
)
}
25 changes: 25 additions & 0 deletions cmd/lind/signals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lind

import (
"context"
"os"
"os/signal"
"syscall"
)

// newCtxWithSignals returns a context which will can be canceled by sending signal.
func newCtxWithSignals() context.Context {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
select {
case <-ctx.Done():
return
case <-c:
return
}
}()
return ctx
}
Loading

0 comments on commit 1f8ee08

Please sign in to comment.