github.com/coreos/mantle@v0.13.0/cmd/ore/packet/delete-keys.go (about) 1 // Copyright 2017 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package packet 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/spf13/cobra" 22 ) 23 24 var ( 25 cmdDeleteKeys = &cobra.Command{ 26 Use: "delete-keys <key>...", 27 Short: "Delete Packet SSH keys", 28 RunE: runDeleteKeys, 29 } 30 ) 31 32 func init() { 33 Packet.AddCommand(cmdDeleteKeys) 34 } 35 36 func runDeleteKeys(cmd *cobra.Command, args []string) error { 37 if len(args) == 0 { 38 fmt.Fprintf(os.Stderr, "Specify at least one key.\n") 39 os.Exit(2) 40 } 41 42 labels := map[string]bool{} 43 for _, arg := range args { 44 labels[arg] = true 45 } 46 47 keys, err := API.ListKeys() 48 if err != nil { 49 fmt.Fprintf(os.Stderr, "Couldn't list keys: %v\n", err) 50 os.Exit(1) 51 } 52 53 exit := 0 54 for _, key := range keys { 55 if labels[key.Label] { 56 if err := API.DeleteKey(key.ID); err != nil { 57 fmt.Fprintf(os.Stderr, "Couldn't delete key: %v\n", key.Label) 58 exit = 1 59 } 60 delete(labels, key.Label) 61 } 62 } 63 64 for label := range labels { 65 fmt.Fprintf(os.Stderr, "No such key: %v\n", label) 66 exit = 1 67 } 68 69 os.Exit(exit) 70 return nil 71 }