-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
7f221a9
commit e1b491e
Showing
4 changed files
with
131 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"compress/zlib" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Decode(_ *cobra.Command, args []string) { | ||
input := args[0] | ||
if input == "-" { | ||
reader := bufio.NewReader(os.Stdin) | ||
DecodeFromReader(reader) | ||
} else { | ||
DecodeFromInput(input) | ||
} | ||
} | ||
|
||
func DecodeFromReader(reader io.Reader) { | ||
text, err := GetTextFromReader(reader) | ||
if err != nil { | ||
exit(err) | ||
} | ||
result, err := DecodeInput(text) | ||
if err != nil { | ||
exit(err) | ||
} | ||
fmt.Println(result) | ||
} | ||
|
||
func DecodeFromInput(input string) { | ||
result, err := DecodeInput(input) | ||
if err != nil { | ||
exit(err) | ||
} | ||
fmt.Println(result) | ||
} | ||
|
||
// takes a string encoded using deflate + base64 format and returns a decoded string | ||
func DecodeInput(input string) (string, error) { | ||
// special case to extract the encoded diagram from a URL (GET request) | ||
// expected format is: https://kroki.io/diagram/format/encoded | ||
if strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") { | ||
inputUrl, _ := getUrl(input) | ||
if inputUrl != nil { | ||
// get the last part of the URL | ||
input = path.Base(inputUrl.Path) | ||
} | ||
} | ||
result, err := base64.URLEncoding.DecodeString(input) | ||
if err != nil { | ||
return "", errors.Wrap(err, "fail to decode the input") | ||
} | ||
reader, err := zlib.NewReader(bytes.NewReader(result)) | ||
if err != nil { | ||
return "", errors.Wrap(err, "fail to create the reader") | ||
} | ||
out := new(strings.Builder) | ||
_, _ = io.Copy(out, reader) | ||
return out.String(), nil | ||
} | ||
|
||
func getUrl(input string) (*url.URL, error) { | ||
_, err := url.ParseRequestURI(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
u, err := url.Parse(input) | ||
if err != nil || u.Scheme == "" || u.Host == "" { | ||
return u, errors.New("invalid URL") | ||
} | ||
|
||
return u, nil | ||
} |
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,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestDecodeFromReader(t *testing.T) { | ||
cases := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
input: "eNpKyUwvSizIUHBXqPZIzcnJ17ULzy_KSakFBAAA__9sQAjG", | ||
expected: "digraph G {Hello->World}\n", | ||
}, | ||
{ | ||
input: "https://kroki.io/graphviz/svg/eNpKyUwvSizIUHBXqPZIzcnJ17ULzy_KSakFBAAA__9sQAjG", | ||
expected: "digraph G {Hello->World}\n", | ||
}, | ||
{ | ||
input: "http://localhost:8000/graphviz/svg/eNpKyUwvSizIUHBXqPZIzcnJ17ULzy_KSakFBAAA__9sQAjG", | ||
expected: "digraph G {Hello->World}\n", | ||
}, | ||
} | ||
for _, c := range cases { | ||
buf := bytes.NewBuffer([]byte("")) | ||
buf.Write([]byte(c.input)) | ||
result := CaptureOutput(func() { | ||
DecodeFromReader(buf) | ||
}) | ||
if result != c.expected { | ||
t.Errorf("DecodeFromReader error\nexpected: %s\nactual: %s", c.expected, result) | ||
} | ||
} | ||
} |
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