From bf2b017767ba4bc9b4ee1a5bc5c284dda4ea27b9 Mon Sep 17 00:00:00 2001
From: Guillem Jover <gjover@sipwise.com>
Date: Fri, 17 Aug 2018 17:38:05 +0200
Subject: [PATCH] influx: Make the client default to the Unix socket if it is
 present

---
 client/influxdb.go    |  3 +++
 cmd/influx/cli/cli.go | 13 +++++++++++++
 cmd/influx/main.go    |  5 ++---
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/client/influxdb.go b/client/influxdb.go
index e28ac70fe..467eae4b2 100644
--- a/client/influxdb.go
+++ b/client/influxdb.go
@@ -25,6 +25,9 @@ const (
 	// DefaultPort is the default port used to connect to an InfluxDB instance
 	DefaultPort = 8086
 
+	// DefaultUnixSocket is the default unix socket used to connect to an InfluxDB instance
+	DefaultUnixSocket = "/run/influxdb/influxdb.sock"
+
 	// DefaultTimeout is the default connection timeout used to connect to an InfluxDB instance
 	DefaultTimeout = 0
 )
diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go
index 5705067c8..1a1f084b6 100644
--- a/cmd/influx/cli/cli.go
+++ b/cmd/influx/cli/cli.go
@@ -96,6 +96,19 @@ func (c *CommandLine) Run() error {
 		return errors.New("Unable to prompt for a password with no TTY.")
 	}
 
+	if c.UnixSocket == "" && c.Host == "" && c.Port == 0 {
+		fi, err := os.Stat(client.DefaultUnixSocket)
+		if (err == nil && fi.Mode() & os.ModeSocket != 0) {
+			c.UnixSocket = client.DefaultUnixSocket
+		}
+	}
+	if c.Host == "" {
+		c.Host = client.DefaultHost
+	}
+	if c.Port == 0 {
+		c.Port = client.DefaultPort
+	}
+
 	// Read environment variables for username/password.
 	if c.Username == "" {
 		c.Username = os.Getenv("INFLUX_USERNAME")
diff --git a/cmd/influx/main.go b/cmd/influx/main.go
index 2d5b0e0d7..7e9c6f308 100644
--- a/cmd/influx/main.go
+++ b/cmd/influx/main.go
@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"os"
 
-	"github.com/influxdata/influxdb/client"
 	"github.com/influxdata/influxdb/cmd/influx/cli"
 )
 
@@ -37,8 +36,8 @@ func main() {
 	c := cli.New(version)
 
 	fs := flag.NewFlagSet("InfluxDB shell version "+version, flag.ExitOnError)
-	fs.StringVar(&c.Host, "host", client.DefaultHost, "Influxdb host to connect to.")
-	fs.IntVar(&c.Port, "port", client.DefaultPort, "Influxdb port to connect to.")
+	fs.StringVar(&c.Host, "host", c.Host, "Influxdb host to connect to.")
+	fs.IntVar(&c.Port, "port", c.Port, "Influxdb port to connect to.")
 	fs.StringVar(&c.UnixSocket, "socket", c.UnixSocket, "Influxdb unix socket to connect to.")
 	fs.StringVar(&c.Username, "username", c.Username, "Username to connect to the server.")
 	fs.StringVar(&c.Password, "password", c.Password, `Password to connect to the server.  Leaving blank will prompt for password (--password="").`)
-- 
2.18.0

