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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	"encoding/json"
    24  	"fmt"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  	"github.com/snapcore/snapd/i18n"
    30  )
    31  
    32  var shortCreateUserHelp = i18n.G("Create a local system user")
    33  var longCreateUserHelp = i18n.G(`
    34  The create-user command creates a local system user with the username and SSH
    35  keys registered on the store account identified by the provided email address.
    36  
    37  An account can be setup at https://login.ubuntu.com.
    38  `)
    39  
    40  type cmdCreateUser struct {
    41  	clientMixin
    42  	Positional struct {
    43  		Email string
    44  	} `positional-args:"yes"`
    45  
    46  	JSON         bool `long:"json"`
    47  	Sudoer       bool `long:"sudoer"`
    48  	Known        bool `long:"known"`
    49  	ForceManaged bool `long:"force-managed"`
    50  }
    51  
    52  func init() {
    53  	cmd := addCommand("create-user", shortCreateUserHelp, longCreateUserHelp, func() flags.Commander { return &cmdCreateUser{} },
    54  		map[string]string{
    55  			// TRANSLATORS: This should not start with a lowercase letter.
    56  			"json": i18n.G("Output results in JSON format"),
    57  			// TRANSLATORS: This should not start with a lowercase letter.
    58  			"sudoer": i18n.G("Grant sudo access to the created user"),
    59  			// TRANSLATORS: This should not start with a lowercase letter.
    60  			"known": i18n.G("Use known assertions for user creation"),
    61  			// TRANSLATORS: This should not start with a lowercase letter.
    62  			"force-managed": i18n.G("Force adding the user, even if the device is already managed"),
    63  		}, []argDesc{{
    64  			// TRANSLATORS: This is a noun and it needs to begin with < and end with >
    65  			name: i18n.G("<email>"),
    66  			// TRANSLATORS: This should not start with a lowercase letter (unless it's "login.ubuntu.com"). Also, note users on login.ubuntu.com can have multiple email addresses.
    67  			desc: i18n.G("An email of a user on login.ubuntu.com"),
    68  		}})
    69  	cmd.hidden = true
    70  }
    71  
    72  func (x *cmdCreateUser) Execute(args []string) error {
    73  	if len(args) > 0 {
    74  		return ErrExtraArgs
    75  	}
    76  
    77  	options := client.CreateUserOptions{
    78  		Email:        x.Positional.Email,
    79  		Sudoer:       x.Sudoer,
    80  		Known:        x.Known,
    81  		ForceManaged: x.ForceManaged,
    82  	}
    83  
    84  	var results []*client.CreateUserResult
    85  	var result *client.CreateUserResult
    86  	var err error
    87  
    88  	if options.Email == "" && options.Known {
    89  		results, err = x.client.CreateUsers([]*client.CreateUserOptions{&options})
    90  	} else {
    91  		result, err = x.client.CreateUser(&options)
    92  		if err == nil {
    93  			results = append(results, result)
    94  		}
    95  	}
    96  
    97  	createErr := err
    98  
    99  	// Print results regardless of error because some users may have been created.
   100  	if x.JSON {
   101  		var data []byte
   102  		if result != nil {
   103  			data, err = json.Marshal(result)
   104  		} else if len(results) > 0 {
   105  			data, err = json.Marshal(results)
   106  		}
   107  		if err != nil {
   108  			return err
   109  		}
   110  		fmt.Fprintf(Stdout, "%s\n", data)
   111  	} else {
   112  		for _, result := range results {
   113  			fmt.Fprintf(Stdout, i18n.G("created user %q\n"), result.Username)
   114  		}
   115  	}
   116  
   117  	return createErr
   118  }