本文整理汇总了Golang中strings.TrimSuffix函数的典型用法代码示例。如果您正苦于以下问题:Golang TrimSuffix函数的具体用法?Golang TrimSuffix怎么用?Golang TrimSuffix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TrimSuffix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sendEvent
func (p *SlowLogParser) sendEvent(inHeader bool, inQuery bool) {
if p.opt.Debug {
l.Println("send event")
}
// Make a new event and reset our metadata.
defer func() {
p.event = log.NewEvent()
p.headerLines = 0
p.queryLines = 0
p.inHeader = inHeader
p.inQuery = inQuery
}()
if _, ok := p.event.TimeMetrics["Query_time"]; !ok {
if p.headerLines == 0 {
l.Panicf("No Query_time in event at %d: %#v", p.lineOffset, p.event)
}
// Started parsing in header after Query_time. Throw away event.
return
}
// Clean up the event.
p.event.Db = strings.TrimSuffix(p.event.Db, ";\n")
p.event.Query = strings.TrimSuffix(p.event.Query, ";")
// Send the event. This will block.
select {
case p.EventChan <- p.event:
case <-p.stopChan:
p.stopped = true
}
}
示例2: parse_split
/**
* parse split chars and return value
*/
func parse_split(split string) (id byte, value int, err error) {
split = strings.ToLower(split)
switch split {
case "day":
id, value = SPLIT_DAY, 1
default:
if strings.HasSuffix(split, "hour") {
id = SPLIT_HOUR
value, err = strconv.Atoi(strings.TrimSuffix(split, "hour"))
//fix illegal hour
if err == nil && (value < 1 || value > 23) {
value = 1
}
} else if strings.HasSuffix(split, "min") {
id = SPLIT_MIN
value, err = strconv.Atoi(strings.TrimSuffix(split, "min"))
//fix illegal minute
if err == nil && (value > 60 || value < 5) {
value = 5
}
} else if strings.HasSuffix(split, "m") {
id = SPLIT_SIZE
value, err = strconv.Atoi(strings.TrimSuffix(split, "m"))
//fix illegal size
if err == nil && value < 1 {
value = 1
}
} else {
err = errors.New("unknow split")
}
}
return
}
示例3: ExecuteRollback
// 回滚部署
func ExecuteRollback(username string, params martini.Params, r render.Render) {
deployId, _ := strconv.Atoi(params["id"])
var deploy Deploy
db.First(&deploy, deployId)
if deploy.Id <= 0 {
sendFailMsg(r, "该版本不存在.", nil)
return
}
id := deploy.SystemId
stage := deploy.Stage
if isDeploying(id) {
sendFailMsg(r, "另一部署进程正在进行中,请稍候.", nil)
return
}
var conf SystemConfig
db.First(&conf, id)
workdir := conf.Path
switch deploy.Stage {
case "dev":
workdir = strings.TrimSuffix(conf.Path, "/") + "/development"
case "prod":
workdir = strings.TrimSuffix(conf.Path, "/") + "/production"
default:
}
releaseDir := fmt.Sprintf("%s/releases", workdir)
versionDir := fmt.Sprintf("%s/%s", releaseDir, deploy.Version)
currentDir := fmt.Sprintf("%s/current", workdir)
tags := conf.Tags
cmds := NewShellCommand()
// 判断该版本目录是否存在,存在直接回滚
cmds.ExistDir(versionDir).Rollback(versionDir, currentDir)
// 取对应该tag的所有服务器
servers := getTagServers(tags)
session := NewShellSession(servers, *cmds, deployId)
mutex.Lock()
sessions[id] = session
mutex.Unlock()
session.ParallelRun()
if !session.Success {
sendFailMsg(r, "回滚失败.", session.Output())
return
}
// 去掉旧的部署的启用状态
db.Model(Deploy{}).Where(Deploy{SystemId: id, Stage: stage, Enable: true}).Update(map[string]interface{}{"enable": false})
deploy.Enable = true
db.Save(&deploy)
sendSuccessMsg(r, session.Output())
return
}
示例4: parseContent
// parseContent parse client Content container into printer struct.
func parseContent(c *client.Content) ContentMessage {
content := ContentMessage{}
content.Time = c.Time.Local()
// guess file type
content.Filetype = func() string {
if c.Type.IsDir() {
return "folder"
}
return "file"
}()
content.Size = c.Size
// Convert OS Type to match console file printing style.
content.Name = func() string {
switch {
case runtime.GOOS == "windows":
c.Name = strings.Replace(c.Name, "/", "\\", -1)
c.Name = strings.TrimSuffix(c.Name, "\\")
default:
c.Name = strings.TrimSuffix(c.Name, "/")
}
if c.Type.IsDir() {
switch {
case runtime.GOOS == "windows":
return fmt.Sprintf("%s\\", c.Name)
default:
return fmt.Sprintf("%s/", c.Name)
}
}
return c.Name
}()
return content
}
示例5: add
// structProcessor.add() takes in a metric name prefix, an arbitrary struct, and a tagset.
// The processor recurses through the struct and builds metrics. The field tags direct how
// the field should be processed, as well as the metadata for the resulting metric.
//
// The field tags used are described as follows:
//
// version: typically set to '1' or '2'.
// This is compared against the elastic cluster version. If the version from the tag
// does not match the version in production, the metric will not be sent for this field.
//
// exclude:
// If this tag is set to 'true', a metric will not be sent for this field.
//
// rate: one of 'gauge', 'counter', 'rate'
// This tag dictates the metadata.RateType we send.
//
// unit: 'bytes', 'pages', etc
// This tag dictates the metadata.Unit we send.
//
// metric:
// This is the metric name which will be sent. If not present, the 'json'
// tag is sent as the metric name.
//
// Special handling:
//
// Metrics having the json tag suffix of 'in_milliseconds' are automagically
// divided by 1000 and sent as seconds. The suffix is stripped from the name.
//
// Metrics having the json tag suffix of 'in_bytes' are automatically sent as
// gauge bytes. The suffix is stripped from the metric name.
func (s *structProcessor) add(prefix string, st interface{}, ts opentsdb.TagSet) {
t := reflect.TypeOf(st)
valueOf := reflect.ValueOf(st)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
value := valueOf.Field(i).Interface()
if field.Tag.Get("exclude") == "true" {
continue
}
var (
jsonTag = field.Tag.Get("json")
metricTag = field.Tag.Get("metric")
versionTag = field.Tag.Get("version")
rateTag = field.Tag.Get("rate")
unitTag = field.Tag.Get("unit")
)
metricName := jsonTag
if metricTag != "" {
metricName = metricTag
}
if metricName == "" {
slog.Errorf("Unable to determine metric name for field %s. Skipping.", field.Name)
continue
}
if versionTag == "" || strings.HasPrefix(s.elasticVersion, versionTag) {
switch value := value.(type) {
case int, float64: // Number types in our structs are only ints and float64s.
// Turn all millisecond metrics into seconds
if strings.HasSuffix(metricName, "_in_millis") {
switch value.(type) {
case int:
value = float64(value.(int)) / 1000
case float64:
value = value.(float64) / 1000
}
unitTag = "seconds"
metricName = strings.TrimSuffix(metricName, "_in_millis")
}
// Set rate and unit for all "_in_bytes" metrics, and strip the "_in_bytes"
if strings.HasSuffix(metricName, "_in_bytes") {
if rateTag == "" {
rateTag = "gauge"
}
unitTag = "bytes"
metricName = strings.TrimSuffix(metricName, "_in_bytes")
}
Add(s.md, prefix+"."+metricName, value, ts, metadata.RateType(rateTag), metadata.Unit(unitTag), field.Tag.Get("desc"))
case string:
// The json data has a lot of strings, and we don't care about em.
default:
// If we hit another struct, recurse
if reflect.ValueOf(value).Kind() == reflect.Struct {
s.add(prefix+"."+metricName, value, ts)
} else {
slog.Errorf("Field %s for metric %s is non-numeric type. Cannot record as a metric.\n", field.Name, prefix+"."+metricName)
}
}
}
}
}
示例6: simpleImporter
func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) {
pkg := imports[path]
if pkg == nil {
// Guess the package name without importing it. Start with the last
// element of the path.
name := path[strings.LastIndex(path, "/")+1:]
// Trim commonly used prefixes and suffixes containing illegal name
// runes.
name = strings.TrimSuffix(name, ".go")
name = strings.TrimSuffix(name, "-go")
name = strings.TrimPrefix(name, "go.")
name = strings.TrimPrefix(name, "go-")
name = strings.TrimPrefix(name, "biogo.")
// It's also common for the last element of the path to contain an
// extra "go" prefix, but not always. TODO: examine unresolved ids to
// detect when trimming the "go" prefix is appropriate.
pkg = ast.NewObj(ast.Pkg, name)
pkg.Data = ast.NewScope(nil)
imports[path] = pkg
}
return pkg, nil
}
示例7: TestInspectExecID
func TestInspectExecID(t *testing.T) {
defer deleteAllContainers()
out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "top"))
if exitCode != 0 || err != nil {
t.Fatalf("failed to run container: %s, %v", out, err)
}
id := strings.TrimSuffix(out, "\n")
out, err = inspectField(id, "ExecIDs")
if err != nil {
t.Fatalf("failed to inspect container: %s, %v", out, err)
}
if out != "<no value>" {
t.Fatalf("ExecIDs should be empty, got: %s", out)
}
exitCode, err = runCommand(exec.Command(dockerBinary, "exec", "-d", id, "ls", "/"))
if exitCode != 0 || err != nil {
t.Fatalf("failed to exec in container: %s, %v", out, err)
}
out, err = inspectField(id, "ExecIDs")
if err != nil {
t.Fatalf("failed to inspect container: %s, %v", out, err)
}
out = strings.TrimSuffix(out, "\n")
if out == "[]" || out == "<no value>" {
t.Fatalf("ExecIDs should not be empty, got: %s", out)
}
logDone("inspect - inspect a container with ExecIDs")
}
示例8: handleRedirects
func handleRedirects(w http.ResponseWriter, r *http.Request, user string, repo string, ref string, doc string) bool {
redirectTo := ""
if r.RequestURI == "/" {
redirectTo = "http://progrium.viewdocs.io/viewdocs/"
}
if strings.Contains(r.Host, "progrium") && strings.HasPrefix(r.RequestURI, "/dokku") {
redirectTo = "http://dokku.viewdocs.io" + r.RequestURI
}
if isAsset(doc) {
redirectTo = "https://cdn.rawgit.com/" + user + "/" + repo + "/" + ref + "/docs/" + doc
}
if !strings.HasSuffix(r.RequestURI, "/") {
for ext := range markdownExtensions() {
if strings.HasSuffix(r.RequestURI, ext) {
redirectTo = strings.TrimSuffix(r.RequestURI, ext) + "/"
break
}
}
if redirectTo == "" {
redirectTo = r.RequestURI + "/"
}
}
for ext := range markdownExtensions() {
if strings.HasSuffix(r.RequestURI, ext+"/") {
redirectTo = strings.TrimSuffix(r.RequestURI, ext+"/") + "/"
break
}
}
if redirectTo != "" {
log.Println("REDIRECT: ", redirectTo)
http.Redirect(w, r, redirectTo, 301)
return true
}
return false
}
示例9: hitNodePort
func (config *KubeProxyTestConfig) hitNodePort(epCount int) {
node1_IP := strings.TrimSuffix(config.nodes[0], ":22")
tries := epCount*epCount + 5 // + 10 if epCount == 0
By("dialing(udp) node1 --> node1:nodeUdpPort")
config.dialFromNode("udp", node1_IP, nodeUdpPort, tries, epCount)
By("dialing(http) node1 --> node1:nodeHttpPort")
config.dialFromNode("http", node1_IP, nodeHttpPort, tries, epCount)
By("dialing(udp) test container --> node1:nodeUdpPort")
config.dialFromTestContainer("udp", node1_IP, nodeUdpPort, tries, epCount)
By("dialing(http) container --> node1:nodeHttpPort")
config.dialFromTestContainer("http", node1_IP, nodeHttpPort, tries, epCount)
By("dialing(udp) endpoint container --> node1:nodeUdpPort")
config.dialFromEndpointContainer("udp", node1_IP, nodeUdpPort, tries, epCount)
By("dialing(http) endpoint container --> node1:nodeHttpPort")
config.dialFromEndpointContainer("http", node1_IP, nodeHttpPort, tries, epCount)
// TODO: doesnt work because masquerading is not done
By("TODO: Test disabled. dialing(udp) node --> 127.0.0.1:nodeUdpPort")
//config.dialFromNode("udp", "127.0.0.1", nodeUdpPort, tries, epCount)
// TODO: doesnt work because masquerading is not done
By("Test disabled. dialing(http) node --> 127.0.0.1:nodeHttpPort")
//config.dialFromNode("http", "127.0.0.1", nodeHttpPort, tries, epCount)
node2_IP := strings.TrimSuffix(config.nodes[1], ":22")
By("dialing(udp) node1 --> node2:nodeUdpPort")
config.dialFromNode("udp", node2_IP, nodeUdpPort, tries, epCount)
By("dialing(http) node1 --> node2:nodeHttpPort")
config.dialFromNode("http", node2_IP, nodeHttpPort, tries, epCount)
}
示例10: usrParse
func usrParse(usrStr string) (err error) {
switch {
default:
err = client.Call("Receiver.SendMsg", Args{Token, usrStr}, nil)
case strings.HasPrefix(usrStr, COMM_CREATEROOM):
cName := strings.TrimSuffix(strings.TrimPrefix(usrStr, COMM_CREATEROOM+" "), "\n")
err = client.Call("Receiver.CreateCRoom", Args{Token, cName}, nil)
case strings.HasPrefix(usrStr, COMM_ENTERROOM):
cName := strings.TrimSuffix(strings.TrimPrefix(usrStr, COMM_ENTERROOM+" "), "\n")
err = client.Call("Receiver.JoinCRoom", Args{Token, cName}, nil)
case strings.HasPrefix(usrStr, COMM_LEAVEROOM):
err = client.Call("Receiver.LeaveCRoom", &Token, nil)
case strings.HasPrefix(usrStr, COMM_LISTROOMS):
err = client.Call("Receiver.ListCRooms", &Token, nil)
case strings.HasPrefix(usrStr, COMM_HELPCHAT):
fmt.Print(MESS_HELP)
case strings.HasPrefix(usrStr, COMM_CHANGENAME):
cName := strings.TrimSuffix(strings.TrimPrefix(usrStr, COMM_CHANGENAME+" "), "\n")
err = client.Call("Receiver.ChangeName", Args{Token, cName}, nil)
case strings.HasPrefix(usrStr, COMM_QUITCHAT):
err = client.Call("Receiver.Quit", &Token, nil)
waitG.Done()
}
//fmt.Print(err)
return err
}
示例11: Scan
// Scan scans all the plugin paths and returns all the names it found
func Scan() ([]string, error) {
var names []string
if err := filepath.Walk(socketsPath, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
if fi.Mode()&os.ModeSocket != 0 {
name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))
names = append(names, name)
}
return nil
}); err != nil {
return nil, err
}
for _, path := range specsPaths {
if err := filepath.Walk(path, func(p string, fi os.FileInfo, err error) error {
if err != nil || fi.IsDir() {
return nil
}
name := strings.TrimSuffix(fi.Name(), filepath.Ext(fi.Name()))
names = append(names, name)
return nil
}); err != nil {
return nil, err
}
}
return names, nil
}
示例12: GetBrowseUrl
func (repository *gitRepository) GetBrowseUrl(revision Revision, path string, lineNumber int) string {
rawUrl := fmt.Sprintf("/raw?repo=%s&revision=%s&fileName=%s&lineNumber=%d",
repository.GetRepoId(), string(revision), url.QueryEscape(path), lineNumber)
out, err := repository.runGitCommand(exec.Command("git", "remote", "-v"))
if err != nil {
return rawUrl
}
remotes := strings.Split(strings.Trim(string(out), "\n"), "\n")
for _, remote := range remotes {
remoteParts := strings.SplitN(remote, "\t", 2)
if len(remoteParts) == 2 {
remoteUrl := strings.Split(remoteParts[1], " ")[0]
if isGitHubHttpsUrl(remoteUrl) {
browseSuffix := gitHubBrowseSuffix(revision, path, lineNumber)
return strings.TrimSuffix(remoteUrl, ".git") + browseSuffix
}
if isGitHubSshUrl(remoteUrl) {
browseSuffix := gitHubBrowseSuffix(revision, path, lineNumber)
repoName := strings.SplitN(
strings.TrimSuffix(remoteUrl, ".git"),
":", 2)[1]
return "https://github.com/" + repoName + browseSuffix
}
}
}
return rawUrl
}
示例13: preConfig
// preConfig sanitizes URLs and sets up config with URLs.
func preConfig(cmd *cli.Command, args []string) {
var baseURL string
// Add port details to rootURL else try localhost
// if nothing is given on command line or config.
if rootURL == "" {
rootURL = config.GetString("RootURL")
}
if rootPort == "" {
rootPort = config.GetString("RootPort")
}
if rootPort == "" {
re, _ := regexp.Compile(`:\d+/?`)
port := re.FindString(rootURL)
port = strings.TrimPrefix(port, ":")
port = strings.TrimSuffix(port, "/")
if port != "" {
rootPort = port
} else {
rootPort = "9600"
}
}
config.Set("RootPort", rootPort)
if rootURL != "" {
baseURL = strings.TrimSuffix(rootURL, "/")
baseURL = strings.TrimSuffix(baseURL, ":9600")
baseURL = strings.TrimSuffix(baseURL, ":"+rootPort)
} else {
baseURL = "http://localhost"
}
config.Set("BaseURL", baseURL)
rootURL = baseURL + ":" + rootPort + "/"
config.Set("RootURL", rootURL)
// Give command line options higher priority then
// the corresponding config options.
if format == "" {
format = config.GetString("Format")
}
// if format is still not found just default to tabular format.
if format == "" {
format = "table"
}
config.Set("Format", format)
if platform == "" {
platform = config.GetString("Platform")
}
if platform == "" {
platform = "openstack"
}
config.Set("Platform", platform)
fmt.Println(config.GetString("username"))
err := credential.Initialize()
if err != nil {
log.Printf("Error: %s", err)
os.Exit(1)
}
}
示例14: AddDirRecursive
// AddDirRecursive is just like AddDir, but it also recursively adds
// subdirectories; it returns an error only if the path couldn't be resolved;
// any directories recursed into without go source are ignored.
func (b *Builder) AddDirRecursive(dir string) error {
// This is a bit of a hack. The srcDir argument to Import() should
// properly be the dir of the file which depends on the package to be
// imported, so that vendoring can work properly. We assume that there is
// only one level of vendoring, and that the CWD is inside the GOPATH, so
// this should be safe.
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current directory: %v", err)
}
// First, find it, so we know what path to use.
pkg, err := b.context.Import(dir, cwd, build.FindOnly)
if err != nil {
return fmt.Errorf("unable to *find* %q: %v", dir, err)
}
if err := b.addDir(dir, true); err != nil {
glog.Warningf("Ignoring directory %v: %v", dir, err)
}
prefix := strings.TrimSuffix(pkg.Dir, strings.TrimSuffix(dir, "/"))
filepath.Walk(pkg.Dir, func(path string, info os.FileInfo, err error) error {
if info != nil && info.IsDir() {
trimmed := strings.TrimPrefix(path, prefix)
if trimmed != "" {
if err := b.addDir(trimmed, true); err != nil {
glog.Warningf("Ignoring child directory %v: %v", trimmed, err)
}
}
}
return nil
})
return nil
}
示例15: CppBodyInput
func CppBodyInput(f *parser.Function) (o string) {
if f.Meta == "slot" {
for _, p := range f.Parameters {
o += fmt.Sprintf(", Q_ARG(%v, %v)", CppBodyInputSlotValue(f, p), cppInput(p.Name, p.Value, f))
}
return
}
if f.Meta == "signal" {
for _, p := range f.Parameters {
if isEnum(f.Class(), cleanValue(p.Value)) {
o += fmt.Sprintf("%v, ", cppEnum(f, cleanValue(p.Value), true))
} else {
o += fmt.Sprintf("%v, ", p.Value)
}
}
return strings.TrimSuffix(o, ", ")
}
for _, p := range f.Parameters {
o += fmt.Sprintf("%v, ", cppInput(p.Name, p.Value, f))
}
return strings.TrimSuffix(o, ", ")
}