github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/logout/logout.go (about)

     1  package logout
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
     8  	"github.com/henvic/wedeploycli/command/internal/we"
     9  	"github.com/henvic/wedeploycli/fancy"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // LogoutCmd unsets the user credential
    14  var LogoutCmd = &cobra.Command{
    15  	Use:     "logout",
    16  	Short:   "Logout from your account\n\t\t",
    17  	Args:    cobra.NoArgs,
    18  	PreRunE: preRun,
    19  	RunE:    logoutRun,
    20  }
    21  
    22  var setupHost = cmdflagsfromhost.SetupHost{
    23  	Pattern: cmdflagsfromhost.RemotePattern,
    24  }
    25  
    26  func init() {
    27  	setupHost.Init(LogoutCmd)
    28  }
    29  
    30  func preRun(cmd *cobra.Command, args []string) error {
    31  	return setupHost.Process(context.Background(), we.Context())
    32  }
    33  
    34  func logoutRun(cmd *cobra.Command, args []string) error {
    35  	var wectx = we.Context()
    36  	var conf = wectx.Config()
    37  	var params = conf.GetParams()
    38  	var rl = params.Remotes
    39  	var remote = rl.Get(wectx.Remote())
    40  
    41  	switch remote.Username {
    42  	case "":
    43  		fmt.Println(fancy.Info(fmt.Sprintf(`You are not logged in on %s.`,
    44  			remote.Infrastructure)))
    45  	default:
    46  		fmt.Printf("You (%s) have been logged out of %s.\n",
    47  			remote.Username,
    48  			remote.Infrastructure)
    49  	}
    50  
    51  	remote.Username = ""
    52  	remote.Token = ""
    53  	rl.Set(wectx.Remote(), remote)
    54  	return conf.Save()
    55  }