-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (114 loc) · 2.77 KB
/
Copy pathmain.go
File metadata and controls
147 lines (114 loc) · 2.77 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
)
var output string
var isString bool
func main() {
var rootCmd = &cobra.Command{
Use: "osaka-codec",
Short: "Encode your data with osaka-codec",
}
var encodeCmd = &cobra.Command{
Use: "encode",
Short: "Encode data with osaka-codec",
Args: cobra.MinimumNArgs(1),
RunE: encode,
}
var decodeCmd = &cobra.Command{
Use: "decode",
Short: "Decode data with osaka-codec",
Args: cobra.MinimumNArgs(1),
RunE: decode,
}
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "Specify an output file to save the result. (Only on file encoding/decoding)")
rootCmd.PersistentFlags().BoolVarP(&isString, "string", "s", false, "Specify that the input arguments are a string rather than a file path.")
rootCmd.AddCommand(encodeCmd, decodeCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Print(err)
}
}
func encode(cmd *cobra.Command, args []string) error {
if len(args) > 1 || isString {
return encodeString(cmd, args)
}
input := args[0]
file, err := os.Open(input)
if os.IsNotExist(err) {
return fmt.Errorf("file '%s' does not exist", input)
}
b, readErr := io.ReadAll(file)
if readErr != nil {
return readErr
}
encoded, encodeErr := Encode(b)
if encodeErr != nil {
return encodeErr
}
if output == "" {
output = input + ".osaka"
}
newFile, newErr := os.OpenFile(output, os.O_CREATE|os.O_RDWR, 0644)
if newErr != nil {
return newErr
}
_, writeErr := newFile.Write([]byte(encoded))
if writeErr != nil {
return writeErr
}
fmt.Printf("Encoded your data '%s'\n", output)
return nil
}
func decode(cmd *cobra.Command, args []string) error {
if len(args) > 1 || isString {
return decodeString(cmd, args)
}
input := args[0]
file, err := os.Open(input)
if os.IsNotExist(err) {
return fmt.Errorf("file '%s' does not exist", input)
}
b, readErr := io.ReadAll(file)
if readErr != nil {
return readErr
}
decoded, decodeErr := Decode(string(b))
if decodeErr != nil {
return decodeErr
}
if output == "" {
output = input + ".decoded_osaka"
}
newFile, newErr := os.OpenFile(output, os.O_CREATE|os.O_RDWR, 0644)
if newErr != nil {
return newErr
}
_, writeErr := newFile.Write([]byte(decoded))
if writeErr != nil {
return writeErr
}
fmt.Printf("Decoded your data '%s'\n", output)
return nil
}
func encodeString(_ *cobra.Command, args []string) error {
text := strings.Join(args, " ")
encoded, decodeErr := Encode([]byte(text))
if decodeErr != nil {
return decodeErr
}
fmt.Printf("%s\n", encoded)
return nil
}
func decodeString(_ *cobra.Command, args []string) error {
text := strings.Join(args, " ")
decoded, decodeErr := Decode(text)
if decodeErr != nil {
return decodeErr
}
fmt.Printf("%s\n", decoded)
return nil
}