Skip to content

Commit

Permalink
update info cmd to use new info methods. add json flag to output as json
Browse files Browse the repository at this point in the history
  • Loading branch information
colindickson committed Sep 20, 2023
1 parent 2da5c6e commit a0bc31b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
77 changes: 36 additions & 41 deletions cmd/substreams/info.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"encoding/json"
"fmt"
"github.com/streamingfast/substreams/info"
"strings"

"github.com/streamingfast/cli"

"github.com/spf13/cobra"
"github.com/streamingfast/substreams/manifest"
pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
"github.com/streamingfast/substreams/pipeline/outputmodules"
)

// var manifestCmd = &cobra.Command{
Expand All @@ -31,6 +30,7 @@ var infoCmd = &cobra.Command{
}

func init() {
infoCmd.Flags().Bool("json", false, "Output as JSON")
rootCmd.AddCommand(infoCmd)
}

Expand All @@ -45,68 +45,63 @@ func runInfo(cmd *cobra.Command, args []string) error {
outputModule = args[1]
}

manifestReader, err := manifest.NewReader(manifestPath)
if err != nil {
return fmt.Errorf("manifest reader: %w", err)
var infoOptions []info.InfoOption
if outputModule != "" {
infoOptions = append(infoOptions, info.WithOutputModule(outputModule))
}

pkg, err := manifestReader.Read()
info, err := info.Info(manifestPath, infoOptions...)
if err != nil {
return fmt.Errorf("read manifest %q: %w", manifestPath, err)
return err
}

graph, err := manifest.NewModuleGraph(pkg.Modules.Modules)
if err != nil {
return fmt.Errorf("creating module graph: %w", err)
if mustGetBool(cmd, "json") {
res, err := json.MarshalIndent(info, "", " ")
if err != nil {
return err
}
fmt.Println(string(res))
return nil
}

fmt.Println("Package name:", pkg.PackageMeta[0].Name)
fmt.Println("Version:", pkg.PackageMeta[0].Version)
if doc := pkg.PackageMeta[0].Doc; doc != "" {
fmt.Println("Doc: " + strings.Replace(doc, "\n", "\n ", -1))
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))
}

hashes := manifest.NewModuleHashes()

fmt.Println("Modules:")
fmt.Println("----")
for modIdx, module := range pkg.Modules.Modules {
fmt.Println("Name:", module.Name)
fmt.Println("Initial block:", module.InitialBlock)
kind := module.GetKind()
switch v := kind.(type) {
case *pbsubstreams.Module_KindMap_:
fmt.Println("Kind: map")
fmt.Println("Output Type:", v.KindMap.OutputType)
case *pbsubstreams.Module_KindStore_:
fmt.Println("Kind: store")
fmt.Println("Value Type:", v.KindStore.ValueType)
fmt.Println("Update Policy:", v.KindStore.UpdatePolicy)
for _, mod := range info.Modules {
fmt.Println("Name:", mod.Name)
fmt.Println("Initial block:", mod.InitialBlock)
fmt.Println("Kind:", mod.Kind)

switch mod.Kind {
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")
}

hashes.HashModule(pkg.Modules, module, graph)

fmt.Println("Hash:", hashes.Get(module.Name))
moduleMeta := pkg.ModuleMeta[modIdx]
if moduleMeta != nil && moduleMeta.Doc != "" {
fmt.Println("Doc: " + strings.Replace(moduleMeta.Doc, "\n", "\n ", -1))
fmt.Println("Hash:", mod.Hash)
if doc := mod.Documentation; doc != nil && *doc != "" {
fmt.Println("Doc: ", mod.Documentation)
}
fmt.Println("")
}

if outputModule != "" {
outputGraph, err := outputmodules.NewOutputModuleGraph(outputModule, true, pkg.Modules)
if err != nil {
return err
}
for i, layers := range outputGraph.StagedUsedModules() {
stages := info.ExecutionStages
for i, layers := range stages {
var layerDefs []string
for _, l := range layers {
var mods []string
for _, m := range l {
mods = append(mods, m.Name)
mods = append(mods, m)
}
layerDefs = append(layerDefs, fmt.Sprintf(`["%s"]`, strings.Join(mods, `","`)))
}
Expand Down
39 changes: 23 additions & 16 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
)

type ManifestInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Modules []ModulesInfo `json:"modules"`
ProtoFiles []ProtoFileInfo `json:"proto_files"`
Name string `json:"name"`
Version string `json:"version"`
Documentation *string `json:"documentation,omitempty"`
Modules []ModulesInfo `json:"modules"`
ProtoFiles []ProtoFileInfo `json:"proto_files"`

ExecutionStages [][][]string `json:"execution_stages,omitempty"`
}
Expand All @@ -32,8 +33,9 @@ type ModulesInfo struct {
Name string `json:"name"`
Kind string `json:"kind"`
Inputs []ModuleInput `json:"inputs"`
OutputType string `json:"output_type"`
UpdatePolicy *string `json:"update_policy,omitempty"`
OutputType *string `json:"output_type,omitempty"` //for map inputs
ValueType *string `json:"value_type,omitempty"` //for store inputs
UpdatePolicy *string `json:"update_policy,omitempty"` //for store inputs
InitialBlock uint64 `json:"initial_block"`
Documentation *string `json:"documentation,omitempty"`
Hash string `json:"hash"`
Expand Down Expand Up @@ -73,6 +75,14 @@ func Info(manifestPath string, opts ...InfoOption) (*ManifestInfo, error) {
return nil, fmt.Errorf("read manifest %q: %w", manifestPath, err)
}

manifestInfo := &ManifestInfo{
Name: pkg.PackageMeta[0].Name,
Version: pkg.PackageMeta[0].Version,
}
if pkg.PackageMeta[0].Doc != "" {
manifestInfo.Documentation = strPtr(strings.Replace(pkg.PackageMeta[0].Doc, "\n", "\n ", -1))
}

graph, err := manifest.NewModuleGraph(pkg.Modules.Modules)
if err != nil {
return nil, fmt.Errorf("creating module graph: %w", err)
Expand All @@ -94,14 +104,13 @@ func Info(manifestPath string, opts ...InfoOption) (*ManifestInfo, error) {
switch v := kind.(type) {
case *pbsubstreams.Module_KindMap_:
modInfo.Kind = "map"
modInfo.OutputType = v.KindMap.OutputType
modInfo.OutputType = strPtr(v.KindMap.OutputType)
case *pbsubstreams.Module_KindStore_:
modInfo.Kind = "store"
modInfo.OutputType = v.KindStore.ValueType
modInfo.ValueType = strPtr(v.KindStore.ValueType)
modInfo.UpdatePolicy = strPtr(v.KindStore.UpdatePolicy.Pretty())
default:
modInfo.Kind = "unknown"
modInfo.OutputType = "unknown"
}

modMeta := pkg.ModuleMeta[ix]
Expand Down Expand Up @@ -170,13 +179,11 @@ func Info(manifestPath string, opts ...InfoOption) (*ManifestInfo, error) {
}
}

return &ManifestInfo{
Name: pkg.PackageMeta[0].Name,
Version: pkg.PackageMeta[0].Version,
Modules: modules,
ProtoFiles: protoFiles,
ExecutionStages: stages,
}, nil
manifestInfo.Modules = modules
manifestInfo.ProtoFiles = protoFiles
manifestInfo.ExecutionStages = stages

return manifestInfo, nil
}

func strPtr(s string) *string {
Expand Down

0 comments on commit a0bc31b

Please sign in to comment.