-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
117 lines (112 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bytes"
"compress/flate"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli/v2"
)
func main() {
app := cli.NewApp()
app.Name = "escort"
app.Commands = cli.Commands{
&cli.Command{
Name: "compress",
Usage: "compress data using DEFLATE and base64 encode it",
Action: func(c *cli.Context) error {
var data string
if c.String("input") != "" {
data = c.String("input")
} else if c.String("input.file") != "" {
dBytes, err := ioutil.ReadFile(c.String("input.file"))
if err != nil {
return err
}
data = string(dBytes)
} else {
return errors.New("input.file and input are nil")
}
buffer := new(bytes.Buffer)
writer, err := flate.NewWriter(buffer, flate.BestCompression)
if err != nil {
return err
}
if _, err := writer.Write([]byte(data)); err != nil {
return err
}
if err := writer.Close(); err != nil {
return err
}
parts := Chunks(base64.StdEncoding.EncodeToString(buffer.Bytes()), 250)
for i, part := range parts {
fmt.Printf("%v|%s\n", i, part)
}
return nil
},
},
&cli.Command{
Name: "base64",
Usage: "base64 encode/decode util commands",
Subcommands: cli.Commands{
&cli.Command{
Name: "encode",
Usage: "base64 encode input data",
Action: func(c *cli.Context) error {
fmt.Println(base64.StdEncoding.EncodeToString([]byte(c.String("input"))))
return nil
},
},
&cli.Command{
Name: "decode",
Usage: "base64 decode input data",
Action: func(c *cli.Context) error {
data, err := base64.StdEncoding.DecodeString(c.String("input"))
if err != nil {
return err
}
fmt.Println(string(data))
return nil
},
},
},
},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "input",
Usage: "input data to compress",
},
&cli.StringFlag{
Name: "input.file",
Usage: "input data to compress from a file",
},
}
if err := app.Run(os.Args); err != nil {
panic(err)
}
}
// Chunks is used to split a string into segments of chunkSize
// https://stackoverflow.com/questions/25686109/split-string-by-length-in-golang
func Chunks(s string, chunkSize int) []string {
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string
chunk := make([]rune, chunkSize)
len := 0
for _, r := range s {
chunk[len] = r
len++
if len == chunkSize {
chunks = append(chunks, string(chunk))
len = 0
}
}
if len > 0 {
chunks = append(chunks, string(chunk[:len]))
}
return chunks
}