()
| 13 | ) |
| 14 | |
| 15 | func (r *RootCmd) logout() *serpent.Command { |
| 16 | cmd := &serpent.Command{ |
| 17 | Use: "logout", |
| 18 | Short: "Unauthenticate your local session", |
| 19 | Handler: func(inv *serpent.Invocation) error { |
| 20 | client, err := r.InitClient(inv) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | var errors []error |
| 26 | |
| 27 | config := r.createConfig() |
| 28 | |
| 29 | _, err = cliui.Prompt(inv, cliui.PromptOptions{ |
| 30 | Text: "Are you sure you want to log out?", |
| 31 | IsConfirm: true, |
| 32 | Default: cliui.ConfirmYes, |
| 33 | }) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | err = client.Logout(inv.Context()) |
| 39 | if err != nil { |
| 40 | errors = append(errors, xerrors.Errorf("logout api: %w", err)) |
| 41 | } |
| 42 | |
| 43 | err = config.URL().Delete() |
| 44 | // Only throw error if the URL configuration file is present, |
| 45 | // otherwise the user is already logged out, and we proceed |
| 46 | if err != nil && !os.IsNotExist(err) { |
| 47 | errors = append(errors, xerrors.Errorf("remove URL file: %w", err)) |
| 48 | } |
| 49 | |
| 50 | err = r.ensureTokenBackend().Delete(client.URL) |
| 51 | // Only throw error if the session configuration file is present, |
| 52 | // otherwise the user is already logged out, and we proceed |
| 53 | if err != nil && !xerrors.Is(err, os.ErrNotExist) { |
| 54 | if xerrors.Is(err, sessionstore.ErrNotImplemented) { |
| 55 | errors = append(errors, errKeyringNotSupported) |
| 56 | } else { |
| 57 | errors = append(errors, xerrors.Errorf("remove session token: %w", err)) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | err = config.Organization().Delete() |
| 62 | // If the organization configuration file is absent, we still proceed |
| 63 | if err != nil && !os.IsNotExist(err) { |
| 64 | errors = append(errors, xerrors.Errorf("remove organization file: %w", err)) |
| 65 | } |
| 66 | |
| 67 | if len(errors) > 0 { |
| 68 | var errorStringBuilder strings.Builder |
| 69 | for _, err := range errors { |
| 70 | _, _ = fmt.Fprint(&errorStringBuilder, "\t"+err.Error()+"\n") |
| 71 | } |
| 72 | errorString := strings.TrimRight(errorStringBuilder.String(), "\n") |
no test coverage detected