root.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/Masterminds/semver/v3"
  9. "github.com/fatih/color"
  10. "github.com/google/go-github/v41/github"
  11. api "github.com/porter-dev/porter/api/client"
  12. "github.com/spf13/cobra"
  13. "k8s.io/client-go/util/homedir"
  14. )
  15. // rootCmd represents the base command when called without any subcommands
  16. var rootCmd = &cobra.Command{
  17. Use: "porter",
  18. Short: "Porter is a dashboard for managing Kubernetes clusters.",
  19. 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`,
  20. }
  21. var home = homedir.HomeDir()
  22. // Execute adds all child commands to the root command and sets flags appropriately.
  23. // This is called by main.main(). It only needs to happen once to the rootCmd.
  24. func Execute() {
  25. Setup()
  26. rootCmd.PersistentFlags().AddFlagSet(defaultFlagSet)
  27. if Version != "dev" {
  28. ghClient := github.NewClient(nil)
  29. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  30. defer cancel()
  31. release, _, err := ghClient.Repositories.GetLatestRelease(ctx, "porter-dev", "porter")
  32. if err == nil {
  33. release.GetURL()
  34. // we do not care for an error here because we do not want to block the user here
  35. constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", strings.TrimPrefix(Version, "v")))
  36. if err == nil {
  37. latestRelease, err := semver.NewVersion(strings.TrimPrefix(release.GetTagName(), "v"))
  38. if err == nil {
  39. if constraint.Check(latestRelease) {
  40. color.New(color.FgYellow).Println("A new version of the porter CLI is available to download at https://github.com/porter-dev/porter/releases/latest")
  41. }
  42. }
  43. }
  44. }
  45. }
  46. if err := rootCmd.Execute(); err != nil {
  47. color.New(color.FgRed).Println(err)
  48. os.Exit(1)
  49. }
  50. }
  51. func Setup() {
  52. InitAndLoadConfig()
  53. }
  54. func GetAPIClient(config *CLIConfig) *api.Client {
  55. if token := config.Token; token != "" {
  56. return api.NewClientWithToken(config.Host+"/api", token)
  57. }
  58. return api.NewClient(config.Host+"/api", "cookie.json")
  59. }