pkg/util/{ip.go,url.go}: Fix some golint issues

See,
$ gometalinter --vendor --deadline 10m --disable-all --enable=golint  ./...
ip.go:8:6⚠️ func SplitIpPort should be SplitIPPort (golint)
url.go:14:6⚠️ func NewUrlQueryReader should be NewURLQueryReader (golint)
url.go:9:6⚠️ type UrlQueryReader should be URLQueryReader (golint)
url.go:37:6⚠️ func JoinUrlFragments should be JoinURLFragments (golint)
This commit is contained in:
Mario Trangoni 2019-01-28 22:18:48 +01:00
parent 5f6383a750
commit 8261613b51
13 changed files with 37 additions and 37 deletions

View File

@ -35,7 +35,7 @@ func (hs *HTTPServer) initAppPluginRoutes(r *macaron.Macaron) {
for _, plugin := range plugins.Apps { for _, plugin := range plugins.Apps {
for _, route := range plugin.Routes { for _, route := range plugin.Routes {
url := util.JoinUrlFragments("/api/plugin-proxy/"+plugin.Id, route.Path) url := util.JoinURLFragments("/api/plugin-proxy/"+plugin.Id, route.Path)
handlers := make([]macaron.Handler, 0) handlers := make([]macaron.Handler, 0)
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{
ReqSignedIn: true, ReqSignedIn: true,

View File

@ -30,7 +30,7 @@ func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
req.URL.Host = url.Host req.URL.Host = url.Host
req.Host = url.Host req.Host = url.Host
req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath) req.URL.Path = util.JoinURLFragments(url.Path+"/api", proxyPath)
// clear cookie headers // clear cookie headers
req.Header.Del("Cookie") req.Header.Del("Cookie")

View File

@ -39,7 +39,7 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route
req.URL.Scheme = routeURL.Scheme req.URL.Scheme = routeURL.Scheme
req.URL.Host = routeURL.Host req.URL.Host = routeURL.Host
req.Host = routeURL.Host req.Host = routeURL.Host
req.URL.Path = util.JoinUrlFragments(routeURL.Path, proxyPath) req.URL.Path = util.JoinURLFragments(routeURL.Path, proxyPath)
if err := addHeaders(&req.Header, route, data); err != nil { if err := addHeaders(&req.Header, route, data); err != nil {
logger.Error("Failed to render plugin headers", "error", err) logger.Error("Failed to render plugin headers", "error", err)

View File

@ -139,19 +139,19 @@ func (proxy *DataSourceProxy) getDirector() func(req *http.Request) {
reqQueryVals := req.URL.Query() reqQueryVals := req.URL.Query()
if proxy.ds.Type == m.DS_INFLUXDB_08 { if proxy.ds.Type == m.DS_INFLUXDB_08 {
req.URL.Path = util.JoinUrlFragments(proxy.targetUrl.Path, "db/"+proxy.ds.Database+"/"+proxy.proxyPath) req.URL.Path = util.JoinURLFragments(proxy.targetUrl.Path, "db/"+proxy.ds.Database+"/"+proxy.proxyPath)
reqQueryVals.Add("u", proxy.ds.User) reqQueryVals.Add("u", proxy.ds.User)
reqQueryVals.Add("p", proxy.ds.Password) reqQueryVals.Add("p", proxy.ds.Password)
req.URL.RawQuery = reqQueryVals.Encode() req.URL.RawQuery = reqQueryVals.Encode()
} else if proxy.ds.Type == m.DS_INFLUXDB { } else if proxy.ds.Type == m.DS_INFLUXDB {
req.URL.Path = util.JoinUrlFragments(proxy.targetUrl.Path, proxy.proxyPath) req.URL.Path = util.JoinURLFragments(proxy.targetUrl.Path, proxy.proxyPath)
req.URL.RawQuery = reqQueryVals.Encode() req.URL.RawQuery = reqQueryVals.Encode()
if !proxy.ds.BasicAuth { if !proxy.ds.BasicAuth {
req.Header.Del("Authorization") req.Header.Del("Authorization")
req.Header.Add("Authorization", util.GetBasicAuthHeader(proxy.ds.User, proxy.ds.Password)) req.Header.Add("Authorization", util.GetBasicAuthHeader(proxy.ds.User, proxy.ds.Password))
} }
} else { } else {
req.URL.Path = util.JoinUrlFragments(proxy.targetUrl.Path, proxy.proxyPath) req.URL.Path = util.JoinURLFragments(proxy.targetUrl.Path, proxy.proxyPath)
} }
if proxy.ds.BasicAuth { if proxy.ds.BasicAuth {
req.Header.Del("Authorization") req.Header.Del("Authorization")

View File

@ -46,7 +46,7 @@ func NewApiPluginProxy(ctx *m.ReqContext, proxyPath string, route *plugins.AppPl
req.URL.Host = targetURL.Host req.URL.Host = targetURL.Host
req.Host = targetURL.Host req.Host = targetURL.Host
req.URL.Path = util.JoinUrlFragments(targetURL.Path, proxyPath) req.URL.Path = util.JoinURLFragments(targetURL.Path, proxyPath)
// clear cookie headers // clear cookie headers
req.Header.Del("Cookie") req.Header.Del("Cookie")

View File

@ -14,7 +14,7 @@ import (
) )
func (hs *HTTPServer) RenderToPng(c *m.ReqContext) { func (hs *HTTPServer) RenderToPng(c *m.ReqContext) {
queryReader, err := util.NewUrlQueryReader(c.Req.URL) queryReader, err := util.NewURLQueryReader(c.Req.URL)
if err != nil { if err != nil {
c.Handle(400, "Render parameters error", err) c.Handle(400, "Render parameters error", err)
return return

View File

@ -45,9 +45,9 @@ func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) {
fp.BaseUrl = app.BaseUrl fp.BaseUrl = app.BaseUrl
if isExternalPlugin(app.PluginDir) { if isExternalPlugin(app.PluginDir) {
fp.Module = util.JoinUrlFragments("plugins/"+app.Id, appSubPath) + "/module" fp.Module = util.JoinURLFragments("plugins/"+app.Id, appSubPath) + "/module"
} else { } else {
fp.Module = util.JoinUrlFragments("app/plugins/app/"+app.Id, appSubPath) + "/module" fp.Module = util.JoinURLFragments("app/plugins/app/"+app.Id, appSubPath) + "/module"
} }
} }

View File

@ -223,7 +223,7 @@ func (ss *SqlStore) buildConnectionString() (string, error) {
cnnstr += "&tls=custom" cnnstr += "&tls=custom"
} }
case migrator.POSTGRES: case migrator.POSTGRES:
host, port, err := util.SplitIpPort(ss.dbCfg.Host, "5432") host, port, err := util.SplitIPPort(ss.dbCfg.Host, "5432")
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -49,7 +49,7 @@ func generateConnectionString(datasource *models.DataSource) (string, error) {
} }
} }
server, port, err := util.SplitIpPort(datasource.Url, "1433") server, port, err := util.SplitIPPort(datasource.Url, "1433")
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -4,8 +4,8 @@ import (
"net" "net"
) )
// SplitIpPort splits the ip string and port. // SplitIPPort splits the ip string and port.
func SplitIpPort(ipStr string, portDefault string) (ip string, port string, err error) { func SplitIPPort(ipStr string, portDefault string) (ip string, port string, err error) {
ipAddr := net.ParseIP(ipStr) ipAddr := net.ParseIP(ipStr)
if ipAddr == nil { if ipAddr == nil {

View File

@ -6,10 +6,10 @@ import (
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
func TestSplitIpPort(t *testing.T) { func TestSplitIPPort(t *testing.T) {
Convey("When parsing an IPv4 without explicit port", t, func() { Convey("When parsing an IPv4 without explicit port", t, func() {
ip, port, err := SplitIpPort("1.2.3.4", "5678") ip, port, err := SplitIPPort("1.2.3.4", "5678")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
So(ip, ShouldEqual, "1.2.3.4") So(ip, ShouldEqual, "1.2.3.4")
@ -17,7 +17,7 @@ func TestSplitIpPort(t *testing.T) {
}) })
Convey("When parsing an IPv6 without explicit port", t, func() { Convey("When parsing an IPv6 without explicit port", t, func() {
ip, port, err := SplitIpPort("::1", "5678") ip, port, err := SplitIPPort("::1", "5678")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
So(ip, ShouldEqual, "::1") So(ip, ShouldEqual, "::1")
@ -25,7 +25,7 @@ func TestSplitIpPort(t *testing.T) {
}) })
Convey("When parsing an IPv4 with explicit port", t, func() { Convey("When parsing an IPv4 with explicit port", t, func() {
ip, port, err := SplitIpPort("1.2.3.4:56", "78") ip, port, err := SplitIPPort("1.2.3.4:56", "78")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
So(ip, ShouldEqual, "1.2.3.4") So(ip, ShouldEqual, "1.2.3.4")
@ -33,7 +33,7 @@ func TestSplitIpPort(t *testing.T) {
}) })
Convey("When parsing an IPv6 with explicit port", t, func() { Convey("When parsing an IPv6 with explicit port", t, func() {
ip, port, err := SplitIpPort("[::1]:56", "78") ip, port, err := SplitIPPort("[::1]:56", "78")
So(err, ShouldEqual, nil) So(err, ShouldEqual, nil)
So(ip, ShouldEqual, "::1") So(ip, ShouldEqual, "::1")

View File

@ -5,26 +5,26 @@ import (
"strings" "strings"
) )
// UrlQueryReader is a URL query type. // URLQueryReader is a URL query type.
type UrlQueryReader struct { type URLQueryReader struct {
values url.Values values url.Values
} }
// NewUrlQueryReader parses a raw query and returns it as a UrlQueryReader type. // NewURLQueryReader parses a raw query and returns it as a URLQueryReader type.
func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) { func NewURLQueryReader(urlInfo *url.URL) (*URLQueryReader, error) {
u, err := url.ParseQuery(urlInfo.RawQuery) u, err := url.ParseQuery(urlInfo.RawQuery)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &UrlQueryReader{ return &URLQueryReader{
values: u, values: u,
}, nil }, nil
} }
// Get parse parameters from an URL. If the parameter does not exist, it returns // Get parse parameters from an URL. If the parameter does not exist, it returns
// the default value. // the default value.
func (r *UrlQueryReader) Get(name string, def string) string { func (r *URLQueryReader) Get(name string, def string) string {
val := r.values[name] val := r.values[name]
if len(val) == 0 { if len(val) == 0 {
return def return def
@ -33,8 +33,8 @@ func (r *UrlQueryReader) Get(name string, def string) string {
return val[0] return val[0]
} }
// JoinUrlFragments joins two URL fragments into only one URL string. // JoinURLFragments joins two URL fragments into only one URL string.
func JoinUrlFragments(a, b string) string { func JoinURLFragments(a, b string) string {
aslash := strings.HasSuffix(a, "/") aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/") bslash := strings.HasPrefix(b, "/")

View File

@ -1,60 +1,60 @@
package util package util
import ( import (
"net/url"
"testing" "testing"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
"net/url"
) )
func TestUrl(t *testing.T) { func TestUrl(t *testing.T) {
Convey("When joining two urls where right hand side is empty", t, func() { Convey("When joining two urls where right hand side is empty", t, func() {
result := JoinUrlFragments("http://localhost:8080", "") result := JoinURLFragments("http://localhost:8080", "")
So(result, ShouldEqual, "http://localhost:8080") So(result, ShouldEqual, "http://localhost:8080")
}) })
Convey("When joining two urls where right hand side is empty and lefthand side has a trailing slash", t, func() { Convey("When joining two urls where right hand side is empty and lefthand side has a trailing slash", t, func() {
result := JoinUrlFragments("http://localhost:8080/", "") result := JoinURLFragments("http://localhost:8080/", "")
So(result, ShouldEqual, "http://localhost:8080/") So(result, ShouldEqual, "http://localhost:8080/")
}) })
Convey("When joining two urls where neither has a trailing slash", t, func() { Convey("When joining two urls where neither has a trailing slash", t, func() {
result := JoinUrlFragments("http://localhost:8080", "api") result := JoinURLFragments("http://localhost:8080", "api")
So(result, ShouldEqual, "http://localhost:8080/api") So(result, ShouldEqual, "http://localhost:8080/api")
}) })
Convey("When joining two urls where lefthand side has a trailing slash", t, func() { Convey("When joining two urls where lefthand side has a trailing slash", t, func() {
result := JoinUrlFragments("http://localhost:8080/", "api") result := JoinURLFragments("http://localhost:8080/", "api")
So(result, ShouldEqual, "http://localhost:8080/api") So(result, ShouldEqual, "http://localhost:8080/api")
}) })
Convey("When joining two urls where righthand side has preceding slash", t, func() { Convey("When joining two urls where righthand side has preceding slash", t, func() {
result := JoinUrlFragments("http://localhost:8080", "/api") result := JoinURLFragments("http://localhost:8080", "/api")
So(result, ShouldEqual, "http://localhost:8080/api") So(result, ShouldEqual, "http://localhost:8080/api")
}) })
Convey("When joining two urls where righthand side has trailing slash", t, func() { Convey("When joining two urls where righthand side has trailing slash", t, func() {
result := JoinUrlFragments("http://localhost:8080", "api/") result := JoinURLFragments("http://localhost:8080", "api/")
So(result, ShouldEqual, "http://localhost:8080/api/") So(result, ShouldEqual, "http://localhost:8080/api/")
}) })
Convey("When joining two urls where lefthand side has a trailing slash and righthand side has preceding slash", t, func() { Convey("When joining two urls where lefthand side has a trailing slash and righthand side has preceding slash", t, func() {
result := JoinUrlFragments("http://localhost:8080/", "/api/") result := JoinURLFragments("http://localhost:8080/", "/api/")
So(result, ShouldEqual, "http://localhost:8080/api/") So(result, ShouldEqual, "http://localhost:8080/api/")
}) })
} }
func TestNewUrlQueryReader(t *testing.T) { func TestNewURLQueryReader(t *testing.T) {
u, _ := url.Parse("http://www.abc.com/foo?bar=baz&bar2=baz2") u, _ := url.Parse("http://www.abc.com/foo?bar=baz&bar2=baz2")
uqr, _ := NewUrlQueryReader(u) uqr, _ := NewURLQueryReader(u)
Convey("when trying to retrieve the first query value", t, func() { Convey("when trying to retrieve the first query value", t, func() {
result := uqr.Get("bar", "foodef") result := uqr.Get("bar", "foodef")