-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullyear-cli.go
74 lines (62 loc) · 2.09 KB
/
fullyear-cli.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
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
)
const apiURL = "https://getfullyear.com/api/year"
type YearResponse struct {
Year int `json:"year"`
YearString string `json:"year_string"`
SponsoredBy string `json:"sponsored_by"`
}
func main() {
// Define flags
jsonOutput := flag.Bool("json", false, "Output in JSON format")
copyright := flag.String("copyright", "", "Print copyright message with the specified value")
flag.Parse()
// Fetch and display year data
fetchYearData(*jsonOutput, *copyright)
}
func fetchYearData(jsonOutput bool, copyright string) {
resp, err := http.Get(apiURL)
if err != nil {
fmt.Printf("Error fetching data: %s\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Error: received status code %d\n", resp.StatusCode)
return
}
var data YearResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
fmt.Printf("Error decoding response: %s\n", err)
return
}
console := struct {
log func(string)
}{
log: func(message string) {
fmt.Println(message)
},
}
console.log(data.SponsoredBy)
if jsonOutput {
output, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Printf("Error formatting JSON: %s\n", err)
return
}
fmt.Println(string(output))
} else {
fmt.Println("\n--- Year Data ---")
fmt.Printf("Year: %d\n", data.Year)
fmt.Printf("Year String: %s\n", data.YearString)
//fmt.Printf("Sponsored By: %s\n", data.SponsoredBy)
}
if copyright != "" {
fmt.Printf("\nCopyright © %d %s\n", data.Year, copyright)
}
}