Skip to content

Commit

Permalink
added a license subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jun 2, 2019
1 parent 5488312 commit 77d6a09
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 18 deletions.
33 changes: 33 additions & 0 deletions internal/command/license/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************************
* Copyright (c) 2009-2019 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package license

import (
"fmt"

"github.com/emitter-io/emitter/internal/provider/logging"
"github.com/emitter-io/emitter/internal/security/license"
"github.com/jawher/mow.cli"
)

// New generates a new license and secret key pair.
func New(cmd *cli.Cmd) {
cmd.Spec = ""
cmd.Action = func() {
license, secret := license.New()
logging.LogAction("license", fmt.Sprintf("generated new license: %v", license))
logging.LogAction("license", fmt.Sprintf("generated new secret key: %v", secret))
}
}
36 changes: 36 additions & 0 deletions internal/command/license/license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**********************************************************************************
* Copyright (c) 2009-2019 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package license

import (
"testing"

"github.com/jawher/mow.cli"
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
assert.NotPanics(t, func() {
runCommand(New)
})
}

func runCommand(f func(cmd *cli.Cmd), args ...string) {
app := cli.App("emitter", "")
app.Command("test", "", f)
v := []string{"emitter", "test"}
v = append(v, args...)
app.Run(v)
}
10 changes: 5 additions & 5 deletions internal/command/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ var dial = net.Dial
func Run(cmd *cli.Cmd) {
cmd.Spec = "KEY [ -h=<host> ] [ -c=<channel> ] [ -b=<batch> ] [ -s=<size> ]"
var (
key = cmd.StringArg("KEY", "", "Key for the channel (by default a key for the `load/` channel)")
host = cmd.StringOpt("h host", "127.0.0.1:8080", "Host name")
channel = cmd.StringOpt("c channel", "load/", "The channel for load testing")
batch = cmd.IntOpt("b batch", 10, "The size of the batch to send")
size = cmd.IntOpt("s size", 64, "The size of the message to send")
key = cmd.StringArg("KEY", "", "Specifies the key for the channel (by default a key for the `load/` channel).")
host = cmd.StringOpt("h host", "127.0.0.1:8080", "Specifies the broker host name and port. This must follow the <ip:port> format.")
channel = cmd.StringOpt("c channel", "load/", "Specifies the channel for load testing.")
batch = cmd.IntOpt("b batch", 10, "Specifies the number of messages to send at once to the broker at once.")
size = cmd.IntOpt("s size", 64, "Specifies the size of the message to send in bytes.")
)
cmd.Action = func() {
logging.LogAction("client", "starting a load test...")
Expand Down
43 changes: 30 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,68 @@
/**********************************************************************************
* Copyright (c) 2009-2019 Misakai Ltd.
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or(at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see<http://www.gnu.org/licenses/>.
************************************************************************************/

package main

import (
"context"
"fmt"
"os"

"github.com/emitter-io/config/dynamo"
"github.com/emitter-io/config/vault"
"github.com/emitter-io/emitter/internal/broker"
"github.com/emitter-io/emitter/internal/command/license"
"github.com/emitter-io/emitter/internal/command/load"
"github.com/emitter-io/emitter/internal/config"
"github.com/emitter-io/emitter/internal/provider/logging"
"github.com/emitter-io/emitter/internal/security/license"
"github.com/jawher/mow.cli"
)

//go:generate go run internal/broker/generate/assets_gen.go

func main() {
app := cli.App("emitter", "Emitter: the high performance, distributed and low latency publish-subscribe platform.")
app := cli.App("emitter", "Runs the Emitter broker.")
app.Spec = "[ -c=<configuration path> ] "
confPath := app.StringOpt("c config", "emitter.conf", "The configuration file to use for the broker.")
app.Action = func() { run(confPath) }
confPath := app.StringOpt("c config", "emitter.conf", "Specifies the configuration path (file) to use for the broker.")
app.Action = func() { listen(app, confPath) }

// Register sub-commands
app.Command("load", "Runs the load testing client for emitter.", load.Run)
app.Run(os.Args)
app.Command("license", "Manipulates licenses and secret keys.", func(config *cli.Cmd) {
config.Command("new", "Generates a new license and secret key pair.", license.New)
// TODO: add more sub-commands for license
})

app.Run(os.Args)
}

func run(conf *string) {
cfg := config.New(*conf, dynamo.NewProvider(), vault.NewProvider(config.VaultUser))
// Listen starts the service.
func listen(app *cli.Cli, conf *string) {

// Generate a new license if none was provided
cfg := config.New(*conf, dynamo.NewProvider(), vault.NewProvider(config.VaultUser))
if cfg.License == "" {
license, secret := license.New()
logging.LogAction("service", "unable to find a license, make sure 'license' "+
"value is set in the config file or EMITTER_LICENSE environment variable")
logging.LogAction("service", fmt.Sprintf("generated new license: %v", license))
logging.LogAction("service", fmt.Sprintf("generated new secret key: %v", secret))
os.Exit(0)
app.Run([]string{"emitter", "license", "new"})
return
}

// Start new service
svc, err := broker.NewService(context.Background(), cfg)
if err != nil {
panic(err.Error())
logging.LogError("service", "startup", err)
return
}

// Listen and serve
Expand Down

0 comments on commit 77d6a09

Please sign in to comment.