diff --git a/codegen/helpers.go b/codegen/helpers.go index f76d1f3e..30c469ae 100644 --- a/codegen/helpers.go +++ b/codegen/helpers.go @@ -81,7 +81,11 @@ func processMessage(message *descriptorpb.DescriptorProto, parentName string, pr } } -func buildGenerateCommandFromArgs(manifestPath, moduleName, outputType string, withDevEnv bool) error { +func buildGenerateCommandFromArgs(manifestPath, outputType string, withDevEnv bool) error { + _, err := os.Stat(".devcontainer") + + isInDevContainer := !os.IsNotExist(err) + reader, err := manifest.NewReader(manifestPath) if err != nil { return fmt.Errorf("manifest reader: %w", err) @@ -92,7 +96,19 @@ func buildGenerateCommandFromArgs(manifestPath, moduleName, outputType string, w return fmt.Errorf("read manifest %q: %w", manifestPath, err) } - requestedModule, err := getModule(pkg, moduleName) + moduleNames := []string{} + for _, module := range pkg.Modules.Modules { + if module.Output != nil { + moduleNames = append(moduleNames, module.Name) + } + } + + selectedModule, err := createRequestModuleForm(moduleNames) + if err != nil { + return fmt.Errorf("creating request module form: %w", err) + } + + requestedModule, err := getModule(pkg, selectedModule) if err != nil { return fmt.Errorf("getting module: %w", err) } @@ -130,9 +146,16 @@ func buildGenerateCommandFromArgs(manifestPath, moduleName, outputType string, w return fmt.Errorf("rendering project files: %w", err) } - saveDir, err := createSaveDirForm() - if err != nil { - fmt.Println("creating save directory: %w", err) + saveDir := "subgraph" + if cwd, err := os.Getwd(); err == nil { + saveDir = filepath.Join(cwd, saveDir) + } + + if !isInDevContainer { + saveDir, err = createSaveDirForm(saveDir) + if err != nil { + fmt.Println("creating save directory: %w", err) + } } err = saveProjectFiles(projectFiles, saveDir) @@ -143,12 +166,7 @@ func buildGenerateCommandFromArgs(manifestPath, moduleName, outputType string, w return nil } -func createSaveDirForm() (string, error) { - saveDir := "subgraph" - if cwd, err := os.Getwd(); err == nil { - saveDir = filepath.Join(cwd, saveDir) - } - +func createSaveDirForm(saveDir string) (string, error) { inputField := huh.NewInput().Title("In which directory do you want to generate the project?").Value(&saveDir) var WITH_ACCESSIBLE = false @@ -182,3 +200,31 @@ func saveProjectFiles(projectFiles map[string][]byte, saveDir string) error { return nil } + +func createRequestModuleForm(labels []string) (string, error) { + if len(labels) == 0 { + fmt.Println("Hmm, the server sent no option to select from (!)") + } + + var options []huh.Option[string] + optionsMap := make(map[string]string) + for i := 0; i < len(labels); i++ { + entry := huh.Option[string]{ + Key: labels[i], + Value: labels[i], + } + options = append(options, entry) + optionsMap[entry.Value] = entry.Key + } + var selection string + selectField := huh.NewSelect[string](). + Options(options...). + Value(&selection) + + err := huh.NewForm(huh.NewGroup(selectField)).WithTheme(huh.ThemeCharm()).Run() + if err != nil { + return "", fmt.Errorf("failed taking input: %w", err) + } + + return selection, nil +} diff --git a/codegen/sql.go b/codegen/sql.go index c2355c81..b4370a05 100644 --- a/codegen/sql.go +++ b/codegen/sql.go @@ -16,10 +16,9 @@ var SQLCmd = &cobra.Command{ func generateSQLEnv(cmd *cobra.Command, args []string) error { manifestPath := args[0] - moduleName := args[1] withDevEnv := sflags.MustGetBool(cmd, "with-dev-env") - err := buildGenerateCommandFromArgs(manifestPath, moduleName, outputTypeSubgraph, withDevEnv) + err := buildGenerateCommandFromArgs(manifestPath, outputTypeSubgraph, withDevEnv) if err != nil { return fmt.Errorf("building generate command: %w", err) } diff --git a/codegen/subgraph.go b/codegen/subgraph.go index 8cb19acf..472cc07c 100644 --- a/codegen/subgraph.go +++ b/codegen/subgraph.go @@ -8,27 +8,21 @@ import ( ) var SubgraphCmd = &cobra.Command{ - Use: "subgraph [] ", + Use: "subgraph []", Short: "Generate subgraph dev environment from substreams manifest", - Args: cobra.RangeArgs(1, 2), + Args: cobra.RangeArgs(0, 1), RunE: generateSubgraphEnv, } func generateSubgraphEnv(cmd *cobra.Command, args []string) error { manifestPath := "" - moduleName := "" - if len(args) == 2 { - manifestPath = args[0] - moduleName = args[1] - } - if len(args) == 1 { - moduleName = args[0] + manifestPath = args[0] } withDevEnv := sflags.MustGetBool(cmd, "with-dev-env") - err := buildGenerateCommandFromArgs(manifestPath, moduleName, outputTypeSubgraph, withDevEnv) + err := buildGenerateCommandFromArgs(manifestPath, outputTypeSubgraph, withDevEnv) if err != nil { return fmt.Errorf("building generate command: %w", err) }