-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (69 loc) · 1.76 KB
/
main.go
File metadata and controls
87 lines (69 loc) · 1.76 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
// TODO: document
package main
import (
"github.com/idagio/poethe/poethe"
"flag"
"fmt"
"os"
"time"
)
const (
defaultInputRGB = "#ef476f"
defaultGeneratorID = "random"
)
func main() {
inputColorRGB := flag.String("in", defaultInputRGB, "CSV list of RGB values")
generatorID := flag.String("gen", defaultGeneratorID, "ID for the color generator")
flag.Parse()
if inputColorRGB == nil {
fmt.Printf("Please provide a CSV of RGB values\n")
os.Exit(1)
}
if generatorID == nil {
fmt.Printf("Please provide an ID for the color generator\n")
os.Exit(1)
}
// TODO: check if the inputColorRGB is not set to nil, get rid of panic in the following function
colors, err := poethe.ParseColors(*inputColorRGB)
if err != nil {
fmt.Printf("Could not parse color codes: %v\n", err)
os.Exit(1)
}
config := &appConfiguration{
InputColorRGB: *inputColorRGB,
}
if _, err := validConfig(config); err != nil {
fmt.Printf("Invalid input: %v\n", err)
os.Exit(1)
}
generators := buildGenerators()
gen, ok := generators[*generatorID]
if !ok {
fmt.Printf("No generator found for key: %s\n", *generatorID)
os.Exit(1)
}
palette := gen.Generate(colors)
// Generate the colors
formattedOutput := poethe.FormatColors(palette)
fmt.Printf("%s", formattedOutput)
}
type appConfiguration struct {
InputColorRGB string
}
func validConfig(c *appConfiguration) (bool, error) {
if c == nil {
panic(fmt.Errorf("No configuration provided"))
}
if len(c.InputColorRGB) == 0 {
return false, fmt.Errorf("No input color RGB provided")
}
return true, nil
}
func buildGenerators() map[string]poethe.Generator {
generators := make(map[string]poethe.Generator)
rg := &poethe.RandomGenerator{
Seed: time.Now().UnixNano(),
}
generators["random"] = rg
return generators
}