Skip to content

Commit

Permalink
Merge pull request IBM-Cloud#4 from IBM-Cloud/compliancefixes
Browse files Browse the repository at this point in the history
Compliance fixes
  • Loading branch information
codepope committed Aug 21, 2018
2 parents f661978 + 6edefba commit db51836
Show file tree
Hide file tree
Showing 248 changed files with 164,370 additions and 43 deletions.
42 changes: 39 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/mattn/go-isatty"
version = "0.0.3"
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ Redli is a Go-based alternative to the official Redis-cli application. It's majo

```text
redli [<flags>] [<commands>...]
```
### Flags

```text
--help Show context-sensitive help (also try --help-long and --help-man).
--debug Enable debug mode.
--long Enable long prompt with host/port
Expand All @@ -24,23 +20,21 @@ Redli is a Go-based alternative to the official Redis-cli application. It's majo
--tls Enable TLS/SSL
--certfile=CERTFILE Self-signed certificate file for validation
--certb64=CERTB64 Self-signed certificate string as base64 for validation
```

* `URI` URI to connect To. It follow the format of [the provisional IANA spec for Redis URLs](https://www.iana.org/assignments/uri-schemes/prov/redis), but with the option to denote a TLS secured connection with the protocol rediss:.
--raw Produce raw output
### Args

```text
Args:
[<commands>] Redis commands and values
```

e.g. `INFO KEYSPACE`
* `URI` URI to connect To. It follow the format of [the provisional IANA spec for Redis URLs](https://www.iana.org/assignments/uri-schemes/prov/redis), but with the option to denote a TLS secured connection with the protocol rediss:.

e.g. `INFO KEYSPACE`

Be aware of interactions with wild cards and special characters in the shell; quote and escape as appropriate.

## License

Redli is (c) IBM Corporation 2017. All rights reserved.
Redli is (c) IBM Corporation 2018. All rights reserved.

Redli is released under the Apache 2 License.

Expand Down
114 changes: 86 additions & 28 deletions redli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/gomodule/redigo/redis"
"github.com/mattn/go-isatty"
"github.com/mattn/go-shellwords"
"github.com/peterh/liner"
"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -30,17 +31,27 @@ var (
redistls = kingpin.Flag("tls", "Enable TLS/SSL").Default("false").Bool()
rediscertfile = kingpin.Flag("certfile", "Self-signed certificate file for validation").Envar("REDIS_CERTFILE").File()
rediscertb64 = kingpin.Flag("certb64", "Self-signed certificate string as base64 for validation").Envar("REDIS_CERTB64").String()
forceraw = kingpin.Flag("raw", "Produce raw output").Bool()
commandargs = kingpin.Arg("commands", "Redis commands and values").Strings()
)

var (
rawrediscommands = Commands{}
conn redis.Conn
raw = false
)

func main() {
kingpin.Parse()

if *forceraw {
raw = true
} else {
if !isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()) {
raw = true
}
}

cert := []byte{}

if *rediscertfile != nil {
Expand Down Expand Up @@ -115,23 +126,14 @@ func main() {
log.Fatal(err)
}

switch v := result.(type) {
case redis.Error:
fmt.Printf("%s\n", v.Error())
case int64:
fmt.Printf("%d\n", v)
case string:
fmt.Printf("%s\n", v)
case []byte:
fmt.Printf("%s\n", string(v))
case nil:
fmt.Printf("nil\n")
case []interface{}:
for i, j := range v {
fmt.Printf("%d) %s\n", i+1, j)
}
forceraw := false

if strings.ToLower(command[0]) == "info" {
forceraw = true
}

printRedisResult(result, forceraw)

os.Exit(0)
}

Expand Down Expand Up @@ -185,6 +187,8 @@ func main() {
})

for {
forceraw := false

line, err := liner.Prompt(getPrompt())
if err != nil {
break
Expand Down Expand Up @@ -233,30 +237,84 @@ func main() {
break
}

if strings.ToLower(parts[0]) == "info" {
forceraw = true
}

var args = make([]interface{}, len(parts[1:]))
for i, d := range parts[1:] {
args[i] = d
}

result, err := conn.Do(parts[0], args...)

switch v := result.(type) {
case redis.Error:
fmt.Printf("%s\n", v.Error())
case int64:
fmt.Printf("%d\n", v)
case string:
fmt.Printf("%s\n", v)
case []byte:
fmt.Printf("%s\n", string(v))
case nil:
fmt.Printf("nil\n")
case []interface{}:
printRedisResult(result, forceraw)
}
}

func printRedisResult(result interface{}, forceraw bool) {
printRedisResultIndenting(result, "", forceraw)
}

func printRedisResultIndenting(result interface{}, prefix string, forceraw bool) {
switch v := result.(type) {
case []interface{}:
if raw || forceraw {
for _, j := range v {
switch vt := j.(type) {
case []interface{}:
printRedisResultIndenting(vt, "", forceraw)
default:
fmt.Printf("%s\n", toRedisValueString(vt, forceraw))
}
}
} else {
spacer := strings.Repeat(" ", len(prefix))
for i, j := range v {
fmt.Printf("%d) %s\n", i+1, j)
switch vt := j.(type) {
case []interface{}:
newprefix := fmt.Sprintf("%s %d)", prefix, i+1)
printRedisResultIndenting(vt, newprefix, forceraw)
default:
if i == 0 {
fmt.Printf("%s %d) %s\n", prefix, i+1, toRedisValueString(j, forceraw))
} else {
fmt.Printf("%s %d) %s\n", spacer, i+1, toRedisValueString(j, forceraw))
}
}
}
}
default:
fmt.Printf("%s\n", toRedisValueString(result, forceraw))
}
}

func toRedisValueString(value interface{}, forceraw bool) string {
switch v := value.(type) {
case redis.Error:
if raw || forceraw {
return fmt.Sprintf("%s", v.Error())
} else {
return fmt.Sprintf("(error) %s", v.Error())
}
case int64:
if raw || forceraw {
return fmt.Sprintf("%d", v)
} else {
return fmt.Sprintf("(integer) %d", v)
}
case string:
return fmt.Sprintf("%s", v)
case []byte:
if raw || forceraw {
return fmt.Sprintf("%s", string(v))
} else {
return fmt.Sprintf("\"%s\"", string(v))
}
case nil:
return "nil"
}
return ""
}

func redisParseInfo(reply string) map[string]string {
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/mattn/go-isatty/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/mattn/go-isatty/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions vendor/github.com/mattn/go-isatty/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/github.com/mattn/go-isatty/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db51836

Please sign in to comment.