File: cmd_create_user.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (118 lines) | stat: -rw-r--r-- 3,533 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2016 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main

import (
	"encoding/json"
	"fmt"

	"github.com/jessevdk/go-flags"

	"github.com/snapcore/snapd/client"
	"github.com/snapcore/snapd/i18n"
)

var shortCreateUserHelp = i18n.G("Create a local system user")
var longCreateUserHelp = i18n.G(`
The create-user command creates a local system user with the username and SSH
keys registered on the store account identified by the provided email address.

An account can be setup at https://login.ubuntu.com.
`)

type cmdCreateUser struct {
	clientMixin
	Positional struct {
		Email string
	} `positional-args:"yes"`

	JSON         bool `long:"json"`
	Sudoer       bool `long:"sudoer"`
	Known        bool `long:"known"`
	ForceManaged bool `long:"force-managed"`
}

func init() {
	cmd := addCommand("create-user", shortCreateUserHelp, longCreateUserHelp, func() flags.Commander { return &cmdCreateUser{} },
		map[string]string{
			// TRANSLATORS: This should not start with a lowercase letter.
			"json": i18n.G("Output results in JSON format"),
			// TRANSLATORS: This should not start with a lowercase letter.
			"sudoer": i18n.G("Grant sudo access to the created user"),
			// TRANSLATORS: This should not start with a lowercase letter.
			"known": i18n.G("Use known assertions for user creation"),
			// TRANSLATORS: This should not start with a lowercase letter.
			"force-managed": i18n.G("Force adding the user, even if the device is already managed"),
		}, []argDesc{{
			// TRANSLATORS: This is a noun and it needs to begin with < and end with >
			name: i18n.G("<email>"),
			// 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.
			desc: i18n.G("An email of a user on login.ubuntu.com"),
		}})
	cmd.hidden = true
}

func (x *cmdCreateUser) Execute(args []string) error {
	if len(args) > 0 {
		return ErrExtraArgs
	}

	options := client.CreateUserOptions{
		Email:        x.Positional.Email,
		Sudoer:       x.Sudoer,
		Known:        x.Known,
		ForceManaged: x.ForceManaged,
	}

	var results []*client.CreateUserResult
	var result *client.CreateUserResult
	var err error

	if options.Email == "" && options.Known {
		results, err = x.client.CreateUsers([]*client.CreateUserOptions{&options})
	} else {
		result, err = x.client.CreateUser(&options)
		if err == nil {
			results = append(results, result)
		}
	}

	createErr := err

	// Print results regardless of error because some users may have been created.
	if x.JSON {
		var data []byte
		if result != nil {
			data, err = json.Marshal(result)
		} else if len(results) > 0 {
			data, err = json.Marshal(results)
		}
		if err != nil {
			return err
		}
		fmt.Fprintf(Stdout, "%s\n", data)
	} else {
		for _, result := range results {
			fmt.Fprintf(Stdout, i18n.G("created user %q\n"), result.Username)
		}
	}

	return createErr
}