github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/cmd/spc/export.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  
     8  	"SiaPrime/types"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var (
    14  	renterExportCmd = &cobra.Command{
    15  		Use:   "export",
    16  		Short: "export renter data to various formats",
    17  		Long:  "Export renter data in various formats.",
    18  		// Run field not provided; export requires a subcommand.
    19  	}
    20  
    21  	renterExportContractTxnsCmd = &cobra.Command{
    22  		Use:   "contract-txns [destination]",
    23  		Short: "export the renter's contracts for import to `https://rankings.sia.tech/`",
    24  		Long: "Export the renter's current contract set in JSON format to the specified " +
    25  			"file. Intended for upload to `https://rankings.sia.tech/`.",
    26  		Run: wrap(renterexportcontracttxnscmd),
    27  	}
    28  )
    29  
    30  // renterexportcontracttxnscmd is the handler for the command `siac renter export contract-txns`.
    31  // Exports the current contract set to JSON.
    32  func renterexportcontracttxnscmd(destination string) {
    33  	cs, err := httpClient.RenterContractsGet()
    34  	if err != nil {
    35  		die("Could not retrieve contracts:", err)
    36  	}
    37  	var contractTxns []types.Transaction
    38  	for _, c := range cs.Contracts {
    39  		contractTxns = append(contractTxns, c.LastTransaction)
    40  	}
    41  	destination = abs(destination)
    42  	file, err := os.Create(destination)
    43  	if err != nil {
    44  		die("Could not export to file:", err)
    45  	}
    46  	err = json.NewEncoder(file).Encode(contractTxns)
    47  	if err != nil {
    48  		die("Could not export to file:", err)
    49  	}
    50  	fmt.Println("Exported contract data to", destination)
    51  }