github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/cmd_remove_user.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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/jessevdk/go-flags"
    26  
    27  	"github.com/snapcore/snapd/client"
    28  	"github.com/snapcore/snapd/i18n"
    29  )
    30  
    31  var shortRemoveUserHelp = i18n.G("Remove a local system user")
    32  var longRemoveUserHelp = i18n.G(`
    33  The remove-user command removes a local system user.
    34  `)
    35  
    36  type cmdRemoveUser struct {
    37  	clientMixin
    38  	Positional struct {
    39  		Username string
    40  	} `positional-args:"yes"`
    41  }
    42  
    43  func init() {
    44  	cmd := addCommand("remove-user", shortRemoveUserHelp, longRemoveUserHelp, func() flags.Commander { return &cmdRemoveUser{} },
    45  		map[string]string{}, []argDesc{{
    46  			// TRANSLATORS: This is a noun and it needs to begin with < and end with >
    47  			name: i18n.G("<username>"),
    48  			// TRANSLATORS: This should not start with a lowercase letter
    49  			desc: i18n.G("The username to remove"),
    50  		}})
    51  	cmd.hidden = true
    52  }
    53  
    54  func (x *cmdRemoveUser) Execute(args []string) error {
    55  	if len(args) > 0 {
    56  		return ErrExtraArgs
    57  	}
    58  
    59  	username := x.Positional.Username
    60  
    61  	options := client.RemoveUserOptions{
    62  		Username: username,
    63  	}
    64  
    65  	removed, err := x.client.RemoveUser(&options)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	if len(removed) != 1 {
    70  		return fmt.Errorf("internal error: RemoveUser returned unexpected number of removed users: %v", len(removed))
    71  	}
    72  	fmt.Fprintf(Stdout, i18n.G("removed user %q\n"), removed[0].Username)
    73  
    74  	return nil
    75  }