Skip to content

Commit

Permalink
Merge pull request #5 from aydinnyunus/check_env_modification
Browse files Browse the repository at this point in the history
Check env modification
  • Loading branch information
aydinnyunus authored Jun 23, 2024
2 parents cacef4b + 949f913 commit ff0e2b3
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 464 deletions.
2 changes: 1 addition & 1 deletion cli/command/neodash/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func CreateNeodash(args models.ScammerQueryArgs) ([]byte, error) {

// DefineSchema is a temporary method to satisfy the authentication process.
func DefineSchema(args models.ScammerQueryArgs) ([]byte, error) {
cmdStr := "sudo sh define_schema.sh"
cmdStr := "sudo bash define_schema.sh"
out, _ := exec.Command("/bin/sh", "-c", cmdStr).Output()
color.Yellow(string(out))
return out, nil
Expand Down
8 changes: 5 additions & 3 deletions cli/command/repository/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const (
)

var (
Neo4jUri = getEnv("NEO4J_URI", "neo4j://localhost:7687")
Neo4jUser = getEnv("NEO4J_USERNAME", "neo4j")
Neo4jPass = getEnv("NEO4J_PASS", "letmein")
Neo4jUri = GetEnv("NEO4J_URI", "neo4j://localhost:7687")
Neo4jUser = GetEnv("NEO4J_USERNAME", "neo4j")
Neo4jPass = GetEnv("NEO4J_PASS", "letmein")
ContainerName = "neo4j"
Neo4jAuth = "NEO4J_AUTH"
exchange = []string{"uniswap", "bitfinex"}
SatoshiToBitcoin = float64(100000000)
ExitNodes []string
Expand Down
52 changes: 51 additions & 1 deletion cli/command/repository/generic.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package repository

import (
"fmt"
"github.com/joho/godotenv"
"log"
"os"
"os/exec"
"strings"
)

func getEnv(key, fallback string) string {
func GetEnv(key, fallback string) string {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
Expand Down Expand Up @@ -38,3 +40,51 @@ func StringInSlice(a string, list []string) bool {
}
return false
}

// Function to restart Docker Compose
func RestartDockerCompose() {
commands := []string{
"docker compose down",
"sudo bash define_schema.sh",
"docker compose up -d",
}

for _, cmd := range commands {
command := exec.Command("sh", "-c", cmd)
command.Stdout = os.Stdout
command.Stderr = os.Stderr

err := command.Run()
if err != nil {
log.Printf("Command failed: %s\n", cmd)
log.Fatal(err)
}
}
}

func GetDockerEnvVar(containerName, key string) (string, error) {
cmd := exec.Command("docker", "exec", containerName, "printenv", key)
output, err := cmd.Output()
if err != nil {
return "", err
}

return strings.TrimSpace(string(output)), nil
}

func ContainerExists(containerName string) bool {
cmd := exec.Command("docker", "ps", "--filter", fmt.Sprintf("name=%s", containerName), "--format", "{{.Names}}")
output, err := cmd.Output()
if err != nil {
log.Printf("Error checking if container exists: %v", err)
return false
}

containerNames := strings.Split(strings.TrimSpace(string(output)), "\n")
for _, name := range containerNames {
if name == containerName {
return true
}
}
return false
}
27 changes: 27 additions & 0 deletions cmd/wallet-tracker/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"fmt"
generic "github.com/aydinnyunus/wallet-tracker/cli/command/repository"
"github.com/fatih/color"
"github.com/joho/godotenv"
"log"
"os"
Expand All @@ -18,6 +21,30 @@ func main() {
log.Fatalf("Error loading .env file")
}

neo4jUser := generic.GetEnv("NEO4J_USERNAME", "neo4j")
neo4jPass := generic.GetEnv("NEO4J_PASS", "letmein")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}

if generic.ContainerExists(generic.ContainerName) {
dockerEnvVarValue, err := generic.GetDockerEnvVar(generic.ContainerName, generic.Neo4jAuth)
if err != nil {
log.Fatalf("Error getting Docker env var: %v", err)
}

envVarValue := neo4jUser + "/" + neo4jPass

if envVarValue == dockerEnvVarValue {
fmt.Println("The .env NEO4J_AUTH value matches the Docker container NEO4J_AUTH value.")
} else {
generic.RestartDockerCompose()
}

} else {
color.Red("Please start neo4j database using ./wallet-tracker neodash start")
}

rootCmd = commands.NewWalletTrackerCommand()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
Expand Down
Loading

0 comments on commit ff0e2b3

Please sign in to comment.