show.go 1.19 KB
Newer Older
ElyKar's avatar
ElyKar committed
1
package cmd
Loïck Bonniot's avatar
Loïck Bonniot committed
2 3 4 5 6 7 8 9

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"text/template"

	"dfss/dfssc/common"
10
	"dfss/dfssp/contract"
ElyKar's avatar
ElyKar committed
11 12

	"github.com/spf13/cobra"
Loïck Bonniot's avatar
Loïck Bonniot committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26
)

const contractShowTemplate = `UUID       : {{.UUID}}
Filename   : {{.File.Name}}
Filehash   : {{.File.Hash}}
Created on : {{.Date.Format "2006-01-02 15:04:05 MST"}}

Comment    :
  {{.Comment}}

Signers    :
{{range .Signers}}  - {{.Email}}
{{end}}`

ElyKar's avatar
ElyKar committed
27 28 29 30
var showCmd = &cobra.Command{
	Use:   "show <c>",
	Short: "print contract information from file c",
	Run:   showContract,
Loïck Bonniot's avatar
Loïck Bonniot committed
31 32
}

ElyKar's avatar
ElyKar committed
33
func showContract(cmd *cobra.Command, args []string) {
Loïck Bonniot's avatar
Loïck Bonniot committed
34 35 36 37 38
	filename := args[0]
	c := getContract(filename)
	if c == nil {
		return
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
39

Loïck Bonniot's avatar
Loïck Bonniot committed
40
	b := new(bytes.Buffer)
Loïck Bonniot's avatar
Loïck Bonniot committed
41 42 43
	tmpl, err := template.New("contract").Parse(contractShowTemplate)
	if err != nil {
		fmt.Println("Internal error:", err)
Loïck Bonniot's avatar
Loïck Bonniot committed
44
		return
Loïck Bonniot's avatar
Loïck Bonniot committed
45 46 47 48 49 50 51 52
	}

	err = tmpl.Execute(b, c)
	if err != nil {
		fmt.Println("Cannot print contract:", err)
	}
	fmt.Print(b.String())
}
ElyKar's avatar
ElyKar committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

func getContract(filename string) *contract.JSON {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println("Cannot open file:", err)
		return nil
	}

	c, err := common.UnmarshalDFSSFile(data)
	if err != nil {
		fmt.Println("Corrupted file:", err)
		return nil
	}
	return c
}