-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
164 lines (138 loc) · 4.99 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
// Use pflag instead of flag
import (
"fmt"
"os"
"path"
"strings"
"github.com/kamermans/github-skyline/pkg/skyline"
flag "github.com/spf13/pflag"
)
const (
version = "1.1.2"
)
var (
username string
token string
saveContribs bool
contribsFile string
trimContribs bool
outputFile string
startYear int
endYear int
aspectRatio string
baseAngle float64
baseHeight float64
baseMargin float64
maxBuildingHeight float64
buildingWidth float64
buildingLength float64
interval string
font string
openscadPath string
showVersion bool
showVersionRaw bool
aspectRatioInts [2]int
outputFileType skyline.OutputType
)
func init() {
flag.StringVarP(&username, "username", "u", os.Getenv("GITHUB_USERNAME"), "GitHub username")
flag.StringVarP(&token, "token", "t", os.Getenv("GITHUB_TOKEN"), "GitHub token")
flag.BoolVarP(&saveContribs, "save", "s", false, "Save contributions to a file")
flag.StringVarP(&contribsFile, "contributions", "f", "contributions.json", "File to save/load contributions")
flag.BoolVarP(&trimContribs, "trim", "T", true, "Trim years from the start that contain no contributions")
flag.StringVarP(&outputFile, "output", "o", "skyline.scad", "Output file (.scad and .stl are supported, but stl requires 'openscad')")
flag.IntVarP(&startYear, "start", "b", 0, "Start year")
flag.IntVarP(&endYear, "end", "e", 0, "End year")
flag.StringVarP(&aspectRatio, "aspect-ratio", "a", "16:4", "Aspect ratio of the skyline")
flag.Float64VarP(&baseAngle, "base-angle", "A", 22.5, "Slope of the base walls in degrees")
flag.Float64VarP(&baseHeight, "base-height", "h", 5.0, "Height of the base (mm)")
flag.Float64VarP(&baseMargin, "base-margin", "g", 1.0, "Distance from the buildings to the base walls (mm)")
flag.Float64VarP(&maxBuildingHeight, "max-building-height", "m", 20.0, "Max building height (mm)")
flag.Float64VarP(&buildingWidth, "building-width", "w", 2.0, "Building width (mm)")
flag.Float64VarP(&buildingLength, "building-length", "l", 2.0, "Building length (mm)")
flag.StringVarP(&interval, "interval", "i", "week", "Interval to use for contributions (day, week)")
flag.StringVarP(&font, "font", "F", "Liberation Sans:style=Bold", "Font to use for text")
flag.StringVarP(&openscadPath, "openscad", "O", "openscad", "Path to the OpenSCAD executable")
flag.BoolVarP(&showVersion, "version", "V", false, "Show version")
flag.BoolVar(&showVersionRaw, "version-raw", false, "Show version (raw)")
flag.Parse()
if showVersion {
fmt.Printf("github-skyline %s\n", version)
os.Exit(0)
}
if showVersionRaw {
fmt.Printf("%s\n", version)
os.Exit(0)
}
if contribsFile == "" && !saveContribs && (username == "" || token == "") {
flag.PrintDefaults()
panic("username and token are required")
}
_, err := fmt.Sscanf(aspectRatio, "%d:%d", &aspectRatioInts[0], &aspectRatioInts[1])
if err != nil {
panic(fmt.Errorf("invalid aspect ratio: %s; %w", aspectRatio, err))
}
if interval != "day" && interval != "week" {
panic(fmt.Errorf("invalid interval: %s; must be day or week", interval))
}
if outputFile == "" && !saveContribs {
panic("output file is required unless you are using --save")
}
parts := strings.Split(path.Base(outputFile), ".")
if len(parts) < 2 {
panic("output file must have an extension")
}
outputFileType = skyline.OutputType(parts[len(parts)-1])
if outputFileType != skyline.OutputTypeSCAD && outputFileType != skyline.OutputTypeSTL {
panic("output file must be .scad or .stl")
}
}
func main() {
var err error
var contribs *skyline.Contributions
if contribsFile != "" && !saveContribs {
contribs, err = skyline.NewContributionsFromFile(contribsFile)
if err != nil {
panic(err)
}
} else {
fetcher := skyline.NewGitHubContributionsFetcher(username, token)
contribs, err = fetcher.FetchContributions(startYear, endYear)
if err != nil {
panic(err)
}
if saveContribs {
err = contribs.SaveToFile(contribsFile)
if err != nil {
panic(err)
}
}
}
if trimContribs {
if contribs.TrimStartYear() {
fmt.Printf("Trimmed start year to %v\n", contribs.FirstDate[:4])
}
}
fmt.Printf("Total contributions: %d between %v and %v\n", contribs.TotalContributions, contribs.FirstDate, contribs.LastDate)
fmt.Printf("Generating OpenSCAD ...\n")
sg := skyline.NewSkylineGenerator(*contribs, aspectRatioInts, maxBuildingHeight, buildingWidth, buildingLength, font)
sl := sg.Generate(interval)
sl.BaseAngle = baseAngle
sl.BaseHeight = baseHeight
sl.BaseMargin = baseMargin
if outputFileType == skyline.OutputTypeSCAD {
dur, err := sl.ToOpenSCAD(outputFile)
if err != nil {
panic(err)
}
fmt.Printf("OpenSCAD file %s generated in %v\n", outputFile, dur)
} else if outputFileType == skyline.OutputTypeSTL {
fmt.Printf("Generating STL ...\n")
dur, err := sl.ToSTL(outputFile, openscadPath)
if err != nil {
panic(err)
}
fmt.Printf("STL file written to %s in %v\n", outputFile, dur)
}
}