Skip to content

Commit

Permalink
reject interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
po3rin committed Nov 28, 2020
1 parent 99de15d commit 1f566bb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
11 changes: 11 additions & 0 deletions cmd/eskeeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
)

const (
TERMINAL = "terminal"
PIPE = "pipe"
)

var rootCmd = &cobra.Command{
Expand All @@ -26,6 +32,11 @@ var rootCmd = &cobra.Command{
os.Exit(1)
}

if terminal.IsTerminal(int(os.Stdin.Fd())) {
fmt.Println("Currently does not support interactive mode")
os.Exit(1)
}

ctx := context.Background()
err = k.Sync(ctx, os.Stdin)
if err != nil {
Expand Down
86 changes: 50 additions & 36 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
var status = map[string]struct{}{
"open": struct{}{},
"close": struct{}{},
"": struct{}{},
"": struct{}{}, // default
}

type config struct {
Expand Down Expand Up @@ -47,6 +47,46 @@ func yaml2Conf(reader io.Reader) (config, error) {
return conf, nil
}

func validateIndex(index index) error {
if index.Name == "" {
return errors.New("index name is empty")
}
if index.Mapping != "" {
m, err := ioutil.ReadFile(index.Mapping)
if err != nil {
return fmt.Errorf("read file %v: %w", index.Mapping, err)
}
// validate json format
var jsonStr map[string]interface{}
if err := json.Unmarshal(m, &jsonStr); err != nil {
return fmt.Errorf("mapping json is invalid: %w", err)
}
}
_, ok := status[index.Status]
if !ok {
return fmt.Errorf("unsupported status %v", index.Status)
}

return nil
}

func validateAlias(alias alias) error {
if alias.Name == "" {
return errors.New("alias name is empty")
}

if len(alias.Indices) == 0 {
return fmt.Errorf("no indices in %v alias", alias.Name)
}

for _, index := range alias.Indices {
if index == "" {
return errors.New("index name is empty")
}
}
return nil
}

func (e *Eskeeper) validateConfigFormat(c config) error {
createIndices := make(map[string]struct{}, 0)

Expand All @@ -59,54 +99,28 @@ func (e *Eskeeper) validateConfigFormat(c config) error {

createIndices[index.Name] = struct{}{}

if index.Name == "" {
e.logf("[fail] index: %v\n", index.Name)
return errors.New("index name is empty")
}
if index.Mapping != "" {
m, err := ioutil.ReadFile(index.Mapping)
if err != nil {
e.logf("[fail] index: %v\n", index.Name)
return fmt.Errorf("read file %v: %w", index.Mapping, err)
}
// validate json format
var jsonStr map[string]interface{}
if err := json.Unmarshal(m, &jsonStr); err != nil {
e.logf("[fail] index: %v\n", index.Name)
return fmt.Errorf("mapping json is invalid: %w", err)
}
}
_, ok := status[index.Status]
if !ok {
err := validateIndex(index)
if err != nil {
e.logf("[fail] index: %v\n", index.Name)
return fmt.Errorf("unsupported status %v", index.Status)
return fmt.Errorf("validate index: %w", err)
}

e.logf("[pass] index: %v\n", index.Name)
}

for _, alias := range c.Aliases {
if alias.Name == "" {
e.logf("[fail] alias: %v\n", alias.Name)
return errors.New("alias name is empty")
}

if len(alias.Indices) == 0 {
e.logf("[fail] alias: %v\n", alias.Name)
return fmt.Errorf("no indices in %v alias", alias.Name)
}

_, ok := createIndices[alias.Name]
if ok {
e.logf("[fail] alias: %v\n", alias.Name)
return fmt.Errorf("alias name %v is a duplicate of an index name that already exists", alias.Name)
}

for _, index := range alias.Indices {
if index == "" {
e.logf("[fail] alias: %v\n", alias.Name)
return errors.New("index name is empty")
}
err := validateAlias(alias)
if err != nil {
e.logf("[fail] alias: %v\n", alias.Name)
return fmt.Errorf("validate alias: %w", err)
}

e.logf("[pass] alias: %v\n", alias.Name)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/google/go-cmp v0.4.0 // indirect
github.com/itchyny/gojq v0.11.1
github.com/itchyny/timefmt-go v0.1.1 // indirect
github.com/kataras/pio v0.0.10
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/ory/dockertest v3.3.5+incompatible
github.com/ory/dockertest/v3 v3.6.0 // indirect
Expand All @@ -27,7 +28,7 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.7.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 // indirect
golang.org/x/text v0.3.3 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kataras/pio v0.0.10 h1:b0qtPUqOpM2O+bqa5wr2O6dN4cQNwSmFd6HQqgVae0g=
github.com/kataras/pio v0.0.10/go.mod h1:gS3ui9xSD+lAUpbYnjOGiQyY7sUMJO+EHpiRzhtZ5no=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -300,6 +302,7 @@ golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down

0 comments on commit 1f566bb

Please sign in to comment.