2014-09-04 16:40:44 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-03-24 23:28:32 +09:00
|
|
|
"time"
|
2014-09-04 16:40:44 +09:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
2020-12-30 21:22:36 +09:00
|
|
|
"github.com/urfave/cli/v2"
|
2014-09-04 16:40:44 +09:00
|
|
|
)
|
|
|
|
|
2016-07-26 00:25:33 +09:00
|
|
|
func subscribe(c *cli.Context) error {
|
2016-01-18 21:53:38 +09:00
|
|
|
setDebugLevel(c)
|
2014-11-07 12:57:06 +09:00
|
|
|
opts, err := NewOption(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-02-09 18:27:17 -07:00
|
|
|
opts.SetKeepAlive(time.Second * 60)
|
2016-02-04 15:48:42 -07:00
|
|
|
|
2014-09-04 16:40:44 +09:00
|
|
|
if c.Bool("c") {
|
2014-12-15 23:33:31 +09:00
|
|
|
clientId := c.String("i")
|
|
|
|
if clientId == "" {
|
|
|
|
log.Warn("clean Flag does not work without client id")
|
|
|
|
}
|
|
|
|
|
2014-09-04 16:40:44 +09:00
|
|
|
opts.SetCleanSession(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
qos := c.Int("q")
|
|
|
|
topic := c.String("t")
|
|
|
|
if topic == "" {
|
|
|
|
log.Errorf("Please specify topic")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
log.Infof("Topic: %s", topic)
|
|
|
|
|
2015-03-24 23:28:32 +09:00
|
|
|
subscribed := map[string]byte{
|
|
|
|
topic: byte(qos),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = connect(c, opts, subscribed)
|
2014-09-04 16:40:44 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2015-03-24 23:28:32 +09:00
|
|
|
os.Exit(1)
|
2014-09-04 16:40:44 +09:00
|
|
|
}
|
2015-03-24 23:28:32 +09:00
|
|
|
|
|
|
|
// loops forever
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
2016-07-26 00:25:33 +09:00
|
|
|
return nil
|
2014-09-04 16:40:44 +09:00
|
|
|
}
|