root.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "runtime"
  7. "strings"
  8. "time"
  9. "github.com/Masterminds/semver/v3"
  10. "github.com/fatih/color"
  11. "github.com/google/go-github/v41/github"
  12. "github.com/porter-dev/porter/cli/cmd/config"
  13. "github.com/porter-dev/porter/cli/cmd/utils"
  14. "github.com/spf13/cobra"
  15. "k8s.io/client-go/util/homedir"
  16. )
  17. // rootCmd represents the base command when called without any subcommands
  18. var rootCmd = &cobra.Command{
  19. Use: "porter",
  20. Short: "Porter is a dashboard for managing Kubernetes clusters.",
  21. Long: `Porter is a tool for creating, versioning, and updating Kubernetes deployments using a visual dashboard. For more information, visit github.com/porter-dev/porter`,
  22. }
  23. var home = homedir.HomeDir()
  24. // Execute adds all child commands to the root command and sets flags appropriately.
  25. // This is called by main.main(). It only needs to happen once to the rootCmd.
  26. func Execute() {
  27. Setup()
  28. alerter := config.InitAlerter(config.GetCLIConfig())
  29. defer alerter.Flush()
  30. rootCmd.PersistentFlags().AddFlagSet(utils.DefaultFlagSet)
  31. if config.Version != "dev" {
  32. ghClient := github.NewClient(nil)
  33. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  34. defer cancel()
  35. release, _, err := ghClient.Repositories.GetLatestRelease(ctx, "porter-dev", "porter")
  36. if err == nil {
  37. release.GetURL()
  38. // we do not care for an error here because we do not want to block the user here
  39. constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", strings.TrimPrefix(config.Version, "v")))
  40. if err == nil {
  41. latestRelease, err := semver.NewVersion(strings.TrimPrefix(release.GetTagName(), "v"))
  42. if err == nil {
  43. if constraint.Check(latestRelease) {
  44. color.New(color.FgYellow).Fprint(os.Stderr, "A new version of the porter CLI is available. Run the following to update: ")
  45. if runtime.GOOS == "darwin" {
  46. color.New(color.FgYellow, color.Bold).Fprintln(os.Stderr, "brew install porter-dev/porter/porter")
  47. } else {
  48. color.New(color.FgYellow, color.Bold).Fprintln(os.Stderr, "/bin/bash -c \"$(curl -fsSL https://install.porter.run)\"")
  49. }
  50. color.New(color.FgYellow).Fprintf(os.Stderr, "View CLI installation and upgrade docs at https://docs.porter.run/cli/installation\n\n")
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if err := rootCmd.Execute(); err != nil {
  57. color.New(color.FgRed).Println(err)
  58. os.Exit(1)
  59. }
  60. }
  61. func Setup() {
  62. config.InitAndLoadConfig()
  63. }