11package main
22
33import (
4- "errors"
5- "flag"
64 "fmt"
75 "github.com/Masterminds/semver/v3"
6+ "github.com/pmoscode/go-common/shutdown"
87 chart2 "github.com/pmoscode/helm-chart-update-check/pkg/chart"
8+ "github.com/pmoscode/helm-chart-update-check/pkg/cli"
99 "github.com/pmoscode/helm-chart-update-check/pkg/dockerhub"
10+ "github.com/pmoscode/helm-chart-update-check/pkg/utils"
1011 "log"
11- "strings"
1212)
1313
14- type CliOptions struct {
15- dockerHubRepository * string
16- helmChartPath * string
17- failOnExistingUpdate * bool
18- debug * bool
19- }
20-
21- func getCliOptionsParameters () * CliOptions {
22- dockerHubRepository := flag .String ("docker-hub-repo" , "" , "DockHub repo to check tag versions" )
23- helmChartPath := flag .String ("helm-chart-path" , "." , "Helm chart to check for updates" )
24- failOnExistingUpdate := flag .Bool ("fail-on-update" , false , "Return exit code 1, if update is available" )
25- debug := flag .Bool ("debug" , false , "Enable debug outputs" )
26-
27- flag .Parse ()
28-
29- return & CliOptions {
30- dockerHubRepository : dockerHubRepository ,
31- helmChartPath : helmChartPath ,
32- failOnExistingUpdate : failOnExistingUpdate ,
33- debug : debug ,
34- }
35- }
36-
3714func main () {
38- cliOptions := getCliOptions ()
15+ defer shutdown . ExitOnPanic ()
3916
40- dockerVersions := getDockerVersions ( cliOptions )
17+ cliOptions := cli . GetCliOptions ( )
4118
42- chartVersion := getChartVersion (cliOptions )
19+ dockerVersions := dockerhub .FetchDockerVersions (cliOptions )
20+ chartVersion := chart2 .FetchChartVersion (cliOptions )
4321
4422 _ , err := checkVersion (chartVersion , dockerVersions , cliOptions )
4523 if err != nil {
4624 log .Fatalln (err )
4725 }
4826}
4927
50- func getCliOptions () * CliOptions {
51- cliOptions := getCliOptionsParameters ()
52-
53- if * cliOptions .dockerHubRepository == "" {
54- log .Fatal (errors .New ("parameter 'docker-hub-repo' is required" ))
55- }
56- if * cliOptions .helmChartPath == "" {
57- log .Fatal (errors .New ("parameter 'helm-chart-path' is required" ))
58- }
59-
60- return cliOptions
61- }
62-
63- func getDockerVersions (cliOptions * CliOptions ) []* semver.Version {
64- dockerHub := dockerhub .CreateDockerHub (* cliOptions .dockerHubRepository , * cliOptions .debug )
65- versions := dockerHub .GetVersions ()
66-
67- return versions
68- }
69-
70- func getChartVersion (cliOptions * CliOptions ) * semver.Version {
71- chart := chart2 .NewChart (* cliOptions .helmChartPath )
72-
73- appVersion := strings .Trim (chart .AppVersion (), "\" " )
74- fmt .Println ("Helm chart AppVersion:" )
75- fmt .Println (appVersion )
76-
77- v , err := semver .NewVersion (appVersion )
78- if err != nil {
79- log .Fatal ("Problem creating semver: " , err )
80- }
81- return v
82- }
83-
84- func checkVersion (chartVersion * semver.Version , dockerVersions []* semver.Version , cliOptions * CliOptions ) (int , error ) {
85- constraintStr := fmt .Sprintf ("<= %s-0" , chartVersion .IncPatch ().String ())
28+ func checkVersion (chartVersion * semver.Version , dockerVersions []* semver.Version , cliOptions * cli.Options ) (int , error ) {
8629 // See: https://github.com/Masterminds/semver?tab=readme-ov-file#working-with-prerelease-versions
87- constraint , err := semver .NewConstraint (constraintStr )
30+ constraint , err := semver .NewConstraint (fmt . Sprintf ( "<= %s-0" , chartVersion . IncPatch (). String ()) )
8831 if err != nil {
8932 log .Fatalln (err )
9033 }
9134
9235 newerVersions := make ([]* semver.Version , 0 )
36+ excludeVersions := utils .GetExcludedVersionsSimple (* cliOptions .ExcludeVersions )
9337
9438 fmt .Printf ("Checking, if some version is > %s\n " , chartVersion .String ())
9539 for _ , item := range dockerVersions {
96- if * cliOptions .debug {
40+ if * cliOptions .Debug {
9741 fmt .Printf ("Checking if Helm chart version %v is >= DockerHub version %v: " , chartVersion .Original (), item .Original ())
9842 }
99- if ! constraint .Check (item ) {
100- newerVersions = append (newerVersions , item )
101- if * cliOptions .debug {
102- fmt .Println (false )
43+
44+ skipVersion := false
45+ if excludeVersions != nil {
46+ for _ , version := range excludeVersions {
47+ constraintExclude , err := semver .NewConstraint (fmt .Sprintf ("%s" , version ))
48+ if err != nil {
49+ log .Fatalln (err )
50+ }
51+
52+ if constraintExclude .Check (item ) {
53+ skipVersion = true
54+ break
55+ }
10356 }
104- } else {
105- if * cliOptions .debug {
106- fmt .Println (true )
57+ }
58+
59+ if ! skipVersion {
60+ if ! constraint .Check (item ) {
61+ newerVersions = append (newerVersions , item )
62+ if * cliOptions .Debug {
63+ fmt .Println (false )
64+ }
65+ } else {
66+ if * cliOptions .Debug {
67+ fmt .Println (true )
68+ }
10769 }
70+ } else {
71+ fmt .Println ("skipped" )
10872 }
10973 }
11074
@@ -116,7 +80,7 @@ func checkVersion(chartVersion *semver.Version, dockerVersions []*semver.Version
11680 fmt .Println (item .Original ())
11781 }
11882
119- if * cliOptions .failOnExistingUpdate {
83+ if * cliOptions .FailOnExistingUpdate {
12084 return newerVersionsCnt , fmt .Errorf ("FAIL: Found %d new versions" , newerVersionsCnt )
12185 }
12286 } else {
0 commit comments