github.com/kbehouse/nsc@v0.0.6/cmd/deleteexport.go (about) 1 /* 2 * Copyright 2018-2019 The NATS Authors 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 16 package cmd 17 18 import ( 19 "fmt" 20 21 "github.com/kbehouse/nsc/cmd/store" 22 cli "github.com/nats-io/cliprompts/v2" 23 "github.com/nats-io/jwt/v2" 24 "github.com/nats-io/nkeys" 25 "github.com/spf13/cobra" 26 ) 27 28 func createDeleteExportCmd() *cobra.Command { 29 var params DeleteExportParams 30 cmd := &cobra.Command{ 31 Use: "export", 32 Short: "Delete an export", 33 Args: MaxArgs(0), 34 SilenceUsage: true, 35 RunE: func(cmd *cobra.Command, args []string) error { 36 return RunAction(cmd, args, ¶ms) 37 }, 38 } 39 cmd.Flags().StringVarP(¶ms.subject, "subject", "s", "", "subject") 40 params.AccountContextParams.BindFlags(cmd) 41 return cmd 42 } 43 44 func init() { 45 deleteCmd.AddCommand(createDeleteExportCmd()) 46 } 47 48 type DeleteExportParams struct { 49 AccountContextParams 50 SignerParams 51 claim *jwt.AccountClaims 52 index int 53 subject string 54 } 55 56 func (p *DeleteExportParams) SetDefaults(ctx ActionCtx) error { 57 p.AccountContextParams.SetDefaults(ctx) 58 p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx) 59 p.index = -1 60 return nil 61 } 62 63 func (p *DeleteExportParams) PreInteractive(ctx ActionCtx) error { 64 var err error 65 if err = p.AccountContextParams.Edit(ctx); err != nil { 66 return err 67 } 68 return nil 69 } 70 71 func (p *DeleteExportParams) Load(ctx ActionCtx) error { 72 var err error 73 74 if err = p.AccountContextParams.Validate(ctx); err != nil { 75 return err 76 } 77 78 p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name) 79 if err != nil { 80 return err 81 } 82 83 switch len(p.claim.Exports) { 84 case 0: 85 return fmt.Errorf("account %q doesn't have exports", p.AccountContextParams.Name) 86 case 1: 87 if p.subject == "" { 88 p.subject = string(p.claim.Exports[0].Subject) 89 } 90 } 91 for i, e := range p.claim.Exports { 92 if string(e.Subject) == p.subject { 93 p.index = i 94 break 95 } 96 } 97 98 return nil 99 } 100 101 func (p *DeleteExportParams) PostInteractive(ctx ActionCtx) error { 102 var err error 103 104 choices, err := GetAccountExports(p.claim) 105 if err != nil { 106 return err 107 } 108 labels := AccountExportChoices(choices).String() 109 110 p.index, err = cli.Select("select export to delete", "", labels) 111 if err != nil { 112 return err 113 } 114 115 if err = p.SignerParams.Edit(ctx); err != nil { 116 return err 117 } 118 119 return nil 120 } 121 122 func (p *DeleteExportParams) Validate(ctx ActionCtx) error { 123 if p.subject == "" && p.index == -1 { 124 return fmt.Errorf("subject is required") 125 } 126 if p.index == -1 { 127 return fmt.Errorf("no export matching %q found", p.subject) 128 } 129 if err := p.SignerParams.Resolve(ctx); err != nil { 130 return err 131 } 132 return nil 133 } 134 135 func (p *DeleteExportParams) Run(ctx ActionCtx) (store.Status, error) { 136 dex := p.claim.Exports[p.index] 137 p.claim.Exports = append(p.claim.Exports[:p.index], p.claim.Exports[p.index+1:]...) 138 token, err := p.claim.Encode(p.signerKP) 139 if err != nil { 140 return nil, err 141 } 142 143 r := store.NewDetailedReport(true) 144 r.AddOK("deleted %s export %q", dex.Type, dex.Subject) 145 rs, err := ctx.StoreCtx().Store.StoreClaim([]byte(token)) 146 if rs != nil { 147 r.Add(rs) 148 } 149 if err != nil { 150 r.AddFromError(err) 151 } 152 return r, nil 153 }