github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/releases/cli.go (about)

     1  package releases
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	log "github.com/authzed/spicedb/internal/logging"
     8  
     9  	"github.com/jzelinskie/cobrautil/v2"
    10  	"github.com/spf13/cobra"
    11  	flag "github.com/spf13/pflag"
    12  )
    13  
    14  // RegisterFlags registers the flags for the CheckAndLogRunE function.
    15  func RegisterFlags(flagset *flag.FlagSet) {
    16  	flagset.Bool("skip-release-check", false, "if true, skips checking for new SpiceDB releases")
    17  }
    18  
    19  // CheckAndLogRunE is a run function that checks if the current version of SpiceDB is the latest
    20  // and, if not, logs a warning. This check is disabled by setting --skip-release-check=false.
    21  func CheckAndLogRunE() cobrautil.CobraRunFunc {
    22  	return func(cmd *cobra.Command, args []string) error {
    23  		skipReleaseCheck := cobrautil.MustGetBool(cmd, "skip-release-check")
    24  		if skipReleaseCheck {
    25  			return nil
    26  		}
    27  
    28  		ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    29  		defer cancel()
    30  
    31  		state, currentVersion, release, err := CheckIsLatestVersion(ctx, CurrentVersion, GetLatestRelease)
    32  		if err != nil {
    33  			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")
    34  			return nil
    35  		}
    36  
    37  		switch state {
    38  		case UnreleasedVersion:
    39  			log.Ctx(ctx).Warn().Str("version", currentVersion).Msg("not running a released version of SpiceDB")
    40  			return nil
    41  
    42  		case UpdateAvailable:
    43  			if release == nil {
    44  				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
    45  				return nil
    46  			}
    47  
    48  			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)
    49  			return nil
    50  
    51  		case UpToDate:
    52  			if release == nil {
    53  				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
    54  				return nil
    55  			}
    56  
    57  			log.Ctx(ctx).Info().Str("latest-released-version", release.Version).Msg("this is the latest released version of SpiceDB")
    58  			return nil
    59  
    60  		case Unknown:
    61  			if release == nil {
    62  				log.Ctx(ctx).Warn().Msg("unable to check for or load the new SpiceDB version")
    63  				return nil
    64  			}
    65  
    66  			log.Ctx(ctx).Warn().Str("unknown-released-version", release.Version).Msg("unable to check for a new SpiceDB version")
    67  			return nil
    68  
    69  		default:
    70  			panic("Unknown state for CheckAndLogRunE")
    71  		}
    72  	}
    73  }