github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/logout/logout.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package logout
    10  
    11  import (
    12  	"fmt"
    13  
    14  	"github.com/vchain-us/vcn/pkg/store"
    15  
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  // NewCommand returns the cobra command for `vcn logout`
    20  func NewCommand() *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use:   "logout",
    23  		Short: "Logout the current user",
    24  		Long:  ``,
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			cmd.SilenceUsage = true
    27  			output, err := cmd.Flags().GetString("output")
    28  			if err != nil {
    29  				return err
    30  			}
    31  			if store.Config() == nil ||
    32  				(store.Config().CurrentContext.Email == "" &&
    33  					store.Config().CurrentContext.LcHost == "") {
    34  				fmt.Println("No logged-in user.")
    35  				return nil
    36  			}
    37  			if err := Execute(); err != nil {
    38  				return err
    39  			}
    40  			if output == "" {
    41  				fmt.Println("Logout successful.")
    42  			}
    43  			return nil
    44  		},
    45  		Args: cobra.NoArgs,
    46  	}
    47  
    48  	return cmd
    49  }
    50  
    51  // Execute logout action for both Immutable Ledger and CodeNotary.io
    52  func Execute() error {
    53  	store.Config().ClearContext()
    54  	if err := store.SaveConfig(); err != nil {
    55  		return err
    56  	}
    57  	return nil
    58  }