github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/wsctl/cmd/publisher/create.go (about) 1 package publisher 2 3 import ( 4 "bytes" 5 "fmt" 6 "net/http" 7 8 "github.com/pkg/errors" 9 "github.com/spf13/cobra" 10 "golang.org/x/text/cases" 11 "golang.org/x/text/language" 12 13 "github.com/machinefi/w3bstream/pkg/wsctl/client" 14 "github.com/machinefi/w3bstream/pkg/wsctl/cmd/utils" 15 "github.com/machinefi/w3bstream/pkg/wsctl/config" 16 ) 17 18 var ( 19 _publisherCreateUse = map[config.Language]string{ 20 config.English: "create PROJECT_ID PUB_NAME PUB_KEY", 21 config.Chinese: "create PROJECT_ID PUB_NAME PUB_KEY", 22 } 23 _publisherCreateCmdShorts = map[config.Language]string{ 24 config.English: "Create a publisher", 25 config.Chinese: "通过 PROJECT_ID, PUB_NAME, PUB_KEY 创建 PUBLISHER", 26 } 27 ) 28 29 // newPublisherCreateCmd is a command to create publisher 30 func newPublisherCreateCmd(client client.Client) *cobra.Command { 31 return &cobra.Command{ 32 Use: client.SelectTranslation(_publisherCreateUse), 33 Short: client.SelectTranslation(_publisherCreateCmdShorts), 34 Args: func(cmd *cobra.Command, args []string) error { 35 if len(args) != 3 { 36 return fmt.Errorf("accepts 3 arg(s), received %d", len(args)) 37 } 38 return nil 39 }, 40 RunE: func(cmd *cobra.Command, args []string) error { 41 cmd.SilenceUsage = true 42 if err := create(cmd, client, args); err != nil { 43 return errors.Wrap(err, fmt.Sprintf("problem create publisher %+v", args)) 44 } 45 cmd.Println(cases.Title(language.Und).String(args[0]) + " publisher created successfully ") 46 return nil 47 }, 48 } 49 } 50 51 func create(cmd *cobra.Command, client client.Client, args []string) error { 52 body := fmt.Sprintf(`{"name":"%s", "key":"%s"}`, args[1], args[2]) 53 createURL := GetPublisherCmdUrl(client.Config().Endpoint, args[0]) 54 req, err := http.NewRequest("POST", createURL, bytes.NewBuffer([]byte(body))) 55 if err != nil { 56 return errors.Wrap(err, "failed to create publisher request") 57 } 58 req.Header.Set("Content-Type", "application/json") 59 60 resp, err := client.Call(createURL, req) 61 if err != nil { 62 return errors.Wrap(err, "failed to create publisher") 63 } 64 return utils.PrintResponse(cmd, resp) 65 }