github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_whoami.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/i18n"
    26  
    27  	"github.com/jessevdk/go-flags"
    28  )
    29  
    30  var shortWhoAmIHelp = i18n.G("Show the email the user is logged in with")
    31  var longWhoAmIHelp = i18n.G(`
    32  The whoami command shows the email the user is logged in with.
    33  `)
    34  
    35  type cmdWhoAmI struct {
    36  	clientMixin
    37  }
    38  
    39  func init() {
    40  	addCommand("whoami", shortWhoAmIHelp, longWhoAmIHelp, func() flags.Commander { return &cmdWhoAmI{} }, nil, nil)
    41  }
    42  
    43  func (cmd cmdWhoAmI) Execute(args []string) error {
    44  	if len(args) > 0 {
    45  		return ErrExtraArgs
    46  	}
    47  
    48  	email, err := cmd.client.WhoAmI()
    49  	if err != nil {
    50  		return err
    51  	}
    52  	if email == "" {
    53  		// just printing nothing looks weird (as if something had gone wrong)
    54  		email = "-"
    55  	}
    56  	fmt.Fprintln(Stdout, i18n.G("email:"), email)
    57  	return nil
    58  }