Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented CLI #62

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 134 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,141 @@
package main

import (
"fmt"
"context"
"flag"

"os"
"os/signal"
"path/filepath"
"sync"
"syscall"

"github.com/BlocSoc-iitr/selene/config"
"github.com/BlocSoc-iitr/selene/client"

Check failure on line 14 in cmd/main.go

View workflow job for this annotation

GitHub Actions / build (1.22.3)

no required module provides package github.com/BlocSoc-iitr/selene/client; to add it:
"github.com/BlocSoc-iitr/selene/utils"

"github.com/sirupsen/logrus"
)

func main() {
fmt.Println("hello selene")
cliConfig := parseFlags()

Check failure on line 21 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: parseFlags (typecheck)

setupLogging()

Check failure on line 23 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: setupLogging (typecheck)

cfg := getConfig(cliConfig)

cl, err := client.NewClient(cfg)
if err != nil {
logrus.Fatalf("Failed to create client: %v", err)
}

if err := cl.Start(); err != nil {
logrus.Fatalf("Failed to start client: %v", err)
}

registerShutdownHandler(cl)

Check failure on line 36 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

undefined: registerShutdownHandler (typecheck)

// Block forever
select {}
}

func ParseFlags() *config.CliConfig {
cliConfig := &config.CliConfig{}

flag.StringVar(cliConfig.ExecutionRpc, "execution-rpc", "", "Execution RPC URL")
flag.StringVar(cliConfig.ConsensusRpc, "consensus-rpc", "", "Consensus RPC URL")
flag.StringVar(cliConfig.RpcBindIp, "rpc-bind-ip", "", "RPC bind IP address")

var rpcPort uint
flag.UintVar(&rpcPort, "rpc-port", 0, "RPC port")
if rpcPort != 0 {
port := uint16(rpcPort)
cliConfig.RpcPort = &port
}

flag.StringVar(cliConfig.DataDir, "data-dir", "", "Data directory")
flag.StringVar(cliConfig.Fallback, "fallback", "", "Fallback URL")

var loadExternalFallback bool
flag.BoolVar(&loadExternalFallback, "load-external-fallback", false, "Load external fallback")
cliConfig.LoadExternalFallback = &loadExternalFallback

var strictCheckpointAge bool
flag.BoolVar(&strictCheckpointAge, "strict-checkpoint-age", false, "Strict checkpoint age")
cliConfig.StrictCheckpointAge = &strictCheckpointAge

var checkpoint string
flag.StringVar(&checkpoint, "checkpoint", "", "Checkpoint")
if checkpoint != "" {
checkpointBytes, err := utils.Hex_str_to_bytes(checkpoint)
if err != nil {
logrus.Fatalf("Failed to parse checkpoint: %v", err)
}
cliConfig.Checkpoint = &checkpointBytes
}

flag.Parse()

return cliConfig
}

func SetupLogging() {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
logrus.SetLevel(logrus.InfoLevel)
}

func getConfig(cliConfig *config.CliConfig) *config.Config {
homeDir, err := os.UserHomeDir()
if err != nil {
logrus.Fatalf("Failed to get home directory: %v", err)
}

configPath := filepath.Join(homeDir, ".selene", "config.toml")
network := "mainnet" // You might want to make this configurable

cfg := config.Config{}.FromFile(&configPath, &network, cliConfig)

Check failure on line 98 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint (/home/runner/work/selene/selene)

config.Config{}.FromFile undefined (type "github.com/BlocSoc-iitr/selene/config".Config has no field or method FromFile) (typecheck)

return &cfg
}

func RegisterShutdownHandler(cl *client.Client) {
var shutdownCounter int
var mu sync.Mutex

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

go func() {
for sig := range c {
mu.Lock()
shutdownCounter++
counter := shutdownCounter
mu.Unlock()

if counter == 3 {
logrus.Info("Forced shutdown")
os.Exit(0)
}

logrus.Infof("Shutting down... press ctrl-c %d more times to force quit", 3-counter)

if counter == 1 {
go func() {
if err := cl.Shutdown(context.Background()); err != nil {
logrus.Errorf("Error during shutdown: %v", err)
}
os.Exit(0)
}()
}

if sig == syscall.SIGTERM {
if err := cl.Shutdown(context.Background()); err != nil {
logrus.Errorf("Error during shutdown: %v", err)
}
os.Exit(0)
}
}
}()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand All @@ -63,6 +65,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
Expand All @@ -88,6 +91,7 @@ golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
Expand Down
Loading