Skip to content

Commit

Permalink
cleanup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mmchougule committed Sep 28, 2023
1 parent 42eda17 commit 870342e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 17 deletions.
95 changes: 95 additions & 0 deletions cli/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cli

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/spf13/cobra"
)

func TestGetCommandsFromConfig(t *testing.T) {
// Mocking the TOML config data
mockData := `
[components]
[components.celestia]
[components.celestia.blah]
binary = "/tmp/vimana/celestia/celestia"
download = "/tmp/vimana/celestia/init.sh"
[components.celestia.bridge]
binary = "/tmp/vimana/celestia/celestia"
download = "/tmp/vimana/celestia/init.sh"
[components.berachain]
[components.berachain.light]
binary = "berachain-light"
download = "/tmp/vimana/berachain/init.sh"
`
// Write mockData to a temporary file
tmpfile, err := ioutil.TempFile("", "example.toml")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // cleanup
if _, err := tmpfile.Write([]byte(mockData)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

// Define a mock commanderRegistry
var mockCommanderRegistry = map[string]NodeCommander{
"celestia-light": NewMockCommander(),
"celestia-bridge": NewMockCommander(),
"avail-light": NewMockCommander(),
}

// Call GetCommandsFromConfig
commands, err := GetCommandsFromConfig(tmpfile.Name(), mockCommanderRegistry)

if err != nil {
t.Fatal(err)
}

if len(commands) != 1 {
t.Fatalf("Expected 1 main command but got %d", len(commands))
}

runCmd := commands[0]
if runCmd.Use != "run" {
t.Fatalf("Expected 'run' command but got '%s'", runCmd.Use)
}

if len(runCmd.Commands()) != 2 {
t.Fatalf("Expected 2 component commands but got %d", len(runCmd.Commands()))
}
}

type MockCommander struct{ BaseCommander }

func NewMockCommander() *MockCommander {
return &MockCommander{
BaseCommander: BaseCommander{NodeType: "light"},
}
}

func (m *MockCommander) Init(cmd *cobra.Command, args []string, mode Mode) error {
fmt.Println("MockCommander.Init() called")
return nil
}

func (m *MockCommander) Start(cmd *cobra.Command, args []string, mode Mode) {
}

func (m *MockCommander) Status(cmd *cobra.Command, args []string, mode Mode) {
}

func (m *MockCommander) Stop(cmd *cobra.Command, args []string, mode Mode) {
}
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ func Execute() {
type userHomeDirFunc func() (string, error)
type getCommandsFromConfigFunc func(string, map[string]cli.NodeCommander) ([]*cobra.Command, error)

var osUserHomeDir userHomeDirFunc = os.UserHomeDir
var getCommandsFromConfig getCommandsFromConfigFunc = cli.GetCommandsFromConfig
var OsUserHomeDir userHomeDirFunc = os.UserHomeDir
var GetCommandsFromConfig getCommandsFromConfigFunc = cli.GetCommandsFromConfig

func InitCLI() error {
home, err := osUserHomeDir()
home, err := OsUserHomeDir()
if err != nil {
log.Fatal(err)
}
configFile := home + "/.vimana/config.toml"

rootCmd = &cobra.Command{Use: "vimana"}

commands, err := getCommandsFromConfig(configFile, CommanderRegistry)
commands, err := GetCommandsFromConfig(configFile, CommanderRegistry)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
return err
Expand Down
25 changes: 12 additions & 13 deletions components/celestia.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package components

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

// Reference from roller
const (
CelestiaRestApiEndpoint = "https://api-arabica-9.consensus.celestia-arabica.com"
DefaultCelestiaRPC = "consensus-full-arabica-9.celestia-arabica.com"
Expand Down Expand Up @@ -36,43 +37,41 @@ func NewCelestiaComponent(root string, home string, node string) *CelestiaCompon
}

func (c *CelestiaComponent) InitializeConfig() error {
// lightNodePath := filepath.Join(os.Getenv("HOME"), c.ConfigDir+"/"+c.NodeType+"-node")
// mkdir -p ~/.vimana/celestia/light-node
fmt.Println("🚀 Creating Celestia ", c.NodeType, " node config dir: ", c.NodeStorePath)
log.Println("🚀 Creating Celestia ", c.NodeType, " node config dir: ", c.NodeStorePath)
// use logging instead of log.Println
if _, err := os.Stat(c.NodeStorePath); os.IsNotExist(err) {
err := os.MkdirAll(c.NodeStorePath, 0755)
if err != nil {
fmt.Println("Error creating ", c.NodeType, " node config dir", err)
log.Println("Error creating ", c.NodeType, " node config dir", err)
return err
}
fmt.Println("🚀 Celestia ", c.NodeType, " node config dir created: ", c.NodeStorePath)
log.Println("🚀 Celestia ", c.NodeType, " node config dir created: ", c.NodeStorePath)
} else {
fmt.Println("🚀 Celestia ", c.NodeType, " node config dir already exists: ", c.NodeStorePath)
log.Println("🚀 Celestia ", c.NodeType, " node config dir already exists: ", c.NodeStorePath)
}

path, err := filepath.Abs(filepath.Join(c.NodeStorePath + "/config.toml"))
if err != nil {
fmt.Println("Error:", err)
log.Println("Error:", err)
return err
}
if _, err := os.Stat(path); os.IsNotExist(err) {
initLightNodeCmd := exec.Command(c.Root, c.NodeType, "init", "--p2p.network",
DefaultCelestiaNetwork, "--node.store", c.NodeStorePath)
err := initLightNodeCmd.Run()
fmt.Println("🚀 initLightNodeCmd", initLightNodeCmd)
log.Println("🚀 initLightNodeCmd", initLightNodeCmd)
if err != nil {
fmt.Println("Error initializing light node config", err)
log.Println("Error initializing light node config", err)
return err
}
fmt.Println("🚀 Celestia light node initialized: ", path)
log.Println("🚀 Celestia light node initialized: ", path)
} else {
fmt.Println("🚀 Celestia light node already initialized: ", path)
log.Println("🚀 Celestia light node already initialized: ", path)
}
return nil
}

func (c *CelestiaComponent) GetStartCmd() *exec.Cmd {
// nodePath := filepath.Join(os.Getenv("HOME"), c.ConfigDir+"/"+c.NodeType+"/light-node")
args := []string{
c.NodeType, "start",
"--core.ip", c.rpcEndpoint,
Expand Down

0 comments on commit 870342e

Please sign in to comment.