github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/setdccstatus.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"strconv"
    11  	"strings"
    12  
    13  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    14  	"github.com/decred/politeia/politeiawww/cmd/shared"
    15  )
    16  
    17  type SetDCCStatusCmd struct {
    18  	Args struct {
    19  		Token  string `positional-arg-name:"token"`
    20  		Status string `positional-arg-name:"status"`
    21  		Reason string `positional-arg-name:"reason"`
    22  	} `positional-args:"true" optional:"true"`
    23  }
    24  
    25  func (cmd *SetDCCStatusCmd) Execute(args []string) error {
    26  	DCCStatus := map[string]cms.DCCStatusT{
    27  		"rejected": cms.DCCStatusRejected,
    28  		"approved": cms.DCCStatusApproved,
    29  	}
    30  	// Check for user identity
    31  	if cfg.Identity == nil {
    32  		return shared.ErrUserIdentityNotFound
    33  	}
    34  
    35  	// Parse DCC status. This can be either the numeric status
    36  	// code or the human readable equivalent.
    37  	var status cms.DCCStatusT
    38  	s, err := strconv.ParseUint(cmd.Args.Status, 10, 32)
    39  	if err == nil {
    40  		// Numeric status code found
    41  		status = cms.DCCStatusT(s)
    42  	} else if s, ok := DCCStatus[strings.ToLower(cmd.Args.Status)]; ok {
    43  		// Human readable status code found
    44  		status = s
    45  	} else {
    46  		return fmt.Errorf("Invalid status: '%v'.  "+
    47  			"Valid statuses are:\n"+
    48  			"  rejected  reject the DCC\n"+
    49  			"  approved  approve the DCC",
    50  			cmd.Args.Status)
    51  	}
    52  
    53  	sig := cfg.Identity.SignMessage([]byte(cmd.Args.Token +
    54  		strconv.Itoa(int(status)) + cmd.Args.Reason))
    55  	sd := &cms.SetDCCStatus{
    56  		Token:     cmd.Args.Token,
    57  		Status:    status,
    58  		Reason:    cmd.Args.Reason,
    59  		PublicKey: hex.EncodeToString(cfg.Identity.Public.Key[:]),
    60  		Signature: hex.EncodeToString(sig[:]),
    61  	}
    62  
    63  	// Print request details
    64  	err = shared.PrintJSON(sd)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	// Send request
    70  	sisr, err := client.SetDCCStatus(sd)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	return shared.PrintJSON(sisr)
    76  }