diff --git a/Makefile b/Makefile index 05900af..f680466 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ run: archai - ./archai + ./archai $(ARGS) archai: $(shell find . -type f -regex .*go$) diff --git a/actions/context.go b/actions/context.go index 9b8ac13..e5ba374 100644 --- a/actions/context.go +++ b/actions/context.go @@ -1,6 +1,8 @@ package actions import ( + "encoding/json" + "github.com/scoiatael/archai/http" "github.com/scoiatael/archai/persistence" "github.com/scoiatael/archai/telemetry" @@ -23,4 +25,5 @@ type Context interface { type Action interface { Run(Context) error + json.Marshaler } diff --git a/actions/http_server.go b/actions/http_server.go index 925d1af..17246d2 100644 --- a/actions/http_server.go +++ b/actions/http_server.go @@ -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)) +} diff --git a/actions/migrate.go b/actions/migrate.go index 5d724e4..3da6475 100644 --- a/actions/migrate.go +++ b/actions/migrate.go @@ -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 +} diff --git a/actions/read_events.go b/actions/read_events.go index 2166943..34ef7aa 100644 --- a/actions/read_events.go +++ b/actions/read_events.go @@ -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 +} diff --git a/actions/read_events_to_stream.go b/actions/read_events_to_stream.go index 42beb01..4c0b118 100644 --- a/actions/read_events_to_stream.go +++ b/actions/read_events_to_stream.go @@ -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 +} diff --git a/actions/write_event.go b/actions/write_event.go index cebeb5f..4b38d7a 100644 --- a/actions/write_event.go +++ b/actions/write_event.go @@ -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 } diff --git a/actions/write_event_from_stream.go b/actions/write_event_from_stream.go index 0b847bf..fed0593 100644 --- a/actions/write_event_from_stream.go +++ b/actions/write_event_from_stream.go @@ -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 +} diff --git a/config.go b/config.go index a2cec92..9d620de 100644 --- a/config.go +++ b/config.go @@ -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. @@ -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()) @@ -84,3 +89,7 @@ func (c Config) Run() error { } return nil } + +func (c Config) PrettyPrint() { + util.PrettyPrint(c) +} diff --git a/main.go b/main.go index b66dcb2..b2ab717 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( - "bufio" "os" + "strings" "github.com/scoiatael/archai/actions" "github.com/urfave/cli" @@ -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() } diff --git a/util/pretty_print.go b/util/pretty_print.go new file mode 100644 index 0000000..2a96726 --- /dev/null +++ b/util/pretty_print.go @@ -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("") + } +}