github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/pubtool.go (about) 1 /* 2 * Copyright 2018-2022 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 "os" 21 "strings" 22 23 "github.com/nats-io/nsc/v2/cmd/store" 24 25 nats "github.com/nats-io/nats.go" 26 27 "github.com/spf13/cobra" 28 ) 29 30 func createPubCmd() *cobra.Command { 31 var params PubParams 32 var cmd = &cobra.Command{ 33 Use: "pub", 34 Short: "Publish to a subject from a NATS account", 35 Example: "nsc tool pub <subject> <opt_payload>", 36 Args: cobra.MinimumNArgs(1), 37 RunE: func(cmd *cobra.Command, args []string) error { 38 if err := RunAction(cmd, args, ¶ms); err != nil { 39 return err 40 } 41 return nil 42 }, 43 } 44 params.BindFlags(cmd) 45 return cmd 46 } 47 48 func init() { 49 toolCmd.AddCommand(createPubCmd()) 50 hidden := createPubCmd() 51 hidden.Hidden = true 52 hidden.Example = "nsc pub <subject> <opt_payload>" 53 GetRootCmd().AddCommand(hidden) 54 } 55 56 // ToolPubParams is the driving struct for the list plans action 57 type PubParams struct { 58 AccountUserContextParams 59 credsPath string 60 natsURLs []string 61 } 62 63 func (p *PubParams) SetDefaults(ctx ActionCtx) error { 64 return p.AccountUserContextParams.SetDefaults(ctx) 65 } 66 67 func (p *PubParams) PreInteractive(ctx ActionCtx) error { 68 return p.AccountUserContextParams.Edit(ctx) 69 } 70 71 func (p *PubParams) Load(ctx ActionCtx) error { 72 p.credsPath = ctx.StoreCtx().KeyStore.CalcUserCredsPath(p.AccountContextParams.Name, p.UserContextParams.Name) 73 if natsURLFlag != "" { 74 p.natsURLs = []string{natsURLFlag} 75 return nil 76 } 77 78 oc, err := ctx.StoreCtx().Store.ReadOperatorClaim() 79 if err != nil { 80 return err 81 } 82 p.natsURLs = oc.OperatorServiceURLs 83 return nil 84 } 85 86 func (p *PubParams) PostInteractive(ctx ActionCtx) error { 87 return nil 88 } 89 90 func (p *PubParams) Validate(ctx ActionCtx) error { 91 if err := p.AccountContextParams.Validate(ctx); err != nil { 92 return err 93 } 94 95 if err := p.UserContextParams.Validate(ctx); err != nil { 96 return err 97 } 98 99 if p.credsPath == "" { 100 return fmt.Errorf("a creds file for account %q/%q was not found", p.AccountContextParams.Name, p.UserContextParams.Name) 101 } 102 103 _, err := os.Stat(p.credsPath) 104 if os.IsNotExist(err) { 105 return fmt.Errorf("%v: %#q", err, p.credsPath) 106 } 107 if len(p.natsURLs) == 0 { 108 return fmt.Errorf("operator %q doesn't have operator_service_urls set", ctx.StoreCtx().Operator.Name) 109 } 110 return nil 111 } 112 113 func (p *PubParams) Run(ctx ActionCtx) (store.Status, error) { 114 nc, err := nats.Connect(strings.Join(p.natsURLs, ", "), 115 createDefaultToolOptions("nsc_pub", ctx, nats.UserCredentials(p.credsPath))...) 116 if err != nil { 117 return nil, err 118 } 119 defer nc.Close() 120 121 subj := ctx.Args()[0] 122 payload := "" 123 if len(ctx.Args()) > 1 { 124 payload = ctx.Args()[1] 125 } 126 if err := nc.Publish(subj, []byte(payload)); err != nil { 127 return nil, err 128 } 129 if err := nc.Flush(); err != nil { 130 return nil, err 131 } 132 if err := nc.LastError(); err != nil { 133 return nil, err 134 } 135 136 ctx.CurrentCmd().Printf("Published [%s] : %q\n", subj, payload) 137 138 return nil, nil 139 }