-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from rerost/rerost/refactor
Wrote domain. and use wire
- Loading branch information
Showing
45 changed files
with
1,332 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.