/
check.go
164 lines (139 loc) · 4.16 KB
/
check.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
/*
Copyright © 2023 Filip Troníček
*/
package cmd
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
type SoftwareVersion struct {
Cycle string `json:"cycle"`
ReleaseDate string `json:"releaseDate"`
Support interface{} `json:"support"`
EOL string `json:"eol"`
Latest string `json:"latest"`
LatestReleaseDate string `json:"latestReleaseDate"`
LTS bool `json:"lts"`
}
type Variant struct {
Name string `yaml:"name"`
Args map[string]string `yaml:"args"`
}
type Chunk struct {
Variants []Variant `yaml:"variants"`
}
func capitalize(word string) string {
if len(word) == 0 {
return ""
}
return strings.ToUpper(string(word[0])) + word[1:]
}
func CheckVersion(name string, version string) (SoftwareVersion, error) {
httpClient := &http.Client{}
req, err := http.NewRequest("GET", "https://endoflife.date/api/"+name+".json", nil)
if err != nil {
return SoftwareVersion{}, err
}
req.Header.Set("User-Agent", "date-reaper-cli")
resp, err := httpClient.Do(req)
if err != nil {
return SoftwareVersion{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return SoftwareVersion{}, fmt.Errorf("Error: Server returned status %d", resp.StatusCode)
}
var versions []SoftwareVersion
if err := json.NewDecoder(resp.Body).Decode(&versions); err != nil {
return SoftwareVersion{}, err
}
for _, v := range versions {
if v.Cycle == version {
return v, nil
}
}
return SoftwareVersion{}, errors.New("Version not found")
}
var tool string
var checkChunkCmd = &cobra.Command{
Use: "check-chunk <path-to-chunk.yaml>",
Long: "Checks a chunk.yaml's variants for those which are EOL'd",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
chunkPath := args[0]
chunkFile, err := os.ReadFile(chunkPath)
if err != nil {
return fmt.Errorf("Error reading chunk file: %s", err)
}
var chunk Chunk
if err := yaml.Unmarshal(chunkFile, &chunk); err != nil {
return fmt.Errorf("Error parsing YAML: %s", err)
}
for _, variant := range chunk.Variants {
version := variant.Name
v, err := CheckVersion(tool, variant.Name)
if err != nil {
fmt.Printf("Error checking version %s: %s\n", version, err)
continue
}
now := time.Now().Format("2006-01-02")
if v.EOL <= now {
fmt.Printf("Version %s is EOL since %s. Support ended on: %s\n", version, v.EOL, v.Support)
} else {
fmt.Printf("Version %s is not EOL yet. It will be EOL on %s.\n", version, v.EOL)
}
}
return nil
},
}
var failOnMissing bool
var failOnUnsupported bool
// checkCmd represents the check command
var checkCmd = &cobra.Command{
Use: "check <name> <version>",
Short: "Check if a software version is EOL",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
name, version := args[0], args[1]
v, err := CheckVersion(name, version)
if err != nil {
return err
}
var supportEndDate string
switch supportValue := v.Support.(type) {
case string:
supportEndDate = supportValue
case bool:
if !supportValue {
supportEndDate = "No Support"
}
default:
supportEndDate = "Unknown"
}
now := time.Now().Format("2006-01-02")
if v.EOL > now {
if failOnUnsupported {
return fmt.Errorf("%s %s is not supported anymore", capitalize(name), version)
}
fmt.Printf("%s %s is not EOL yet. It will be EOL on %s. Support ends on %s\n", capitalize(name), version, v.EOL, supportEndDate)
return nil
} else {
fmt.Printf("%s %s is EOL since %s. Support ended on: %s\n", capitalize(name), version, v.EOL, supportEndDate)
return errors.New("EOL")
}
},
}
func init() {
rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(checkChunkCmd)
checkCmd.Flags().BoolVarP(&failOnMissing, "fail-on-missing", "m", false, "Fail if the version is not found in the database")
checkCmd.Flags().BoolVarP(&failOnUnsupported, "fail-on-unsupported", "u", false, "Fail if the version is not supported by regular updates anymore")
checkChunkCmd.Flags().StringVarP(&tool, "tool", "t", "", "Tool to check versions for")
}