Commit bddb4a8d authored by Loïck Bonniot's avatar Loïck Bonniot

[c] Add show command

parent 14faf31a
Pipeline #255 passed with stage
package common
import (
"encoding/json"
"dfss/dfssp/contract"
)
// UnmarshalDFSSFile decodes a json-encoded DFSS file
func UnmarshalDFSSFile(data []byte) (*contract.JSON, error) {
c := &contract.JSON{}
err := json.Unmarshal(data, c)
if err != nil {
return nil, err
}
return c, nil
}
package common
import (
"io/ioutil"
"testing"
"github.com/bmizerany/assert"
)
func TestUnmarshalDFSSFile(t *testing.T) {
data, err := ioutil.ReadFile("../testdata/file.dfss")
assert.Equal(t, nil, err)
c, err := UnmarshalDFSSFile(data)
assert.Equal(t, nil, err)
assert.Equal(t, "filename.pdf", c.File.Name)
// a tiny test, full unmarshal is tested in dfss/dfssp/contract package
}
......@@ -38,6 +38,7 @@ func init() {
fmt.Println(" help print this help")
fmt.Println(" version print dfss client version")
fmt.Println(" register register a new client")
fmt.Println(" show <c> print contract information from file c")
fmt.Println("\nFlags:")
flag.PrintDefaults()
......@@ -85,6 +86,8 @@ func main() {
fmt.Println("An error occurred : ", err.Error())
}
case "show":
showContract(flag.Arg(1))
default:
flag.Usage()
}
......
package main
import (
"bytes"
"fmt"
"io/ioutil"
"text/template"
"dfss/dfssc/common"
)
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}}`
func showContract(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("Cannot open file:", err)
return
}
c, err := common.UnmarshalDFSSFile(data)
if err != nil {
fmt.Println("Corrupted file:", err)
return
}
tmpl, err := template.New("contract").Parse(contractShowTemplate)
if err != nil {
fmt.Println("Internal error:", err)
return
}
b := new(bytes.Buffer)
err = tmpl.Execute(b, c)
if err != nil {
fmt.Println("Cannot print contract:", err)
}
fmt.Print(b.String())
}
{
"UUID": "00112233445566778899aabb",
"Date": "2000-01-02T03:04:05.000000006-05:00",
"Comment": "A test comment\nallow multiline and accents: éÉ",
"File": {
"Name": "filename.pdf",
"Hash": "hash",
"Hosted": false
},
"Signers": [
{
"Email": "a",
"Hash": "ha"
},
{
"Email": "b",
"Hash": "hb"
}
],
"Sequence": null,
"TTP": null
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment