Skip to content

Commit

Permalink
fallback to docker if rustfmt is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
amirylm committed Nov 1, 2024
1 parent 8819204 commit a2a446a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This folder contains the Rust implementation of the Chain Selectors module.

## Pre-requisites

As part of the code generation, `rustfmt` is used to format the generated code. If you don't have rust toolchain installed, it will run in docker.

## Build

To build the Chain Selectors module, run the `go generate ./rs` from the root of the project.
13 changes: 8 additions & 5 deletions rs/genchains_rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ func main() {
func rustfmt(src []byte) ([]byte, error) {
tmpFile := path.Join(os.TempDir(), generatedFileName)
if err := os.WriteFile(tmpFile, src, 0644); err != nil {
panic(err)
return nil, err
}
defer os.Remove(tmpFile)

cmd := exec.Command("rustfmt", tmpFile)
if err := cmd.Run(); err != nil {
panic(err)
if err := exec.Command("rustfmt", tmpFile).Run(); err != nil {
// if rustfmt is not installed, try to use docker
cmd := exec.Command("docker", "run", "--rm", "-v", fmt.Sprintf("%s:/usr/src/app/generated_chains.rs", tmpFile), "-w", "/usr/src/app", "rust:1.82-alpine", "/bin/sh", "-c", "rustup component add rustfmt &>/dev/null && rustfmt generated_chains.rs")
if dockerErr := cmd.Run(); dockerErr != nil {
return nil, err
}
}
formatted, err := os.ReadFile(tmpFile)
if err != nil {
panic(err)
return nil, err
}

return formatted, nil
Expand Down

0 comments on commit a2a446a

Please sign in to comment.