show.go 1.24 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
	"github.com/spf13/cobra"
Loïck Bonniot's avatar
Loïck Bonniot committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
)

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
26
27
28
29
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
30
31
}

ElyKar's avatar
ElyKar committed
32
func showContract(cmd *cobra.Command, args []string) {
33
34
35
36
37
	if len(args) != 1 {
		_ = cmd.Usage()
		return
	}

Loïck Bonniot's avatar
Loïck Bonniot committed
38
39
40
41
42
	filename := args[0]
	c := getContract(filename)
	if c == nil {
		return
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
43

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

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

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
}