github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/auth/logout.go (about)

     1  package auth
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/errs"
     5  	"github.com/ActiveState/cli/internal/keypairs"
     6  	"github.com/ActiveState/cli/internal/locale"
     7  	"github.com/ActiveState/cli/internal/output"
     8  	"github.com/ActiveState/cli/pkg/platform/authentication"
     9  )
    10  
    11  type Logout struct {
    12  	output.Outputer
    13  	*authentication.Auth
    14  	Cfg keypairs.Configurable
    15  }
    16  
    17  func NewLogout(prime primeable) *Logout {
    18  	return &Logout{prime.Output(), prime.Auth(), prime.Config()}
    19  }
    20  
    21  func (l *Logout) Run() error {
    22  	if err := l.Auth.Logout(); err != nil {
    23  		return errs.Wrap(err, "Logout failed")
    24  	}
    25  
    26  	err := keypairs.DeleteWithDefaults(l.Cfg)
    27  	if err != nil {
    28  		return locale.WrapError(err, "err_auth_logout", "Failed to delete authentication key")
    29  	}
    30  	l.Outputer.Notice(output.Title(locale.Tl("authentication_title", "Authentication")))
    31  	if l.Auth.AvailableAPIToken() == "" {
    32  		l.Outputer.Notice(locale.T("logged_out"))
    33  	} else {
    34  		l.Outputer.Notice(locale.T("logout_still_have_api_token"))
    35  	}
    36  	return nil
    37  }