Skip to content

Commit

Permalink
resolves #7 add an encode command (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 6, 2021
1 parent 31f9fdc commit 53f6945
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
48 changes: 48 additions & 0 deletions cmd/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/yuzutech/kroki-go"
)

func Encode(_ *cobra.Command, args []string) {
filePath := args[0]
if filePath == "-" {
reader := bufio.NewReader(os.Stdin)
EncodeFromReader(reader)
} else {
EncodeFromFile(filePath)
}
}

func EncodeFromReader(reader io.Reader) {
text, err := GetTextFromReader(reader)
if err != nil {
exit(err)
}
result, err := kroki.CreatePayload(text)
if err != nil {
exit(err)
}
fmt.Println(result)
}

func EncodeFromFile(filePath string) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
exit(errors.Wrapf(err, "fail to read file '%s'", filePath))
}
input := string(content)
result, err := kroki.CreatePayload(input)
if err != nil {
exit(err)
}
fmt.Println(result)
}
18 changes: 18 additions & 0 deletions cmd/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"bytes"
"testing"
)

func TestEncodeFromReader(t *testing.T) {
buf := bytes.NewBuffer([]byte(""))
buf.Write([]byte("digraph G {Hello->World}"))
result := CaptureOutput(func() {
EncodeFromReader(buf)
})
expected := "eNpKyUwvSizIUHBXqPZIzcnJ17ULzy_KSakFBAAA__9sQAjG\n"
if result != expected {
t.Errorf("EncodeFromReader error\nexpected: %s\nactual: %s", expected, result)
}
}
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var convertCmd = &cobra.Command{
Run: Convert,
}

var encodeCmd = &cobra.Command{
Use: "encode file",
Short: "Encode text diagram in deflate + base64 format",
Args: cobra.ExactArgs(1),
Run: Encode,
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of kroki",
Expand All @@ -48,6 +55,7 @@ func init() {
convertCmd.PersistentFlags().StringP("out-file", "o", "", "output file (default: based on path of input file); use - to output to STDOUT")
RootCmd.AddCommand(versionCmd)
RootCmd.AddCommand(convertCmd)
RootCmd.AddCommand(encodeCmd)

SetupConfig()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ require (
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.1
github.com/yuzutech/kroki-go v0.4.0
github.com/yuzutech/kroki-go v0.5.0
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ github.com/yuzutech/kroki-go v0.3.0 h1:GIQv3jVQF8GNl6k+el55pksFeV7mJ9dQr9wPjyNj6
github.com/yuzutech/kroki-go v0.3.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
github.com/yuzutech/kroki-go v0.4.0 h1:2w2Vxd7yleXB29vZUL7UVZ2RFL3UfTgYwd4I6poNMIM=
github.com/yuzutech/kroki-go v0.4.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
github.com/yuzutech/kroki-go v0.5.0 h1:XWmxmG22/7PXsqt1TihYjYqmAcs3AO3JtvuwGF20Ehs=
github.com/yuzutech/kroki-go v0.5.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
github.com/yuzutech/kroki-go v0.5.1-0.20201222133542-63f58a4e26a5 h1:Kldh+1bMl7HqpW7AgW0NLS/A7zSptl6HTAgMbuVVCnk=
github.com/yuzutech/kroki-go v0.5.1-0.20201222133542-63f58a4e26a5/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
github.com/yuzutech/kroki-go v0.5.1-0.20201222134200-cec1f2086fdf h1:WCRl6tzkG4IEM5MHvywsCwoUDP6DyE6tHlQD9AizVuU=
github.com/yuzutech/kroki-go v0.5.1-0.20201222134200-cec1f2086fdf/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
github.com/yuzutech/kroki-go v0.6.0 h1:0BNsiwS/xFupdXzaz4MTMzyfYuqoK/RXiFff1YMkWM4=
github.com/yuzutech/kroki-go v0.6.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit 53f6945

Please sign in to comment.