github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/did/didserviceremove.go (about)

     1  package did
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	"github.com/iotexproject/iotex-core/ioctl/cmd/action"
     9  	"github.com/iotexproject/iotex-core/ioctl/config"
    10  	"github.com/iotexproject/iotex-core/ioctl/output"
    11  )
    12  
    13  // Multi-language support
    14  var (
    15  	_serviceremoveCmdShorts = map[config.Language]string{
    16  		config.English: "Remove service to DID document using private key from wallet",
    17  		config.Chinese: "用钱包中的私钥从DID document移除服务",
    18  	}
    19  	_serviceremoveCmdUses = map[config.Language]string{
    20  		config.English: "serviceremove [-s SIGNER] RESOLVER_ENDPOINT TAG",
    21  		config.Chinese: "serviceremove [-s 签署人] Resolver端点 标签",
    22  	}
    23  )
    24  
    25  // _didServiceRemoveCmd represents service remove command
    26  var _didServiceRemoveCmd = &cobra.Command{
    27  	Use:   config.TranslateInLang(_serviceremoveCmdUses, config.UILanguage),
    28  	Short: config.TranslateInLang(_serviceremoveCmdShorts, config.UILanguage),
    29  	Args:  cobra.ExactArgs(2),
    30  	RunE: func(cmd *cobra.Command, args []string) error {
    31  		cmd.SilenceUsage = true
    32  		err := removeService(args)
    33  		return output.PrintError(err)
    34  	},
    35  }
    36  
    37  func init() {
    38  	action.RegisterWriteCommand(_didServiceRemoveCmd)
    39  }
    40  
    41  func removeService(args []string) error {
    42  	endpoint := args[0]
    43  
    44  	signature, _, addr, err := signPermit(endpoint)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	serviceReq := &ServiceRemoveRequest{
    50  		Signature: *signature,
    51  		Tag:       args[1],
    52  	}
    53  	serviceBytes, err := json.Marshal(&serviceReq)
    54  	if err != nil {
    55  		return output.NewError(output.ConvertError, "failed to encode request", err)
    56  	}
    57  	return postToResolver(endpoint+"/did/"+addr+"/service/delete", serviceBytes)
    58  }