github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/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  	"github.com/snapcore/snapd/asserts"
    28  	"github.com/snapcore/snapd/i18n"
    29  )
    30  
    31  type cmdDeleteKey struct {
    32  	Positional struct {
    33  		KeyName keyName
    34  	} `positional-args:"true" required:"true"`
    35  }
    36  
    37  func init() {
    38  	cmd := addCommand("delete-key",
    39  		i18n.G("Delete cryptographic key pair"),
    40  		i18n.G(`
    41  The delete-key command deletes the local cryptographic key pair with
    42  the given name.
    43  `),
    44  		func() flags.Commander {
    45  			return &cmdDeleteKey{}
    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 delete"),
    51  		}})
    52  	cmd.hidden = true
    53  	cmd.completeHidden = true
    54  }
    55  
    56  func (x *cmdDeleteKey) Execute(args []string) error {
    57  	if len(args) > 0 {
    58  		return ErrExtraArgs
    59  	}
    60  
    61  	keypairMgr, err := getKeypairManager()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	err = keypairMgr.Delete(string(x.Positional.KeyName))
    66  	if _, ok := err.(*asserts.ExternalUnsupportedOpError); ok {
    67  		return fmt.Errorf(i18n.G("cannot delete external keypair manager key via snap command, use the appropriate external procedure"))
    68  	}
    69  	return err
    70  }