2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-06 03:20:21 +00:00
lntop/cli/cli.go

102 lines
1.8 KiB
Go
Raw Normal View History

2019-03-15 15:08:57 +00:00
package cli
import (
2019-03-15 16:50:16 +00:00
"context"
2019-04-13 16:45:57 +00:00
"os"
"os/signal"
2019-03-15 16:50:16 +00:00
2019-03-15 15:08:57 +00:00
cli "gopkg.in/urfave/cli.v2"
2019-03-15 16:50:16 +00:00
2019-03-19 08:54:45 +00:00
"github.com/edouardparis/lntop/app"
2019-03-15 16:50:16 +00:00
"github.com/edouardparis/lntop/config"
2019-03-26 16:48:58 +00:00
"github.com/edouardparis/lntop/events"
2019-03-15 16:50:16 +00:00
"github.com/edouardparis/lntop/logging"
2019-03-26 16:48:58 +00:00
"github.com/edouardparis/lntop/pubsub"
2019-03-18 12:38:14 +00:00
"github.com/edouardparis/lntop/ui"
2019-03-15 15:08:57 +00:00
)
const version = "v0.1.0"
2019-04-16 11:40:56 +00:00
2019-03-15 15:08:57 +00:00
// New creates a new cli app.
func New() *cli.App {
cli.VersionFlag = &cli.BoolFlag{
Name: "version", Aliases: []string{},
Usage: "print the version",
}
return &cli.App{
Name: "lntop",
2019-04-16 11:40:56 +00:00
Version: version,
2019-03-15 15:08:57 +00:00
EnableShellCompletion: true,
2019-03-18 12:25:27 +00:00
Action: run,
2019-03-15 15:08:57 +00:00
Flags: []cli.Flag{
2019-03-15 16:50:16 +00:00
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
2019-04-13 10:05:59 +00:00
Usage: "path to config file",
2019-03-15 16:50:16 +00:00
},
},
Commands: []*cli.Command{
2019-03-26 16:48:58 +00:00
{
Name: "pubsub",
Aliases: []string{""},
2019-04-13 10:05:59 +00:00
Usage: "run the pubsub only",
2019-03-26 16:48:58 +00:00
Action: pubsubRun,
},
2019-03-15 15:08:57 +00:00
},
}
}
2019-03-15 16:50:16 +00:00
2019-03-18 12:25:27 +00:00
func run(c *cli.Context) error {
2019-03-26 13:02:59 +00:00
cfg, err := config.Load(c.String("config"))
2019-03-19 08:54:45 +00:00
if err != nil {
return err
}
2019-03-26 13:02:59 +00:00
app, err := app.New(cfg)
if err != nil {
return err
}
2019-03-19 13:19:49 +00:00
2019-04-13 16:45:57 +00:00
ctx := context.Background()
2019-03-29 16:30:48 +00:00
events := make(chan *events.Event)
2019-04-13 16:45:57 +00:00
ps := pubsub.New(app.Logger, app.Network)
2019-03-29 16:30:48 +00:00
go func() {
2019-04-13 16:45:57 +00:00
err := ui.Run(ctx, app, events)
2019-03-29 16:30:48 +00:00
if err != nil {
app.Logger.Debug("ui", logging.String("error", err.Error()))
}
2019-04-13 16:45:57 +00:00
ps.Stop()
2019-03-29 16:30:48 +00:00
}()
2019-04-13 16:45:57 +00:00
ps.Run(ctx, events)
2019-03-29 16:30:48 +00:00
return nil
2019-03-18 12:25:27 +00:00
}
2019-03-26 16:48:58 +00:00
func pubsubRun(c *cli.Context) error {
cfg, err := config.Load(c.String("config"))
if err != nil {
return err
}
app, err := app.New(cfg)
if err != nil {
return err
}
events := make(chan *events.Event)
2019-04-13 16:45:57 +00:00
ps := pubsub.New(app.Logger, app.Network)
ps.Run(context.Background(), events)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func() {
<-sig
ps.Stop()
}()
2019-03-29 16:02:52 +00:00
return nil
2019-03-26 16:48:58 +00:00
}