Skip to content

Commit

Permalink
Merge pull request #37 from rerost/rerost/refactor
Browse files Browse the repository at this point in the history
Wrote domain. and use wire
  • Loading branch information
Hazumi Ichijo authored Jun 22, 2019
2 parents 678c6e0 + ae3a0f5 commit 46475e8
Show file tree
Hide file tree
Showing 45 changed files with 1,332 additions and 154 deletions.
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export GO111MODULE=on
export PATH=$PATH:$PWD/bin
export PATH=$PATH:$PWD/build
export PATH=$PATH:$PWD/script
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ vendor:
go get github.com/izumin5210/gex/cmd/gex
go mod download

PHONY: generate
generate:
go generate ./...

PHONY: go-test
go-test: vendor
go test -v ./... | gex cgt
Expand Down
20 changes: 20 additions & 0 deletions cmd/add/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package add

import (
"context"

"github.com/rerost/es-cli/cmd/add/alias"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewAddCommand(ctx context.Context, ind domain.Index, alis domain.Alias) *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Short: "Add elasitcsearch resources",
Args: cobra.ExactArgs(1),
}

cmd.AddCommand(alias.NewAliasCommand(ctx, alis)) // FIXME Cmd -> Command
return cmd
}
23 changes: 23 additions & 0 deletions cmd/add/alias/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package alias

import (
"context"

"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
"github.com/srvc/fail"
)

func NewAliasCommand(ctx context.Context, alis domain.Alias) *cobra.Command {
cmd := &cobra.Command{
Use: "alias",
Short: "add alias",
Args: cobra.MinimumNArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
err := alis.Add(ctx, args[0], args[1:]...)
return fail.Wrap(err)
},
}

return cmd
}
66 changes: 66 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"context"
"encoding/json"
"fmt"

"github.com/rerost/es-cli/config"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/srvc/fail"
"go.uber.org/zap"
)

func Run() error {
ctx := context.TODO()

cfg, err := NewConfig()
if err != nil {
return fail.Wrap(err)
}

l, err := InitializeLogger(cfg)
if err != nil {
return fail.Wrap(err)
}
defer l.Sync()

zap.ReplaceGlobals(l)

cfgJSON, err := json.Marshal(cfg)
if err != nil {
return fail.Wrap(err)
}
zap.L().Debug("config", zap.String("config", string(cfgJSON)))

cmd, err := InitializeCmd(ctx, cfg)
if err != nil {
return fail.Wrap(err)
}

if err := cmd.Execute(); err != nil {
zap.L().Debug("error", zap.String("stack trace", fmt.Sprintf("%#v\n", err)))
return fail.Wrap(err)
}
return nil
}

func NewConfig() (config.Config, error) {
pflag.StringP("host", "", "http://localhost:9200", "ES hostname")
pflag.StringP("type", "t", "_doc", "ES type")
pflag.StringP("user", "u", "", "ES basic auth user")
pflag.StringP("pass", "p", "", "ES basic auth password")
pflag.BoolP("insecure", "k", false, "Same as curl insecure")
pflag.StringP("namespace", "n", "localhost", "Specify config in es-cli") // For conf. Think alter position

pflag.BoolP("verbose", "v", false, "")
pflag.BoolP("debug", "d", false, "")

viper.BindPFlags(pflag.CommandLine)

var cfg config.Config
pflag.Parse()
err := viper.Unmarshal(&cfg)
return cfg, fail.Wrap(err)
}
20 changes: 20 additions & 0 deletions cmd/copy/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package copy

import (
"context"

copy "github.com/rerost/es-cli/cmd/copy/index"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewCopyCommand(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "copy",
Short: "Copy elasticsearch resources",
Args: cobra.ExactArgs(2),
}

cmd.AddCommand(copy.NewIndexCmd(ctx, ind))
return cmd
}
26 changes: 26 additions & 0 deletions cmd/copy/index/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package copy

import (
"context"

"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
"github.com/srvc/fail"
)

func NewIndexCmd(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "list up index",
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
err := ind.Copy(ctx, args[0], args[1])
if err != nil {
return fail.Wrap(err)
}
return nil
},
}

return cmd
}
20 changes: 20 additions & 0 deletions cmd/count/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package count

import (
"context"

count "github.com/rerost/es-cli/cmd/count/index"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewCountCommand(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "count",
Short: "Count elasticsearch resources",
Args: cobra.ExactArgs(1),
}

cmd.AddCommand(count.NewIndexCmd(ctx, ind))
return cmd
}
29 changes: 29 additions & 0 deletions cmd/count/index/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package count

import (
"context"
"fmt"
"os"

"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
"github.com/srvc/fail"
)

func NewIndexCmd(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "count index",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
cnt, err := ind.Count(ctx, args[0])
if err != nil {
return fail.Wrap(err)
}
fmt.Fprintln(os.Stdout, cnt)
return nil
},
}

return cmd
}
20 changes: 20 additions & 0 deletions cmd/create/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package create

import (
"context"

create "github.com/rerost/es-cli/cmd/create/index"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewCreateCommand(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create elasticsearch resources",
Args: cobra.ExactArgs(2),
}

cmd.AddCommand(create.NewIndexCmd(ctx, ind))
return cmd
}
41 changes: 41 additions & 0 deletions cmd/create/index/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package create

import (
"context"
"io"
"os"

"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
"github.com/srvc/fail"
)

func NewIndexCmd(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "create index",
Args: cobra.RangeArgs(1, 2),
RunE: func(_ *cobra.Command, args []string) error {
var fp io.Reader
switch len(args) {
case 1:
fp = os.Stdin
case 2:
// Read file from filename
fileName := args[1]
var err error
fp, err = os.Open(fileName)
if err != nil {
return fail.Wrap(err)
}
}
err := ind.Create(ctx, args[0], fp)
if err != nil {
return fail.Wrap(err)
}
return nil
},
}

return cmd
}
20 changes: 20 additions & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package delete

import (
"context"

delete "github.com/rerost/es-cli/cmd/delete/index"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewDeleteCommand(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete elasticsearch resources",
Args: cobra.ExactArgs(1),
}

cmd.AddCommand(delete.NewIndexCmd(ctx, ind))
return cmd
}
26 changes: 26 additions & 0 deletions cmd/delete/index/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package delete

import (
"context"

"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
"github.com/srvc/fail"
)

func NewIndexCmd(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "delete index",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
err := ind.Delete(ctx, args[0])
if err != nil {
return fail.Wrap(err)
}
return nil
},
}

return cmd
}
20 changes: 20 additions & 0 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dump

import (
"context"

dump "github.com/rerost/es-cli/cmd/dump/index"
"github.com/rerost/es-cli/domain"
"github.com/spf13/cobra"
)

func NewDumpCommand(ctx context.Context, ind domain.Index) *cobra.Command {
cmd := &cobra.Command{
Use: "dump",
Short: "Delete elasticsearch resources",
Args: cobra.ExactArgs(2),
}

cmd.AddCommand(dump.NewIndexCmd(ctx, ind))
return cmd
}
Loading

0 comments on commit 46475e8

Please sign in to comment.