mirror of
https://github.com/shirou/mqttcli.git
synced 2025-04-26 13:49:17 +08:00
fix default mqttcli.cfg filename problem
This commit is contained in:
parent
01b1035efb
commit
800a9bb338
23
config.go
23
config.go
@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
||||
log "github.com/Sirupsen/logrus"
|
||||
@ -16,7 +17,7 @@ const DefaultConfigFile = ".mqttcli.cfg" // Under HOME
|
||||
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
Port int `json:"port"`
|
||||
UserName string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
@ -48,11 +49,11 @@ func UserHomeDir() string {
|
||||
}
|
||||
func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error {
|
||||
confPath := ""
|
||||
|
||||
if p == "~/.mqtt.cfg" || p == "" {
|
||||
home := UserHomeDir()
|
||||
home := UserHomeDir()
|
||||
// replace home to ~ in order to match
|
||||
p = strings.Replace(p, home, "~", 1)
|
||||
if p == "~/.mqttcli.cfg" || p == "" {
|
||||
confPath = path.Join(home, DefaultConfigFile)
|
||||
|
||||
_, err := os.Stat(confPath)
|
||||
if os.IsNotExist(err) {
|
||||
return err
|
||||
@ -63,14 +64,18 @@ func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error {
|
||||
|
||||
ret, err := readFromConfigFile(confPath)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
if ret.Host != "" {
|
||||
if ret.Port == "" {
|
||||
ret.Port = "1883"
|
||||
if ret.Port == 0 {
|
||||
ret.Port = 1883
|
||||
}
|
||||
scheme := "tcp" // FIXME:
|
||||
brokerUri := fmt.Sprintf("%s://%s:%s", scheme, ret.Host, ret.Port)
|
||||
scheme := "tcp"
|
||||
if ret.Port == 8883 {
|
||||
scheme = "ssl"
|
||||
}
|
||||
brokerUri := fmt.Sprintf("%s://%s:%d", scheme, ret.Host, ret.Port)
|
||||
log.Infof("Broker URI: %s", brokerUri)
|
||||
opts.AddBroker(brokerUri)
|
||||
}
|
||||
|
6
main.go
6
main.go
@ -45,7 +45,11 @@ func pubsub(c *cli.Context) {
|
||||
if c.Bool("d") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
opts := NewOption(c)
|
||||
opts, err := NewOption(c)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
client, err := connect(c, opts)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
|
25
mqtt.go
25
mqtt.go
@ -3,7 +3,9 @@ package main
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
||||
@ -61,6 +63,17 @@ func (m *MQTTClient) Subscribe(topic string, qos int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCertPool(pemPath string) (*x509.CertPool, error) {
|
||||
certs := x509.NewCertPool()
|
||||
|
||||
pemData, err := ioutil.ReadFile(pemPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
certs.AppendCertsFromPEM(pemData)
|
||||
return certs, nil
|
||||
}
|
||||
|
||||
// getRandomClientId returns randomized ClientId.
|
||||
func getRandomClientId() string {
|
||||
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
@ -73,7 +86,7 @@ func getRandomClientId() string {
|
||||
}
|
||||
|
||||
// NewOption returns ClientOptions via parsing command line options.
|
||||
func NewOption(c *cli.Context) *MQTT.ClientOptions {
|
||||
func NewOption(c *cli.Context) (*MQTT.ClientOptions, error) {
|
||||
opts := MQTT.NewClientOptions()
|
||||
|
||||
host := c.String("host")
|
||||
@ -89,14 +102,20 @@ func NewOption(c *cli.Context) *MQTT.ClientOptions {
|
||||
}
|
||||
opts.SetClientId(clientId)
|
||||
|
||||
tlsConfig := &tls.Config{InsecureSkipVerify: false}
|
||||
cafile := c.String("cafile")
|
||||
scheme := "tcp"
|
||||
if cafile != "" {
|
||||
scheme = "ssl"
|
||||
certPool, err := getCertPool(cafile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsConfig.RootCAs = certPool
|
||||
}
|
||||
insecure := true
|
||||
if insecure {
|
||||
tlsConfig := &tls.Config{InsecureSkipVerify: true}
|
||||
tlsConfig.InsecureSkipVerify = true
|
||||
opts.SetTlsConfig(tlsConfig)
|
||||
}
|
||||
|
||||
@ -115,5 +134,5 @@ func NewOption(c *cli.Context) *MQTT.ClientOptions {
|
||||
|
||||
opts.AddBroker(brokerUri)
|
||||
}
|
||||
return opts
|
||||
return opts, nil
|
||||
}
|
||||
|
@ -13,7 +13,11 @@ func publish(c *cli.Context) {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
|
||||
opts := NewOption(c)
|
||||
opts, err := NewOption(c)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
client, err := connect(c, opts)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
|
@ -11,7 +11,11 @@ func subscribe(c *cli.Context) {
|
||||
if c.Bool("d") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
opts := NewOption(c)
|
||||
opts, err := NewOption(c)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if c.Bool("c") {
|
||||
opts.SetCleanSession(false)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user