github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/wsctl/cmd/project/create.go (about) 1 package project 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 _projectCreateUse = map[config.Language]string{ 20 config.English: "create PROJECT_NAME", 21 config.Chinese: "create project名称", 22 } 23 _projectCreateCmdShorts = map[config.Language]string{ 24 config.English: "Create a new project", 25 config.Chinese: "创建一个新的project", 26 } 27 ) 28 29 // newProjectCreateCmd is a command to create project 30 func newProjectCreateCmd(client client.Client) *cobra.Command { 31 return &cobra.Command{ 32 Use: client.SelectTranslation(_projectCreateUse), 33 Short: client.SelectTranslation(_projectCreateCmdShorts), 34 Args: func(cmd *cobra.Command, args []string) error { 35 if len(args) != 1 { 36 return fmt.Errorf("accepts 1 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 project %+v", args)) 44 } 45 cmd.Println(cases.Title(language.Und).String(args[0]) + " project 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"}`, args[0]) 53 url := fmt.Sprintf("%s/srv-applet-mgr/v0/project", client.Config().Endpoint) 54 req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(body))) 55 if err != nil { 56 return errors.Wrap(err, "failed to create project request") 57 } 58 req.Header.Set("Content-Type", "application/json") 59 60 resp, err := client.Call(url, req) 61 if err != nil { 62 return errors.Wrap(err, "failed to create project") 63 } 64 return utils.PrintResponse(cmd, resp) 65 }