()
| 488 | } |
| 489 | |
| 490 | func (r *RootCmd) loginToken() *serpent.Command { |
| 491 | return &serpent.Command{ |
| 492 | Use: "token", |
| 493 | Short: "Print the current session token", |
| 494 | Long: "Print the session token for use in scripts and automation.", |
| 495 | Middleware: serpent.RequireNArgs(0), |
| 496 | Handler: func(inv *serpent.Invocation) error { |
| 497 | if err := r.ensureClientURL(); err != nil { |
| 498 | return err |
| 499 | } |
| 500 | // When using the file storage, a session token is stored for a single |
| 501 | // deployment URL that the user is logged in to. They keyring can store |
| 502 | // multiple deployment session tokens. Error if the requested URL doesn't |
| 503 | // match the stored config URL when using file storage to avoid returning |
| 504 | // a token for the wrong deployment. |
| 505 | backend := r.ensureTokenBackend() |
| 506 | if _, ok := backend.(*sessionstore.File); ok { |
| 507 | conf := r.createConfig() |
| 508 | storedURL, err := conf.URL().Read() |
| 509 | if err == nil { |
| 510 | storedURL = strings.TrimSpace(storedURL) |
| 511 | if storedURL != r.clientURL.String() { |
| 512 | return xerrors.Errorf("file session token storage only supports one server at a time: requested %s but logged into %s", r.clientURL.String(), storedURL) |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | tok, err := backend.Read(r.clientURL) |
| 517 | if err != nil { |
| 518 | if xerrors.Is(err, os.ErrNotExist) { |
| 519 | return xerrors.New("no session token found - run 'coder login' first") |
| 520 | } |
| 521 | if xerrors.Is(err, sessionstore.ErrNotImplemented) { |
| 522 | return errKeyringNotSupported |
| 523 | } |
| 524 | return xerrors.Errorf("read session token: %w", err) |
| 525 | } |
| 526 | if tok == "" { |
| 527 | return xerrors.New("no session token found - run 'coder login' first") |
| 528 | } |
| 529 | _, err = fmt.Fprintln(inv.Stdout, tok) |
| 530 | return err |
| 531 | }, |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // isWSL determines if coder-cli is running within Windows Subsystem for Linux |
| 536 | func isWSL() (bool, error) { |
no test coverage detected