Skip to content

Commit

Permalink
feat: remove cobra/viper
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Nelson authored and Will Nelson committed Jun 22, 2019
1 parent d8aee3f commit eea813f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 414 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/gateway.exe
/.gateway*
/.vscode
/spectacles.toml
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,11 @@ A binary for ingesting data from the Discord gateway to a variety of sources.
## Usage

```
gateway [output] [flags]
Flags:
-c, --config string location of config file
-g, --group string broker group to send Discord events to
-h, --help help for spectacles
-l, --loglevel string log level (default "info")
-s, --shards int number of shards to spawn
-t, --token string Discord token used to connect to gateway
Usage of spectacles:
-config string
location of the Spectacles config file (default "spectacles.toml")
-loglevel string
log level for the client (default "info")
```

`output` is a URL to an output destination (including protocol). If output is `-`, output is set to STDOUT: logging is automatically disabled. All destinations (except STDOUT) must adhere to the [Spectacles specification](https://github.com/spec-tacles/spec).

Applications reading from STDOUT should expect uncompressed JSON packets as received from the Discord gateway. No additional information is provided.

### Configuration Options

Config can be specified in JSON, YAML, TOML, HCL, or Java properties files or in environment variables prefixed with `DISCORD_`. We highly recommend providing sensitive information (such as your token) in either file or environment variable form to avoid disclosing it when viewing processes in `htop` or other monitoring tools.
The config file is required and must conform to the [Spectacles standard config file](https://github.com/spec-tacles/spec/blob/276b7e4737658471b3a74cc06df4795c8901ca8e/config.md).
130 changes: 49 additions & 81 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,72 @@
package cmd

import (
"flag"
"io"
"log"
"os"
"time"

"github.com/BurntSushi/toml"
"github.com/spec-tacles/gateway/gateway"
"github.com/spec-tacles/go/broker"
"github.com/spec-tacles/go/config"
"github.com/spec-tacles/go/rest"
"github.com/spec-tacles/go/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var logger = log.New(os.Stdout, "[CMD] ", log.Ldate|log.Ltime|log.Lshortfile)
var logLevels = map[string]int{
"suppress": gateway.LogLevelSuppress,
"info": gateway.LogLevelInfo,
"warn": gateway.LogLevelWarn,
"debug": gateway.LogLevelDebug,
"error": gateway.LogLevelError,
}
var (
logger = log.New(os.Stdout, "[CMD] ", log.Ldate|log.Ltime|log.Lshortfile)
logLevels = map[string]int{
"suppress": gateway.LogLevelSuppress,
"info": gateway.LogLevelInfo,
"warn": gateway.LogLevelWarn,
"debug": gateway.LogLevelDebug,
"error": gateway.LogLevelError,
}
logLevel = flag.String("loglevel", "info", "log level for the client")
configLocation = flag.String("config", "spectacles.toml", "location of the Spectacles config file")
)

var rootCmd = &cobra.Command{
Use: "gateway [output]",
Short: "Connects to the Discord websocket API",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Run runs the CLI app
func Run() {
flag.Parse()

var (
onPacket func(shard int, d *types.ReceivePacket)
output io.ReadWriter
manager *gateway.Manager
b broker.Broker
logLevel = logLevels[viper.GetString("loglevel")]
)
conf := &config.Config{}
_, err := toml.DecodeFile(*configLocation, conf)
if err != nil {
logger.Fatalf("unable to load config: %s\n", err)
}

if len(args) > 0 {
// TODO: support more broker types
b = broker.NewAMQP(viper.GetString("group"), "", nil)
tryConnect(b, args[0])
}
var (
onPacket func(shard int, d *types.ReceivePacket)
output io.ReadWriter
manager *gateway.Manager
b broker.Broker
logLevel = logLevels[*logLevel]
)

manager = gateway.NewManager(&gateway.ManagerOptions{
ShardOptions: &gateway.ShardOptions{
Identify: &types.Identify{
Token: viper.GetString("token"),
},
// TODO: support more broker types
b = broker.NewAMQP(conf.Broker.Groups.Gateway, "", nil)
tryConnect(b, conf.Broker.URL)

manager = gateway.NewManager(&gateway.ManagerOptions{
ShardOptions: &gateway.ShardOptions{
Identify: &types.Identify{
Token: conf.Discord.Token,
},
OnPacket: onPacket,
Output: output,
REST: rest.NewClient(viper.GetString("token")),
LogLevel: logLevel,
ShardCount: viper.GetInt("shards"),
})
manager.ConnectBroker(b)
},
OnPacket: onPacket,
Output: output,
REST: rest.NewClient(conf.Discord.Token),
LogLevel: logLevel,
ShardCount: conf.Discord.Shards.Count,
})
manager.ConnectBroker(b)

if err := manager.Start(); err != nil {
logger.Fatalf("failed to connect to discord: %v", err)
}
},
if err := manager.Start(); err != nil {
logger.Fatalf("failed to connect to discord: %v", err)
}
}

// tryConnect exponentially increases the retry interval, stopping at 80 seconds
Expand All @@ -74,41 +80,3 @@ func tryConnect(b broker.Broker, url string) {
}
}
}

func initConfig() {
if viper.GetString("config") == "" {
viper.AddConfigPath(".")
viper.SetConfigName(".gateway")
} else {
viper.SetConfigFile(viper.GetString("config"))
}

viper.SetEnvPrefix("discord")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
logger.Println("Cannot read config:", err)
}
}

func init() {
cobra.OnInitialize(initConfig)
fg := rootCmd.PersistentFlags()
fg.StringP("config", "c", "", "location of config file")
fg.StringP("group", "g", "", "broker group to send Discord events to")
fg.StringP("token", "t", "", "Discord token used to connect to gateway")
fg.IntP("shards", "s", 0, "number of shards to spawn")
fg.StringP("loglevel", "l", "", "log level")
rootCmd.MarkFlagRequired("token")
viper.BindPFlag("group", fg.Lookup("group"))
viper.BindPFlag("token", fg.Lookup("token"))
viper.BindPFlag("shards", fg.Lookup("shards"))
viper.BindPFlag("loglevel", fg.Lookup("loglevel"))
}

// Execute the CLI application
func Execute() {
if err := rootCmd.Execute(); err != nil {
logger.Fatalln(err)
}
}
43 changes: 3 additions & 40 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,11 @@ module github.com/spec-tacles/gateway
go 1.12

require (
cloud.google.com/go v0.40.0 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190620071333-e64a0ec8b42a // indirect
github.com/golang/mock v1.3.1 // indirect
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/websocket v1.4.0
github.com/grpc-ecosystem/grpc-gateway v1.9.2 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.5 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/common v0.6.0 // indirect
github.com/rogpeppe/fastuuid v1.1.0 // indirect
github.com/russross/blackfriday v2.0.0+incompatible // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/spec-tacles/go v0.0.0-20190621005507-5c26f0d8038d
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0
github.com/spec-tacles/go v0.0.0-20190622170421-beb19c9cb227
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/ugorji/go v1.1.5-pre // indirect
github.com/valyala/gozstd v1.5.0
go.etcd.io/bbolt v1.3.3 // indirect
go.opencensus.io v0.22.0 // indirect
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 // indirect
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
golang.org/x/image v0.0.0-20190618124811-92942e4437e2 // indirect
golang.org/x/mobile v0.0.0-20190607214518-6fa95d984e88 // indirect
golang.org/x/mod v0.1.0 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45 // indirect
golang.org/x/tools v0.0.0-20190620191750-1fa568393b23 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/genproto v0.0.0-20190620144150-6af8c5fc6601 // indirect
google.golang.org/grpc v1.21.1 // indirect
honnef.co/go/tools v0.0.0-20190614002413-cb51c254f01b // indirect
)
Loading

0 comments on commit eea813f

Please sign in to comment.