summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/releases/cli.go
blob: df551716eaa53f3d90a4051e3ef887b09300899a (plain)
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
package releases

import (
	"context"
	"time"

	log "github.com/authzed/spicedb/internal/logging"

	"github.com/jzelinskie/cobrautil/v2"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
)

// RegisterFlags registers the flags for the CheckAndLogRunE function.
func RegisterFlags(flagset *flag.FlagSet) {
	flagset.Bool("skip-release-check", false, "if true, skips checking for new SpiceDB releases")
}

// CheckAndLogRunE is a run function that checks if the current version of SpiceDB is the latest
// and, if not, logs a warning. This check is disabled by setting --skip-release-check=false.
func CheckAndLogRunE() cobrautil.CobraRunFunc {
	return func(cmd *cobra.Command, args []string) error {
		skipReleaseCheck := cobrautil.MustGetBool(cmd, "skip-release-check")
		if skipReleaseCheck {
			return nil
		}

		ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
		defer cancel()

		state, currentVersion, release, err := CheckIsLatestVersion(ctx, CurrentVersion, GetLatestRelease)
		if err != nil {
			log.Ctx(ctx).Warn().Str("this-version", currentVersion).Err(err).Msg("could not perform version checking; if this problem persists or to skip this check, add --skip-release-check=true")
			return nil
		}

		switch state {
		case UnreleasedVersion:
			log.Ctx(ctx).Warn().Str("version", currentVersion).Msg("not running a released version of SpiceDB")
			return nil

		case UpdateAvailable:
			if release == nil {
				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
				return nil
			}

			log.Ctx(ctx).Warn().Str("this-version", currentVersion).Str("latest-released-version", release.Version).Msgf("this version of SpiceDB is out of date. See: %s", release.ViewURL)
			return nil

		case UpToDate:
			if release == nil {
				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
				return nil
			}

			log.Ctx(ctx).Info().Str("latest-released-version", release.Version).Msg("this is the latest released version of SpiceDB")
			return nil

		case Unknown:
			if release == nil {
				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
				return nil
			}

			log.Ctx(ctx).Warn().Str("unknown-released-version", release.Version).Msg("unable to check for a new SpiceDB version")
			return nil

		default:
			panic("Unknown state for CheckAndLogRunE")
		}
	}
}