-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/mecha: add -type flag to mecha create module to support Rust and …
…Zig module templates Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
44bff01
commit 990fbb5
Showing
5 changed files
with
975 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ package main | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
getter "github.com/hashicorp/go-getter" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
|
@@ -61,6 +63,17 @@ func createModule(cCtx *cli.Context) error { | |
os.Chdir("modules") | ||
defer os.Chdir("..") | ||
|
||
tmpltype := cCtx.String("type") | ||
switch { | ||
case tmpltype == "rust": | ||
return downloadFromTemplate(templateName, name) | ||
case tmpltype == "zig": | ||
return downloadFromTemplate(templateName, name) | ||
case tmpltype == "tinygo": | ||
default: | ||
return fmt.Errorf("unknown template type %s", tmpltype) | ||
} | ||
|
||
if err := createFromTemplate(templateName, name); err != nil { | ||
return err | ||
} | ||
|
@@ -85,27 +98,6 @@ func createFromTemplate(templ, proj string) error { | |
os.Exit(1) | ||
} | ||
|
||
// patch the go.mod file to use forked wazero | ||
basename := filepath.Base(proj) | ||
if err := os.Chdir(basename); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
defer os.Chdir("..") | ||
|
||
return nil // TODO: completely remove replaceWazeroWithFork() | ||
} | ||
|
||
func replaceWazeroWithFork() error { | ||
var stdout, stderr bytes.Buffer | ||
cmd := exec.Command("go", "mod", "edit", "-replace", "github.com/tetratelabs/wazero=github.com/hybridgroup/[email protected]") | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
if err := cmd.Run(); err != nil { | ||
fmt.Printf("%s: %v\n%s%s", cmd.String(), err, stderr.Bytes(), stdout.Bytes()) | ||
os.Exit(1) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -122,3 +114,28 @@ func getModuleName() (string, error) { | |
|
||
return strings.TrimSuffix(stdout.String(), "\n"), nil | ||
} | ||
|
||
func downloadFromTemplate(templateName, name string) error { | ||
pwd, err := os.Getwd() | ||
if err != nil { | ||
fmt.Printf("Error getting pwd: %s", err) | ||
return err | ||
} | ||
|
||
opts := []getter.ClientOption{} | ||
client := &getter.Client{ | ||
Ctx: context.Background(), | ||
Src: templateName, | ||
Dst: filepath.Base(name), | ||
Pwd: pwd, | ||
Mode: getter.ClientModeDir, | ||
Options: opts, | ||
} | ||
|
||
if err := client.Get(); err != nil { | ||
fmt.Printf("Error downloading template: %s", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.