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

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

	"dfss/dfssc/common"
10
	"dfss/dfssp/contract"
Loïck Bonniot's avatar
Loïck Bonniot committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24
)

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}}`

Loïck Bonniot's avatar
Loïck Bonniot committed
25
func getContract(filename string) *contract.JSON {
Loïck Bonniot's avatar
Loïck Bonniot committed
26 27 28
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Println("Cannot open file:", err)
29
		return nil
Loïck Bonniot's avatar
Loïck Bonniot committed
30 31 32 33 34
	}

	c, err := common.UnmarshalDFSSFile(data)
	if err != nil {
		fmt.Println("Corrupted file:", err)
35
		return nil
Loïck Bonniot's avatar
Loïck Bonniot committed
36
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
37 38 39 40 41 42 43 44 45
	return c
}

func showContract(args []string) {
	filename := args[0]
	c := getContract(filename)
	if c == nil {
		return
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
46

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

	err = tmpl.Execute(b, c)
	if err != nil {
		fmt.Println("Cannot print contract:", err)
	}
	fmt.Print(b.String())
}