aboutsummaryrefslogtreecommitdiff
path: root/internal/config
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-09-06 12:04:47 -0400
committerJonathan Amsterdam <jba@google.com>2021-09-07 15:14:39 +0000
commit7624e4766ea785e034eef99e45d5df11778722d0 (patch)
tree0c090aa37e709743ba829e6bfdbd70c84ec6c37f /internal/config
parenta7412093f9cf2b3f8887269f53f0d6ece5e3ba2a (diff)
downloadgo-x-pkgsite-7624e4766ea785e034eef99e45d5df11778722d0.tar.xz
many: replace most uses of strings.SplitN with Cut
In most cases Cut(s, sep) is clearer than strings.SplitN(s, sep, 2). Change-Id: I375c15f778e43ff25b5b74c9ad0e96dac4615786 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/347889 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/config')
-rw-r--r--internal/config/config.go17
-rw-r--r--internal/config/dynconfig/dynconfig.go6
2 files changed, 11 insertions, 12 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 7b1e9349..3f243db7 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -27,6 +27,7 @@ import (
"cloud.google.com/go/storage"
"github.com/ghodss/yaml"
"golang.org/x/net/context/ctxhttp"
+ "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/secrets"
@@ -277,14 +278,14 @@ func (c *Config) DeploymentEnvironment() string {
if c.ServiceID == "" {
return "local"
}
- parts := strings.SplitN(c.ServiceID, "-", 2)
- if len(parts) == 1 {
+ before, _, found := internal.Cut(c.ServiceID, "-")
+ if !found {
return "prod"
}
- if parts[0] == "" {
+ if before == "" {
return "unknownEnv"
}
- return parts[0]
+ return before
}
// Application returns the name of the running application: "worker",
@@ -293,12 +294,12 @@ func (c *Config) Application() string {
if c.ServiceID == "" {
return "unknownApp"
}
- parts := strings.SplitN(c.ServiceID, "-", 2)
+ before, after, found := internal.Cut(c.ServiceID, "-")
var svc string
- if len(parts) == 1 {
- svc = parts[0]
+ if !found {
+ svc = before
} else {
- svc = parts[1]
+ svc = after
}
switch svc {
case "default":
diff --git a/internal/config/dynconfig/dynconfig.go b/internal/config/dynconfig/dynconfig.go
index 9c2e2342..29e65d17 100644
--- a/internal/config/dynconfig/dynconfig.go
+++ b/internal/config/dynconfig/dynconfig.go
@@ -40,12 +40,10 @@ func Read(ctx context.Context, location string) (_ *DynamicConfig, err error) {
log.Debugf(ctx, "reading dynamic config from %s", location)
var r io.ReadCloser
if strings.HasPrefix(location, "gs://") {
- parts := strings.SplitN(location[5:], "/", 2)
- if len(parts) != 2 {
+ bucket, object, found := internal.Cut(location[5:], "/")
+ if !found {
return nil, errors.New("bad GCS URL")
}
- bucket := parts[0]
- object := parts[1]
if err != nil {
return nil, err
}