gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap/cmd_delete_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 cmdDeleteKey struct {
    33  	Positional struct {
    34  		KeyName keyName
    35  	} `positional-args:"true" required:"true"`
    36  }
    37  
    38  func init() {
    39  	cmd := addCommand("delete-key",
    40  		i18n.G("Delete cryptographic key pair"),
    41  		i18n.G(`
    42  The delete-key command deletes the local cryptographic key pair with
    43  the given name.
    44  `),
    45  		func() flags.Commander {
    46  			return &cmdDeleteKey{}
    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 delete"),
    52  		}})
    53  	cmd.hidden = true
    54  	cmd.completeHidden = true
    55  }
    56  
    57  func (x *cmdDeleteKey) Execute(args []string) error {
    58  	if len(args) > 0 {
    59  		return ErrExtraArgs
    60  	}
    61  
    62  	keypairMgr, err := signtool.GetKeypairManager()
    63  	if err != nil {
    64  		return err
    65  	}
    66  	keyName := string(x.Positional.KeyName)
    67  	err = keypairMgr.DeleteByName(keyName)
    68  	if _, ok := err.(*asserts.ExternalUnsupportedOpError); ok {
    69  		return fmt.Errorf(i18n.G("cannot delete external keypair manager key via snap command, use the appropriate external procedure"))
    70  	}
    71  	if err != nil {
    72  		return fmt.Errorf("cannot delete key named %q: %v", keyName, err)
    73  	}
    74  	return nil
    75  }