github.com/gofiber/fiber-cli@v0.0.3/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/muesli/termenv"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  const version = "0.0.3"
    12  const configName = ".fiberconfig"
    13  
    14  var (
    15  	rc = rootConfig{
    16  		CliVersionCheckInterval: int64((time.Hour * 12) / time.Second),
    17  	}
    18  )
    19  
    20  type rootConfig struct {
    21  	CliVersionCheckInterval int64 `json:"cli_version_check_interval"`
    22  	CliVersionCheckedAt     int64 `json:"cli_version_checked_at"`
    23  }
    24  
    25  func init() {
    26  	rootCmd.AddCommand(
    27  		versionCmd, newCmd, devCmd, upgradeCmd,
    28  	)
    29  }
    30  
    31  // rootCmd represents the base command when called without any subcommands
    32  var rootCmd = &cobra.Command{
    33  	Use:               "fiber",
    34  	Long:              longDescription,
    35  	RunE:              rootRunE,
    36  	PersistentPreRun:  rootPersistentPreRun,
    37  	PersistentPostRun: rootPersistentPostRun,
    38  	SilenceErrors:     true,
    39  }
    40  
    41  // Execute adds all child commands to the root command and sets flags appropriately.
    42  // This is called by main.main(). It only needs to happen once to the rootCmd.
    43  func Execute() {
    44  	if err := rootCmd.Execute(); err != nil {
    45  		rootCmd.Println(err)
    46  		osExit(1)
    47  	}
    48  }
    49  
    50  func rootRunE(cmd *cobra.Command, _ []string) error {
    51  	return cmd.Help()
    52  }
    53  
    54  func rootPersistentPreRun(cmd *cobra.Command, _ []string) {
    55  	if err := loadConfig(); err != nil {
    56  		warning := fmt.Sprintf("WARNING: failed to load config: %s\n\n", err)
    57  		cmd.Println(termenv.String(warning).Foreground(termenv.ANSIBrightYellow))
    58  	}
    59  }
    60  
    61  func rootPersistentPostRun(cmd *cobra.Command, _ []string) {
    62  	checkCliVersion(cmd)
    63  }
    64  
    65  func checkCliVersion(cmd *cobra.Command) {
    66  	if !needCheckCliVersion() {
    67  		return
    68  	}
    69  
    70  	cliLatestVersion, err := latestVersion(true)
    71  	if err != nil {
    72  		return
    73  	}
    74  
    75  	if version != cliLatestVersion {
    76  		cmd.Println(termenv.String(fmt.Sprintf(versionWarningFormat, version, cliLatestVersion)).
    77  			Foreground(termenv.ANSIBrightYellow))
    78  	}
    79  
    80  	updateVersionCheckedAt()
    81  }
    82  
    83  func updateVersionCheckedAt() {
    84  	rc.CliVersionCheckedAt = time.Now().Unix()
    85  	storeConfig()
    86  }
    87  
    88  func needCheckCliVersion() bool {
    89  	return rc.CliVersionCheckedAt+rc.CliVersionCheckInterval < time.Now().Unix()
    90  }
    91  
    92  const (
    93  	longDescription = `🚀 Fiber is an Express inspired web framework written in Go with 💖
    94  Learn more on https://gofiber.io
    95  
    96  CLI version ` + version
    97  
    98  	versionWarningFormat = `
    99  WARNING: You are using fiber-cli version %s; however, version %s is available.
   100  You should consider upgrading via the 'go get -u github.com/gofiber/fiber-cli' command.`
   101  )