-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
46 lines (42 loc) · 1.02 KB
/
commands.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
package main
import (
"bytes"
"log"
"strings"
"text/template"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
)
type Command struct {
MaxRecords int
Subcommand string
Address base.Address
Filename string
Format string
Rest string
Silent bool
Chain string
}
func (cmd *Command) String() string {
listCmd := `chifra {{.Subcommand}} --cache {{.Address}} --fmt {{.Format}} --chain {{.Chain}} {{.Rest}}`
if !cmd.Silent {
listCmd += " 2>/dev/null"
}
if cmd.Address.IsZero() {
listCmd = strings.Replace(listCmd, " --cache {{.Address}}", "", -1)
} else if cmd.Subcommand == "list" {
listCmd = strings.Replace(listCmd, " --cache", "", -1)
}
if len(cmd.Filename) > 0 {
listCmd += " --output {{.Filename}}"
}
tmpl, err := template.New("greeting").Parse(listCmd)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, cmd)
if err != nil {
log.Fatalf("Error executing template: %v", err)
}
return buf.String()
}