github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/cmd/snap/cmd_create_key.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 "fmt" 24 25 "github.com/jessevdk/go-flags" 26 27 "github.com/snapcore/snapd/asserts" 28 "github.com/snapcore/snapd/i18n" 29 ) 30 31 type cmdCreateKey struct { 32 Positional struct { 33 KeyName string 34 } `positional-args:"true"` 35 } 36 37 func init() { 38 cmd := addCommand("create-key", 39 i18n.G("Create cryptographic key pair"), 40 i18n.G(` 41 The create-key command creates a cryptographic key pair that can be 42 used for signing assertions. 43 `), 44 func() flags.Commander { 45 return &cmdCreateKey{} 46 }, nil, []argDesc{{ 47 // TRANSLATORS: This needs to begin with < and end with > 48 name: i18n.G("<key-name>"), 49 // TRANSLATORS: This should not start with a lowercase letter. 50 desc: i18n.G("Name of key to create; defaults to 'default'"), 51 }}) 52 cmd.hidden = true 53 cmd.completeHidden = true 54 } 55 56 func (x *cmdCreateKey) Execute(args []string) error { 57 if len(args) > 0 { 58 return ErrExtraArgs 59 } 60 61 keyName := x.Positional.KeyName 62 if keyName == "" { 63 keyName = "default" 64 } 65 if !asserts.IsValidAccountKeyName(keyName) { 66 return fmt.Errorf(i18n.G("key name %q is not valid; only ASCII letters, digits, and hyphens are allowed"), keyName) 67 } 68 69 keypairMgr, err := getKeypairManager() 70 if err != nil { 71 return err 72 } 73 return generateKey(keypairMgr, keyName) 74 }