github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/wsctl/cmd/project/delete.go (about)

     1  package project
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  	"golang.org/x/text/cases"
    10  	"golang.org/x/text/language"
    11  
    12  	"github.com/machinefi/w3bstream/pkg/wsctl/client"
    13  	"github.com/machinefi/w3bstream/pkg/wsctl/cmd/utils"
    14  	"github.com/machinefi/w3bstream/pkg/wsctl/config"
    15  )
    16  
    17  var (
    18  	_projectDeleteUse = map[config.Language]string{
    19  		config.English: "delete PROJECT_NAME",
    20  		config.Chinese: "delete project名称",
    21  	}
    22  	_projectDeleteCmdShorts = map[config.Language]string{
    23  		config.English: "Delete a new project",
    24  		config.Chinese: "删除一个新的project",
    25  	}
    26  )
    27  
    28  // newProjectDeleteCmd is a command to delete project
    29  func newProjectDeleteCmd(client client.Client) *cobra.Command {
    30  	return &cobra.Command{
    31  		Use:   client.SelectTranslation(_projectDeleteUse),
    32  		Short: client.SelectTranslation(_projectDeleteCmdShorts),
    33  		Args: func(cmd *cobra.Command, args []string) error {
    34  			if len(args) != 1 {
    35  				return fmt.Errorf("accepts 1 arg(s), received %d", len(args))
    36  			}
    37  			return nil
    38  		},
    39  		RunE: func(cmd *cobra.Command, args []string) error {
    40  			cmd.SilenceUsage = true
    41  			if err := delete(cmd, client, args); err != nil {
    42  				return errors.Wrap(err, fmt.Sprintf("problem delete project %+v", args))
    43  			}
    44  			cmd.Println(cases.Title(language.Und).String(args[0]) + " project deleted successfully ")
    45  			return nil
    46  		},
    47  	}
    48  }
    49  
    50  func delete(cmd *cobra.Command, client client.Client, args []string) error {
    51  	url := fmt.Sprintf("%s/srv-applet-mgr/v0/project/%s", client.Config().Endpoint, args[0])
    52  	req, err := http.NewRequest("DELETE", url, nil)
    53  	if err != nil {
    54  		return errors.Wrap(err, "failed to delete project request")
    55  	}
    56  	req.Header.Set("Content-Type", "application/json")
    57  
    58  	resp, err := client.Call(url, req)
    59  	if err != nil {
    60  		return errors.Wrap(err, "failed to delete project")
    61  	}
    62  	return utils.PrintResponse(cmd, resp)
    63  }