oasis.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. )
  8. // The relative path to the `node` binary depends on the platform, so we
  9. // pass this via an `-ldflags` hack I don't completely understand. In my
  10. // head this is similar to how GCC lets you use `-D` to define a macro to
  11. // be inserted by the preprocessor.
  12. var node string
  13. func main() {
  14. // The problem with relative paths is that they only work when
  15. // you run `./oasis-platform-x64`, but not when you run a command
  16. // like `./path/to/oasis-platform-x64`. To resolve this problem
  17. // we need to put together an absolute path, which we can build
  18. // with the first argument (the relative path of this executable)
  19. // and the relative path of either the `node` binary or the
  20. // source code directory so that we can run `node src`.
  21. node := filepath.Join(filepath.Dir(os.Args[0]), node)
  22. src := filepath.Join(filepath.Dir(os.Args[0]), "src")
  23. // We know that the command will be the absolute path to `node`
  24. // and the first argument will be the absolute path to the `src`
  25. // directory, but we need to get collect the rest of the arguments
  26. // programatically by pulling them out of the `os.Args` slice and
  27. // putting them in a new slice called `args`.
  28. args := []string{src}
  29. for i := 1; i < len(os.Args); i++ {
  30. args = append(args, os.Args[i])
  31. }
  32. // This seems to execute the script and pass-through all of the
  33. // arguments we want, *plus* it hooks up stdout and stderr, but
  34. // the exit code of Oasis doesn't seem to be passed through. This
  35. // is easy to test with a command like:
  36. //
  37. // ./oasis-platform-x64 --port -1
  38. //
  39. // This should give an exit code of 1, but it seems to exit 0. :/
  40. cmd := exec.Command(node, args...)
  41. cmd.Stdout = os.Stdout
  42. cmd.Stderr = os.Stderr
  43. // This catches problems like "no such file or directory" if the
  44. // `node` variable points to a path where there isn't a binary.
  45. //
  46. // TODO: I think we're supposed to handle the exit code here.
  47. err := cmd.Run()
  48. if err != nil {
  49. fmt.Println(err)
  50. }
  51. }