github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/describeaccount.go (about) 1 /* 2 * Copyright 2018-2020 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 "github.com/nats-io/jwt/v2" 20 "github.com/nats-io/nsc/v2/cmd/store" 21 "github.com/spf13/cobra" 22 ) 23 24 func createDescribeAccountCmd() *cobra.Command { 25 var params DescribeAccountParams 26 cmd := &cobra.Command{ 27 Use: "account", 28 Short: "Describes an account", 29 Args: cobra.MaximumNArgs(1), 30 SilenceUsage: true, 31 RunE: func(cmd *cobra.Command, args []string) error { 32 return RunAction(cmd, args, ¶ms) 33 }, 34 } 35 cmd.Flags().StringVarP(¶ms.outputFile, "output-file", "o", "--", "output file, '--' is stdout") 36 cmd.Flags().StringVarP(¶ms.AccountContextParams.Name, "name", "n", "", "account name") 37 38 return cmd 39 } 40 41 func init() { 42 describeCmd.AddCommand(createDescribeAccountCmd()) 43 } 44 45 type DescribeAccountParams struct { 46 AccountContextParams 47 jwt.AccountClaims 48 outputFile string 49 raw []byte 50 } 51 52 func (p *DescribeAccountParams) SetDefaults(ctx ActionCtx) error { 53 p.AccountContextParams.Name = NameFlagOrArgument(p.AccountContextParams.Name, ctx) 54 return p.AccountContextParams.SetDefaults(ctx) 55 } 56 57 func (p *DescribeAccountParams) PreInteractive(ctx ActionCtx) error { 58 return p.AccountContextParams.Edit(ctx) 59 } 60 61 func (p *DescribeAccountParams) Load(ctx ActionCtx) error { 62 var err error 63 64 if err = p.AccountContextParams.Validate(ctx); err != nil { 65 return err 66 } 67 if Json || Raw || JsonPath != "" { 68 p.raw, err = ctx.StoreCtx().Store.ReadRawAccountClaim(p.AccountContextParams.Name) 69 if err != nil { 70 return err 71 } 72 if Json || JsonPath != "" { 73 p.raw, err = bodyAsJson(p.raw) 74 if err != nil { 75 return err 76 } 77 if JsonPath != "" { 78 p.raw, err = GetField(p.raw, JsonPath) 79 if err != nil { 80 return err 81 } 82 } 83 } 84 } else { 85 ac, err := ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name) 86 if err != nil { 87 return err 88 } 89 p.AccountClaims = *ac 90 } 91 92 return nil 93 } 94 95 func (p *DescribeAccountParams) Validate(_ ActionCtx) error { 96 return nil 97 } 98 99 func (p *DescribeAccountParams) PostInteractive(_ ActionCtx) error { 100 return nil 101 } 102 103 func (p *DescribeAccountParams) Run(_ ActionCtx) (store.Status, error) { 104 if Raw || Json || JsonPath != "" { 105 if !IsStdOut(p.outputFile) && Raw { 106 var err error 107 p.raw, err = jwt.DecorateJWT(string(p.raw)) 108 if err != nil { 109 return nil, err 110 } 111 } 112 p.raw = append(p.raw, '\n') 113 if err := Write(p.outputFile, p.raw); err != nil { 114 return nil, err 115 } 116 } else { 117 v := NewAccountDescriber(p.AccountClaims).Describe() 118 if err := Write(p.outputFile, []byte(v)); err != nil { 119 return nil, err 120 } 121 } 122 var s store.Status 123 if !IsStdOut(p.outputFile) { 124 k := "description" 125 if Raw { 126 k = "jwt" 127 } 128 s = store.OKStatus("wrote account %s to %#q", k, AbbrevHomePaths(p.outputFile)) 129 } 130 return s, nil 131 }