-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.go
71 lines (59 loc) · 1.24 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"text/template"
"dfss/dfssc/common"
"dfss/dfssp/contract"
"github.com/spf13/cobra"
)
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}}`
var showCmd = &cobra.Command{
Use: "show <c>",
Short: "print contract information from file c",
Run: showContract,
}
func showContract(cmd *cobra.Command, args []string) {
if len(args) != 1 {
_ = cmd.Usage()
return
}
filename := args[0]
c := getContract(filename)
if c == nil {
return
}
b := new(bytes.Buffer)
tmpl, err := template.New("contract").Parse(contractShowTemplate)
if err != nil {
fmt.Println("Internal error:", err)
return
}
err = tmpl.Execute(b, c)
if err != nil {
fmt.Println("Cannot print contract:", err)
}
fmt.Print(b.String())
}
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
}