Skip to content

Commit

Permalink
feat(cli): add cli for managing the relay
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 13, 2024
1 parent f98ff71 commit 253c979
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 183 deletions.
Binary file added build/immortal
Binary file not shown.
5 changes: 5 additions & 0 deletions cmd/commands/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package commands

func HandleHelp(_ []string) {
// TODO:::
}
35 changes: 35 additions & 0 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package commands

import (
"errors"
"os"
"os/signal"
"syscall"

"github.com/dezh-tech/immortal/config"
"github.com/dezh-tech/immortal/relay"
)

func HandleRun(args []string) {
if len(args) < 3 {
ExitOnError(errors.New("at least 2 arguments expected, got 1.\nuse help command for more information"))
}

cfg, err := config.LoadfromFile(args[2])
if err != nil {
ExitOnError(err)
}

r := relay.NewRelay(*cfg)
if err := r.Start(); err != nil {
ExitOnError(err)
}

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sigChan

if err := r.Stop(); err != nil {
ExitOnError(err)
}
}
11 changes: 11 additions & 0 deletions cmd/commands/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands

import (
"fmt"
"os"
)

func ExitOnError(err error) {
fmt.Printf("immortal error: %s\n", err.Error()) //nolint
os.Exit(1)
}
30 changes: 24 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package main

import "github.com/dezh-tech/immortal/relay"
import (
"errors"
"fmt"
"os"

// TODO::: create a full functioning CLI to manage rely.
"github.com/dezh-tech/immortal"
"github.com/dezh-tech/immortal/cmd/commands"
)

func main() {
s := relay.NewRelay()
err := s.Start()
if err != nil {
panic(err)
if len(os.Args) < 2 {
commands.ExitOnError(errors.New("at least 2 arguments expected, got 1.\nuse help command for more information"))
}

switch os.Args[1] {
case "run":
commands.HandleRun(os.Args)

case "help":
commands.HandleHelp(os.Args)

case "version":
fmt.Println(immortal.StringVersion()) //nolint
os.Exit(0)

default:
commands.HandleHelp(os.Args)
}
}
50 changes: 50 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package config

import (
"os"

"github.com/dezh-tech/immortal/server"
"gopkg.in/yaml.v3"
)

// Config reprsents the configs used by relay and other concepts on system.
type Config struct {
ServerConf server.Config `yaml:"server"`
DatabseDSN string
}

// LoadfromFile loads config from file, databse and env.
func LoadfromFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, Error{
reason: err.Error(),
}
}
defer file.Close()

config := &Config{}

decoder := yaml.NewDecoder(file)

if err := decoder.Decode(config); err != nil {
return nil, Error{
reason: err.Error(),
}
}

config.DatabseDSN = os.Getenv("IMMO_DB_DSN")

if err = config.basicCheck(); err != nil {
return nil, Error{
reason: err.Error(),
}
}

return config, nil
}

// basicCheck validates the basic stuff in config.
func (c *Config) basicCheck() error {
return nil
}
11 changes: 11 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is default immortal config file.
# This config file contains essential information which is needed for bootstraping.
# The rest of configs such as limitations or NIP-11 profile must be changed on database config table,
# which is defined and documented on documents/config.md.

# server contains information about websocket server.
server:
# bind is the IP address to be bind and listen on.
bind: 127.0.0.1
# port is websocket port to be listen on.
port: 7777
17 changes: 17 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config_test

import (
"testing"

"github.com/dezh-tech/immortal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadfromFile(t *testing.T) {
cfg, err := config.LoadfromFile("./config.yml")
require.NoError(t, err, "error must be nil.")

assert.Equal(t, "7777", cfg.ServerConf.Port)
assert.Equal(t, "127.0.0.1", cfg.ServerConf.Bind)
}
12 changes: 12 additions & 0 deletions config/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import "fmt"

// Error represents an error in loading or validating config.
type Error struct {
reason string
}

func (e Error) Error() string {
return fmt.Sprintf("config error: %s\n", e.reason)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
golang.org/x/net v0.29.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -19,5 +20,4 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ fmt:
check:
golangci-lint run --timeout=20m0s

### Building
build:
go build -o build/immortal cmd/main.go

### pre commit
pre-commit: fmt check unit-test
@echo ready to commit...

.PHONY: build
.PHONY: build
Loading

0 comments on commit 253c979

Please sign in to comment.