login.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/spf13/cobra"
  8. )
  9. var (
  10. host string
  11. )
  12. // loginCmd represents the login command
  13. var loginCmd = &cobra.Command{
  14. Use: "login",
  15. Short: "Authorizes a user for a given Porter server",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. err := login(host)
  18. if err != nil {
  19. fmt.Println("Error logging in:", err.Error())
  20. os.Exit(1)
  21. }
  22. },
  23. }
  24. func init() {
  25. rootCmd.AddCommand(loginCmd)
  26. loginCmd.PersistentFlags().StringVar(
  27. &host,
  28. "host",
  29. "http://localhost:10000",
  30. "host url of Porter instance",
  31. )
  32. }
  33. func login(host string) error {
  34. var username, pw string
  35. fmt.Println("Please log in with an email and password:")
  36. username, err := promptPlaintext("Email: ")
  37. if err != nil {
  38. return err
  39. }
  40. pw, err = promptPasswordWithConfirmation()
  41. if err != nil {
  42. return err
  43. }
  44. client := api.NewClient(host+"/api", "cookie.json")
  45. _, err = client.Login(context.Background(), &api.LoginRequest{
  46. Email: username,
  47. Password: pw,
  48. })
  49. if err != nil {
  50. return err
  51. }
  52. fmt.Println("Successfully logged in!")
  53. return nil
  54. }