Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Read config from cmdline opts
Browse files Browse the repository at this point in the history
  • Loading branch information
scoiatael committed Mar 7, 2017
1 parent da86271 commit 4cc6704
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run: archai
./archai
./archai $(ARGS)


archai: $(shell find . -type f -regex .*go$)
Expand Down
3 changes: 3 additions & 0 deletions actions/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actions

import (
"encoding/json"

"github.com/scoiatael/archai/http"
"github.com/scoiatael/archai/persistence"
"github.com/scoiatael/archai/telemetry"
Expand All @@ -23,4 +25,5 @@ type Context interface {

type Action interface {
Run(Context) error
json.Marshaler
}
4 changes: 4 additions & 0 deletions actions/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ func (hs HttpServer) Run(c Context) error {

func (hs HttpServer) Stop() {
}

func (hs HttpServer) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("Start HTTP server on %s:%d", hs.Addr, hs.Port))
}
4 changes: 4 additions & 0 deletions actions/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ func (a Migrate) Run(c Context) error {
}
return nil
}

func (a Migrate) MarshalJSON() ([]byte, error) {
return []byte(`"Migrate Cassandra keyspace"`), nil
}
4 changes: 4 additions & 0 deletions actions/read_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ func (re *ReadEvents) Run(c Context) error {
re.Events = events
return errors.Wrap(err, fmt.Sprintf("Error reading event from stream %s", re.Stream))
}

func (re *ReadEvents) MarshalJSON() ([]byte, error) {
return []byte(`"Read events"`), nil
}
4 changes: 4 additions & 0 deletions actions/read_events_to_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ func (res ReadEventsToStream) Run(c Context) error {
}
return nil
}

func (res ReadEventsToStream) MarshalJSON() ([]byte, error) {
return []byte(`"Print events from Cassandra to output stream"`), nil
}
7 changes: 6 additions & 1 deletion actions/write_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ func (we WriteEvent) Run(c Context) error {
return errors.Wrap(err, "Obtaining session failed")
}
we.Meta["version"] = c.Version()
return errors.Wrap(session.WriteEvent(we.Stream, we.Payload, we.Meta), fmt.Sprintf("Error writing event to stream %s", we.Stream))
return errors.Wrap(session.WriteEvent(we.Stream, we.Payload, we.Meta),
fmt.Sprintf("Error writing event to stream %s", we.Stream))
}

func (we WriteEvent) MarshalJSON() ([]byte, error) {
return []byte(`"Insert event to Cassandra stream"`), nil
}
4 changes: 4 additions & 0 deletions actions/write_event_from_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ func (wes WriteEventFromStream) Run(c Context) error {
}
}
}

func (wes WriteEventFromStream) MarshalJSON() ([]byte, error) {
return []byte(`"Read event from input and insert into Cassandra stream"`), nil
}
9 changes: 9 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/scoiatael/archai/http"
"github.com/scoiatael/archai/persistence"
"github.com/scoiatael/archai/telemetry"
"github.com/scoiatael/archai/util"
)

// Config is a context for all application actions.
Expand All @@ -23,6 +24,10 @@ type Config struct {
initialized bool
}

func (c *Config) Append(action actions.Action) {
c.Actions = append(c.Actions, action)
}

func (c Config) HandleErr(err error) {
log.Print(err)
c.Telemetry().Failure("server_error", err.Error())
Expand Down Expand Up @@ -84,3 +89,7 @@ func (c Config) Run() error {
}
return nil
}

func (c Config) PrettyPrint() {
util.PrettyPrint(c)
}
51 changes: 44 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"bufio"
"os"
"strings"

"github.com/scoiatael/archai/actions"
"github.com/urfave/cli"
Expand All @@ -13,14 +13,51 @@ func main() {
app.Name = "archai"
app.Usage = "eventstore replacement"
app.Version = Version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "migrate",
Usage: "Migrate Cassandra on startup?",
},
cli.BoolFlag{
Name: "dev-logger",
Usage: "Enable dev logger",
},
cli.StringFlag{
Name: "keyspace",
Value: "archai",
Usage: "Cassandra keyspace to operate in",
},
cli.StringFlag{
Name: "hosts",
Value: "127.0.0.1",
Usage: "Comma-separated list of Cassandra hosts",
},
cli.StringFlag{
Name: "listen",
Value: "127.0.0.1",
Usage: "Address to listen on",
},
cli.Int64Flag{
Name: "port",
Value: 8080,
Usage: "Port to listen on",
},
}
app.Action = func(c *cli.Context) error {
config := Config{Keyspace: "archai_test3", Hosts: []string{"127.0.0.1"}}
config.Actions = []actions.Action{
actions.Migrate{},
// actions.WriteEventFromStream{Stream: "test-stream", Input: os.Stdin},
actions.ReadEventsToStream{Stream: "test-stream", Output: *bufio.NewWriter(os.Stdout)},
actions.HttpServer{Port: 8080, Addr: "127.0.0.1"},
config := Config{}
config.Features = make(map[string]bool)
config.Keyspace = c.String("keyspace")
config.Hosts = strings.Split(c.String("hosts"), ",")
if c.Bool("migrate") {
config.Append(actions.Migrate{})
}
if c.Bool("dev-logger") {
config.Features["dev_logger"] = true
}
config.Append(actions.HttpServer{
Port: c.Int("port"),
Addr: c.String("listen")})
config.PrettyPrint()
return config.Run()
}

Expand Down
21 changes: 21 additions & 0 deletions util/pretty_print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import (
"encoding/json"
"os"
)

type Explain interface {
Name() string
}

func PrettyPrint(object interface{}) {
b, err := json.MarshalIndent(object, "", " ")
if err != nil {
println("Failed to pretty-print:", err)
println(object)
} else {
os.Stdout.Write(b)
println("")
}
}

0 comments on commit 4cc6704

Please sign in to comment.