Skip to content

Commit

Permalink
fix(cosmos): also look for ag-chain-cosmos in the agd directory
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 6, 2021
1 parent f0e42b1 commit f598d40
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
46 changes: 46 additions & 0 deletions golang/cosmos/cmd/agd/find_binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"os"
"os/exec"
"path/filepath"
)

// FindBinaryNextToMe looks for binName next to the current executable.
// It returns an absolute filename for binName, or an error.
func FindBinaryNextToMe(binName string) (string, error) {
ex, err := os.Executable()
if err != nil {
return "", err
}

// Calculate the binary's filename.
bin := filepath.Join(filepath.Dir(ex), binName)

// Take the absolute path.
bin, err = filepath.Abs(bin)
if err != nil {
return "", err
}

// Check that the binary exists.
if _, err := os.Stat(bin); err != nil {
return "", err
}
return bin, nil
}

// FindBinary looks for binName in the following locations:
// 1. The executable's directory
// 2. The system PATH
//
// It returns the absolute filename for binName if it is found, otherwise an
// error.
func FindBinary(binName string) (string, error) {
if binary, err := FindBinaryNextToMe(binName); err == nil {
return binary, nil
}

// Check the system PATH.
return exec.LookPath(binName)
}
11 changes: 5 additions & 6 deletions golang/cosmos/cmd/agd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"os"
"os/exec"
"syscall"

"github.com/tendermint/tendermint/libs/log"
Expand All @@ -15,16 +14,16 @@ import (
func main() {
// We need to delegate to our default app for running the actual chain.
daemoncmd.OnStartHook = func(logger log.Logger) {
logger.Info("Start chain delegating to ag-chain-cosmos executable")
args := []string{"ag-chain-cosmos", "--home", gaia.DefaultNodeHome}
args = append(args, os.Args[1:]...)

logger.Info("Start chain delegating to JS executable", "args", args)

binary, lookErr := exec.LookPath("ag-chain-cosmos")
binary, lookErr := FindBinary(args[0])
if lookErr != nil {
panic(lookErr)
}

args := []string{"ag-chain-cosmos", "--home", gaia.DefaultNodeHome}
args = append(args, os.Args[1:]...)

execErr := syscall.Exec(binary, args, os.Environ())
if execErr != nil {
panic(execErr)
Expand Down

0 comments on commit f598d40

Please sign in to comment.