-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (125 loc) · 5.27 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
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
148
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
g "github.com/poseidon-code/godenticon"
)
func is_flag_passed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {found = true}
})
return found
}
func main() {
// PARSING COMMANDLINE OPTIONS
// identicon configurations
size_ptr := flag.Int( "size", g.IdenticonDefaultOptions.Size, "sets size of the identicon (range: 4-8)")
square_ptr := flag.Bool( "square", g.IdenticonDefaultOptions.Square, "creates a square identicon")
border_ptr := flag.Bool( "border", g.IdenticonDefaultOptions.Border, "adds a border to the identicon")
vertical_ptr := flag.Bool( "vertical", g.IdenticonDefaultOptions.Vertical, "creates identicon in portrait dimension (not visible on using --square flag)")
invert_ptr := flag.Bool( "invert", g.IdenticonDefaultOptions.Invert, "inverts the cell filling of identicon")
symmetric_ptr := flag.Bool( "symmetric", g.IdenticonDefaultOptions.Symmetric, "creates symmetric identicon")
// image configurations
image_size_ptr := flag.String( "image-size", g.ImageDefaultOptions.Size, "saves image with given resolution preset (S,M,L,X)")
image_portrait_ptr := flag.Bool( "image-portrait", g.ImageDefaultOptions.Portrait, "saves image with portrait dimensions")
fg_ptr := flag.String( "fg", g.ImageDefaultOptions.FG, "sets image's foreground color")
bg_ptr := flag.String( "bg", g.ImageDefaultOptions.BG, "sets image's background color")
// if --config path is passed, ignore every other flags
config_ptr := flag.String( "config", "", "path to config.json file")
save_ptr := flag.String( "save", "", "saves image to the specified directory")
hash_ptr := flag.Bool( "hash", false, "allows passing hash directly (instead of text)")
flag.Parse()
var identicon g.Identicon
var identicon_o g.IdenticonConfiguration
var image_o g.ImageConfiguration
// SETTING OPTIONS
if is_flag_passed("config") {
// handle json configs
if flag.NFlag()>1 && !is_flag_passed("save") {
fmt.Println("When --config is passed, all other CLI options will be discarded (except --save).")
}
identicon.ReadConfiguration(*config_ptr)
} else {
// handle commandline options
identicon_o = g.IdenticonConfiguration{
Size: *size_ptr,
Square: *square_ptr,
Border: *border_ptr,
Vertical: *vertical_ptr,
Invert: *invert_ptr,
Symmetric: *symmetric_ptr,
}
image_o = g.ImageConfiguration{
Size: *image_size_ptr,
Portrait: *image_portrait_ptr,
FG: *fg_ptr,
BG: *bg_ptr,
}
identicon.IdenticonOptions = identicon_o
identicon.ImageOptions = image_o
identicon.CheckConfiguration()
}
// SETTING IDENTICON TEXT/HASH
// handling text/hash
if len(flag.Args())>1 {
fmt.Println(
"Invalid sequence of flags & arguments passed.",
"\nUse flags before argument. e.g.: ",
"\nidenticon --size=8 lovely",
)
fmt.Println(); flag.Usage()
os.Exit(1)
} else if len(flag.Args())==0 {
fmt.Println(
"No argument passed for the text. Use like: ",
"\nidenticon lovely",
)
fmt.Println(); flag.Usage()
os.Exit(1)
}
if *hash_ptr {
identicon.Hash = flag.Arg(0)
identicon.CheckHash()
} else {
identicon.Text = flag.Arg(0)
identicon.GenerateHash()
}
// GENERATING IDENTICON
identicon.GenerateMatrix()
// variable `identicon` will now have all the required values for further
// operation on it, like printing or saving image, etc.
// PRINTING
identicon.Print()
// SAVING IDENTICON IMAGE
// checking if any other image related flags are passed except `--save`
// if so, then prompt user to pass `--save` flag also
// else, when no `--save` or any other image related flags are passed, then do nothing.
other_image_flags := false
for _, f := range []string{"image-portrait", "image-size", "fg", "bg"} {
if is_flag_passed(f) {
other_image_flags = true
break
}
}
if is_flag_passed("save") {
// save image only when `--save` flag is passed
_, fileName := filepath.Split(*save_ptr)
parts := strings.Split(fileName, ".")
extension := strings.ToLower(parts[len(parts)-1])
if extension == "png" {
identicon.SaveImage(*save_ptr)
} else if extension == "svg" {
identicon.SaveSVG(*save_ptr)
} else {
identicon.SaveImage(*save_ptr)
identicon.SaveSVG(*save_ptr)
}
} else if other_image_flags {
// if any other image related flags are passed without `--save` flag
fmt.Println("To save image provide --save=<path> flag.")
}
}