Skip to content

Commit

Permalink
add request module form and .devContainer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Jul 26, 2024
1 parent cacf123 commit b7511d0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
68 changes: 57 additions & 11 deletions codegen/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions codegen/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 4 additions & 10 deletions codegen/subgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ import (
)

var SubgraphCmd = &cobra.Command{
Use: "subgraph [<manifest_url>] <module_name>",
Use: "subgraph [<manifest_url>]",
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)
}
Expand Down

0 comments on commit b7511d0

Please sign in to comment.