-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
132 lines (109 loc) · 2.34 KB
/
cmd.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bufio"
"fmt"
"github.com/pkg/errors"
"io"
"net"
"os"
"strings"
)
func runCommand() error {
if flags.version {
fmt.Println(gitTag)
return nil
} else if flags.list {
for _, item := range emojiData {
if len(item.Emoji) == 4 {
fmt.Printf("%x%x:%x%x %s\n", item.Emoji[0], item.Emoji[1], item.Emoji[2], item.Emoji[3], item.Emoji)
}
}
return nil
} else if isInputFromPipe() {
if flags.reverse != "" {
return toEmoji(os.Stdin, os.Stdout)
} else {
return toIPv6(os.Stdin, os.Stdout)
}
} else if flags.reverse != "" {
return toEmoji(strings.NewReader(flags.reverse), os.Stdout)
} else if inputData != "" {
return toIPv6(strings.NewReader(inputData), os.Stdout)
}
return errors.New("no valid operations.")
}
func isInputFromPipe() bool {
fi, _ := os.Stdin.Stat()
return fi.Mode()&os.ModeCharDevice == 0
}
func processConvertEmoji(s string) (string, error) {
var rawData net.IP
rawData = net.ParseIP(s)
if rawData == nil {
return "", errors.New("Invaild IPv6 address")
}
Contains := func(s string) bool {
for _, item := range emojiData {
if item.Emoji == s {
return true
}
}
return false
}
for i := 0; i < 4; i++ {
emojiValue := rawData[(i * 4):(i*4 + 4)]
if !Contains(string(emojiValue)) {
return "", errors.New("There are no convertible emojis.")
}
}
return string(rawData), nil
}
func processConvertIPv6(s string) (string, error) {
if len(s) != 16 {
return "", errors.New("Invalid length of emojis.")
}
var ipv6_addr net.IP
ipv6_addr = []byte(s)
return ipv6_addr.String(), nil
}
func toIPv6(r io.Reader, w io.Writer) error {
scanner := bufio.NewScanner(bufio.NewReader(r))
for scanner.Scan() {
var emojis string
var e error
emojis, e = processConvertIPv6(scanner.Text())
if e != nil {
return e
}
_, e = fmt.Fprintln(
w, emojis)
if e != nil {
return e
}
}
return nil
}
func toEmoji(r io.Reader, w io.Writer) error {
scanner := bufio.NewScanner(bufio.NewReader(r))
for scanner.Scan() {
var ipv6_addr string
var e error
ipv6_addr, e = processConvertEmoji(scanner.Text())
if e != nil {
return e
}
_, e = fmt.Fprintln(
w, ipv6_addr)
if e != nil {
return e
}
}
return nil
}
func fileExists(filepath string) bool {
info, e := os.Stat(filepath)
if os.IsNotExist(e) {
return false
}
return !info.IsDir()
}