...

Source file src/cloud.google.com/go/logging/internal/common.go

Documentation: cloud.google.com/go/logging/internal

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package internal
    16  
    17  import (
    18  	"fmt"
    19  	"runtime"
    20  	"strings"
    21  	"sync"
    22  	"unicode"
    23  )
    24  
    25  const (
    26  	// ProdAddr is the production address.
    27  	ProdAddr = "logging.googleapis.com:443"
    28  )
    29  
    30  // InstrumentOnce guards instrumenting logs one time
    31  var InstrumentOnce = new(sync.Once)
    32  
    33  // LogPath creates a formatted path from a parent and a logID.
    34  func LogPath(parent, logID string) string {
    35  	logID = strings.Replace(logID, "/", "%2F", -1)
    36  	return fmt.Sprintf("%s/logs/%s", parent, logID)
    37  }
    38  
    39  // LogIDFromPath parses and returns the ID from a log path.
    40  func LogIDFromPath(parent, path string) string {
    41  	start := len(parent) + len("/logs/")
    42  	if len(path) < start {
    43  		return ""
    44  	}
    45  	logID := path[start:]
    46  	return strings.Replace(logID, "%2F", "/", -1)
    47  }
    48  
    49  // VersionGo returns the Go runtime version. The returned string
    50  // has no whitespace, suitable for reporting in header.
    51  func VersionGo() string {
    52  	const develPrefix = "devel +"
    53  
    54  	s := runtime.Version()
    55  	if strings.HasPrefix(s, develPrefix) {
    56  		s = s[len(develPrefix):]
    57  		if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 {
    58  			s = s[:p]
    59  		}
    60  		return s
    61  	}
    62  
    63  	notSemverRune := func(r rune) bool {
    64  		return !strings.ContainsRune("0123456789.", r)
    65  	}
    66  
    67  	if strings.HasPrefix(s, "go1") {
    68  		s = s[2:]
    69  		var prerelease string
    70  		if p := strings.IndexFunc(s, notSemverRune); p >= 0 {
    71  			s, prerelease = s[:p], s[p:]
    72  		}
    73  		if strings.HasSuffix(s, ".") {
    74  			s += "0"
    75  		} else if strings.Count(s, ".") < 2 {
    76  			s += ".0"
    77  		}
    78  		if prerelease != "" {
    79  			s += "-" + prerelease
    80  		}
    81  		return s
    82  	}
    83  	return "UNKNOWN"
    84  }
    85  

View as plain text