Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protocol selection in the "substreams init" command #576

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions cmd/substreams/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,49 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to call discovery endpoint: %w", err)
}

var options []huh.Option[*pbconvo.DiscoveryResponse_Generator]
type BlockchainProtocolSelector struct {
Id string
Title string
Generators []*pbconvo.DiscoveryResponse_Generator
}

filteredProtocols := make(map[string]*BlockchainProtocolSelector)
for _, gen := range resp.Msg.Generators {
selector, groupExists := filteredProtocols[gen.Group]
if groupExists {
selector.Generators = append(selector.Generators, gen)
} else {
filteredProtocols[gen.Group] = &BlockchainProtocolSelector{
Id: gen.Group,
Title: generatorGroupToProtocolTitle(gen.Group),
Generators: []*pbconvo.DiscoveryResponse_Generator{gen},
}
}
}

protocolOptions := make([]huh.Option[*BlockchainProtocolSelector], 0, len(filteredProtocols))
for _, value := range filteredProtocols {
protocolOptions = append(protocolOptions, huh.Option[*BlockchainProtocolSelector]{
Key: value.Title,
Value: value,
})
}

var selector *BlockchainProtocolSelector
selectField := huh.NewSelect[*BlockchainProtocolSelector]().
Title("Choose the protocol you inted to work with:").
Options(protocolOptions...).
Value(&selector)

err = huh.NewForm(huh.NewGroup(selectField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking input: %w", err)
}

fmt.Println(gray("┃"), "Chosen protocol: ", bold(selector.Id), "-", selector.Title)

generatorOptions := make([]huh.Option[*pbconvo.DiscoveryResponse_Generator], 0, len(selector.Generators))
for _, gen := range selector.Generators {
endpoint := ""
if gen.Endpoint != "" {
endpoint = " (" + gen.Endpoint + ")"
Expand All @@ -188,21 +229,22 @@ func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
Key: key,
Value: gen,
}
options = append(options, entry)
generatorOptions = append(generatorOptions, entry)
}

var codegen *pbconvo.DiscoveryResponse_Generator
selectField := huh.NewSelect[*pbconvo.DiscoveryResponse_Generator]().
Title("Choose the code generator that you want to use to bootstrap your project").
Options(options...).
selectGeneratorField := huh.NewSelect[*pbconvo.DiscoveryResponse_Generator]().
Title("Choose the type of project that you want to create.").
Options(generatorOptions...).
Value(&codegen)

err = huh.NewForm(huh.NewGroup(selectField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
err = huh.NewForm(huh.NewGroup(selectGeneratorField)).WithTheme(huhTheme).WithAccessible(WITH_ACCESSIBLE).Run()
if err != nil {
return fmt.Errorf("failed taking input: %w", err)
}

fmt.Println(gray("┃"), "Chosen code generator:", bold(codegen.Id), "-", codegen.Title)
fmt.Println(gray("┃"), "Chosen generator: ", bold(codegen.Id), "-", codegen.Title)

lastState.GeneratorID = codegen.Id
generatorID = codegen.Id
}
Expand Down Expand Up @@ -610,6 +652,21 @@ func saveDownloadFile(path string, overwriteForm *OverwriteForm, inputFile *pbco
return nil
}

func generatorGroupToProtocolTitle(group string) string {
switch group {
case "evm":
return "EVM"
case "solana":
return "Solana"
case "cosmos":
return "Cosmos"
case "starknet":
return "Starknet"
}

return ""
}

func ToMarkdown(input string) string {
style := "light"
if lipgloss.HasDarkBackground() {
Expand Down
Loading
Loading