-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
90 lines (70 loc) · 1.69 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
package main
import (
"flag"
"fmt"
"github.com/tofl/pngify/image"
"os"
"time"
)
func main() {
encodeCmd := flag.NewFlagSet("encode", flag.ExitOnError)
encodeText := encodeCmd.String("t", "", "Text to encode")
encodeFile := encodeCmd.String("f", "", "File to encode")
decodeCmd := flag.NewFlagSet("decode", flag.ExitOnError)
filePath := decodeCmd.String("p", "", "Path to the file to decode")
if len(os.Args) < 2 {
fmt.Println("A command must be specified.")
os.Exit(1)
}
executionTime := time.Now()
switch os.Args[1] {
case "encode":
_ = encodeCmd.Parse(os.Args[2:])
var img *image.Image
if *encodeText != "" {
// Encode text
img = image.NewImage([]byte(*encodeText))
} else if *encodeFile != "" {
content, err := os.ReadFile(*encodeFile)
if err != nil {
fmt.Println("Couldn't read file", *encodeFile)
os.Exit(1)
}
fmt.Println("Encoding file...")
img = image.NewImage(content)
img.MakeText([]byte("filename"), []byte(*encodeFile))
}
img.MakeImage()
break
case "decode":
_ = decodeCmd.Parse(os.Args[2:])
f, err := os.Open(*filePath)
defer f.Close()
if err != nil {
fmt.Println("Couldn't open the file")
os.Exit(1)
}
fmt.Println("Decoding image...")
data, fileName := image.Decode(f)
if fileName == "" {
fmt.Println(data)
} else {
// Save file
f, err := os.Create(fileName)
defer f.Close()
if err != nil {
panic("Couldn't create the output file.")
}
_, err = f.Write([]byte(data))
if err != nil {
panic("Couldn't create the output file.")
}
}
break
default:
fmt.Println("Command not recognised")
os.Exit(1)
}
elapsed := time.Since(executionTime)
fmt.Println("Done in", elapsed)
}