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