-
Notifications
You must be signed in to change notification settings - Fork 40
/
info.go
175 lines (153 loc) · 4.71 KB
/
info.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/streamingfast/cli"
"github.com/streamingfast/cli/sflags"
"github.com/streamingfast/substreams/info"
"github.com/spf13/cobra"
)
func init() {
infoCmd.Flags().String("output-sinkconfig-files-path", "", "if non-empty, any sinkconfig field of type 'bytes' that was packed from a file will be written to that path")
infoCmd.Flags().Bool("skip-package-validation", false, "Do not perform any validation when reading substreams package")
}
var infoCmd = &cobra.Command{
Use: "info [<manifest_file> [<output_module>]]",
Short: "Display package modules and docs",
Long: cli.Dedent(`
Display package modules and docs. The manifest is optional as it will try to find a file named
'substreams.yaml' in current working directory if nothing entered. You may enter a directory that contains
a 'substreams.yaml' file in place of '<manifest_file>, or a link to a remote .spkg file, using urls gs://, http(s)://, ipfs://, etc.'.
Specify an "output_module" to see how processing can be divided in different stages to produce the requested output.
`),
RunE: runInfo,
Args: cobra.RangeArgs(0, 2),
SilenceUsage: true,
}
func init() {
infoCmd.Flags().Bool("json", false, "Output as JSON")
rootCmd.AddCommand(infoCmd)
}
func runInfo(cmd *cobra.Command, args []string) error {
manifestPath := ""
if len(args) != 0 {
manifestPath = args[0]
}
var outputModule string
if len(args) == 2 {
outputModule = args[1]
}
outputSinkconfigFilesPath := sflags.MustGetString(cmd, "output-sinkconfig-files-path")
info, err := info.Extended(manifestPath, outputModule, sflags.MustGetBool(cmd, "skip-package-validation"))
if err != nil {
return err
}
if sflags.MustGetBool(cmd, "json") {
res, err := json.MarshalIndent(info, "", " ")
if err != nil {
return err
}
fmt.Println(string(res))
return nil
}
fmt.Println("Package name:", info.Name)
fmt.Println("Version:", info.Version)
if doc := info.Documentation; doc != nil && *doc != "" {
fmt.Println("Doc: " + strings.Replace(*doc, "\n", "\n ", -1))
}
if info.Image != nil {
fmt.Printf("Image: [embedded image: %d bytes]\n", len(info.Image))
}
fmt.Println("Modules:")
fmt.Println("----")
for _, mod := range info.Modules {
fmt.Println("Name:", mod.Name)
fmt.Println("Initial block:", mod.InitialBlock)
fmt.Println("Kind:", mod.Kind)
for _, input := range mod.Inputs {
fmt.Printf("Input: %s: %s\n", input.Type, input.Name)
}
if mod.BlockFilter != nil {
fmt.Printf("Block Filter: (using *%s*): `%s`\n", mod.BlockFilter.Module, mod.BlockFilter.Query)
}
switch mod.Kind {
case "index":
fmt.Println("Output Type:", *mod.OutputType)
case "map":
fmt.Println("Output Type:", *mod.OutputType)
case "store":
fmt.Println("Value Type:", *mod.ValueType)
fmt.Println("Update Policy:", *mod.UpdatePolicy)
default:
fmt.Println("Kind: Unknown")
}
fmt.Println("Hash:", mod.Hash)
if doc := mod.Documentation; doc != nil && *doc != "" {
fmt.Println("Doc: ", *doc)
}
fmt.Println("")
}
if info.Network != "" {
fmt.Printf("Network: %s\n", info.Network)
fmt.Println("")
}
if info.Networks != nil {
fmt.Println("Networks:")
for network, params := range info.Networks {
fmt.Printf(" %s:\n", network)
if params.InitialBlocks != nil {
fmt.Println(" Initial Blocks:")
}
for mod, start := range params.InitialBlocks {
fmt.Printf(" - %s: %d\n", mod, start)
}
if params.Params != nil {
fmt.Println(" Params:")
}
for mod, p := range params.Params {
fmt.Printf(" - %s: %q\n", mod, p)
}
fmt.Println("")
}
}
if outputModule != "" {
stages := info.ExecutionStages
for i, layers := range stages {
var layerDefs []string
for _, l := range layers {
var mods []string
mods = append(mods, l...)
layerDefs = append(layerDefs, fmt.Sprintf(`["%s"]`, strings.Join(mods, `","`)))
}
fmt.Printf("Stage %d: [%s]\n", i, strings.Join(layerDefs, `,`))
}
}
if info.SinkInfo != nil {
fmt.Println("Sink config:")
fmt.Println("----")
fmt.Println("type:", info.SinkInfo.TypeUrl)
fmt.Println("configs:")
fmt.Println(info.SinkInfo.Configs)
if outputSinkconfigFilesPath != "" && info.SinkInfo.Files != nil {
if err := os.MkdirAll(outputSinkconfigFilesPath, 0755); err != nil {
return err
}
fmt.Println("output files:")
for k, v := range info.SinkInfo.Files {
filename := filepath.Join(outputSinkconfigFilesPath, k)
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
if _, err := f.Write(v); err != nil {
return err
}
fmt.Printf(" - %q written to %q\n", k, filename)
}
}
}
return nil
}