/** * 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 }
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 } }
// 回滚部署 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 }
// 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 }
// 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) } } } } }
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 }
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") }
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 }
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) }
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 }
// 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 }
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 }
// 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) } }
// 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 }
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, ", ") }
func server(cmd *cobra.Command, args []string) { InitializeConfig() if BaseUrl == "" { BaseUrl = "http://localhost" } if !strings.HasPrefix(BaseUrl, "http://") { BaseUrl = "http://" + BaseUrl } if serverAppend { Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") + ":" + strconv.Itoa(serverPort) } else { Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") } build(serverWatch) // Watch runs its own server as part of the routine if serverWatch { fmt.Println("Watching for changes in", Config.GetAbsPath(Config.ContentDir)) err := NewWatcher(serverPort) if err != nil { fmt.Println(err) } } serve(serverPort) }
// getDnsZones returns the DNS zones matching dnsZoneName and dnsZoneID (if specified) func getDnsZones(dnsZoneName string, dnsZoneID string, dnsZonesInterface dnsprovider.Zones) ([]dnsprovider.Zone, error) { // TODO: We need query-by-name and query-by-id functions dnsZones, err := dnsZonesInterface.List() if err != nil { return nil, err } var matches []dnsprovider.Zone findName := strings.TrimSuffix(dnsZoneName, ".") for _, dnsZone := range dnsZones { if dnsZoneID != "" { if dnsZoneID != dnsZone.ID() { continue } } if findName != "" { if strings.TrimSuffix(dnsZone.Name(), ".") != findName { continue } } matches = append(matches, dnsZone) } return matches, nil }
// Strip ... func Strip(str string) string { dirty := true strippedStr := str for dirty { hasWhiteSpacePrefix := false if strings.HasPrefix(strippedStr, " ") { hasWhiteSpacePrefix = true strippedStr = strings.TrimPrefix(strippedStr, " ") } hasWhiteSpaceSuffix := false if strings.HasSuffix(strippedStr, " ") { hasWhiteSpaceSuffix = true strippedStr = strings.TrimSuffix(strippedStr, " ") } hasNewlinePrefix := false if strings.HasPrefix(strippedStr, "\n") { hasNewlinePrefix = true strippedStr = strings.TrimPrefix(strippedStr, "\n") } hasNewlineSuffix := false if strings.HasSuffix(strippedStr, "\n") { hasNewlinePrefix = true strippedStr = strings.TrimSuffix(strippedStr, "\n") } if !hasWhiteSpacePrefix && !hasWhiteSpaceSuffix && !hasNewlinePrefix && !hasNewlineSuffix { dirty = false } } return strippedStr }
func (es *EventStream) filterEventJobOffered(resp *etcd.Response) *event.Event { if resp.Action != "set" { return nil } dir, base := path.Split(resp.Node.Key) if base != "object" { return nil } dir = path.Dir(strings.TrimSuffix(dir, "/")) prefix := path.Base(strings.TrimSuffix(dir, "/")) if prefix != offerPrefix { return nil } jo := es.registry.getJobOfferFromJSON(resp.Node.Value) if jo == nil { return nil } return &event.Event{"EventJobOffered", *jo, nil} }
func mkFile(path string, fi os.FileInfo) (err error) { fset := token.NewFileSet() f, err := parser.ParseFile(fset, path, nil, parser.ParseComments) if err != nil { log.Fatalf("Could not parse file: %s", err) } res := &ThriftCli{ Package: f.Name.Name, // Def: fmt.Sprintf("%s", suitecase.Camelize(f.Name.Name)), Fname: strings.TrimSuffix(fi.Name(), ".go") + "_gen_client.go", } for _, decl := range f.Decls { funcDecl, ok := decl.(*ast.FuncDecl) if !ok { continue } if strings.HasSuffix(funcDecl.Name.Name, "ClientFactory") { n := strings.TrimPrefix(strings.TrimSuffix( funcDecl.Name.Name, "ClientFactory"), "New") res.Svc = n break } } res.Def = res.Svc == suitecase.Camelize(res.Package) renderFile(res) return }
func matchHostnames(pattern, host string) bool { host = strings.TrimSuffix(host, ".") pattern = strings.TrimSuffix(pattern, ".") if len(pattern) == 0 || len(host) == 0 { return false } patternParts := strings.Split(pattern, ".") hostParts := strings.Split(host, ".") if len(patternParts) != len(hostParts) { return false } for i, patternPart := range patternParts { if i == 0 && patternPart == "*" { continue } if patternPart != hostParts[i] { return false } } return true }
// 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 { // First, find it, so we know what path to use. pkg, err := b.context.Import(dir, ".", 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 }
// TryMakeURI converts a repository clone URL, such as // "git://github.com/user/repo.git", to a normalized URI string, such // as "github.com/user/repo" lexically. TryMakeURI returns an error if // cloneURL is empty or malformed. func TryMakeURI(cloneURL string) (string, error) { if cloneURL == "" { return "", errors.New("MakeURI: empty clone URL") } // Handle "user@host:path" and "host:path" (assumed SSH). if strings.Contains(cloneURL, ":") && !strings.Contains(cloneURL, "://") { cloneURL = "ssh://" + strings.Replace(cloneURL, ":", "/", -1) } url, err := url.Parse(cloneURL) if err != nil { return "", err } else if url.Path == "" || url.Path == "/" { return "", fmt.Errorf("determining URI from repo clone URL failed: missing path from URL (%q)", cloneURL) } else if url.Host == "" && (url.Path[0] == '/' || !strings.Contains(strings.Trim(url.Path, "/"), "/")) { // We ensure our Path doesn't look like the output of TryMakeURI // so that the output of this function is a fixed point. // ie TryMakeURI("github.com/user/repo") == ("github.com/user/repo", nil), // not an error. return "", fmt.Errorf("determining URI from repo clone URL failed: missing host from URL (%q)", cloneURL) } uri := strings.TrimSuffix(url.Path, ".git") uri = path.Clean(uri) uri = strings.TrimSuffix(uri, "/") return strings.ToLower(url.Host) + uri, nil }
// Determines if the path matches the pattern of the route. // Fill the params map with variables found in the path. func (r *route) match(path string) bool { splitPattern := strings.Split(strings.TrimSuffix(r.pattern, "/"), "/") splitPath := strings.Split(strings.TrimSuffix(path, "/"), "/") if len(splitPattern) != len(splitPath) { return false } for idx, val := range splitPattern { if val != splitPath[idx] { if !strings.HasPrefix(val, ":") { return false } variable := strings.TrimPrefix(val, ":") if matched, _ := regexp.MatchString("^[a-zA-Z0-9_-]+$", variable); !matched { return false } r.params[variable] = splitPath[idx] } } return true }
// setRomanaRootURL sanitizes rootURL and rootPort and also // sets baseURL which is needed to connect to other romana // services. func setRomanaRootURL() { // Variables used for configuration and flags. var baseURL string var rootURL string var rootPort string // Add port details to rootURL else try localhost // if nothing is given on command line or config. rootURL = config.GetString("RootURL") 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) }
// parseTime converts from the string youtube returns to a time.Duration func (yt YouTube) parseTime(duration, regex string) time.Duration { var days, hours, minutes, seconds, totalSeconds int64 if duration != "" { timestampExp := regexp.MustCompile(regex) timestampMatch := timestampExp.FindStringSubmatch(strings.ToUpper(duration)) timestampResult := make(map[string]string) for i, name := range timestampExp.SubexpNames() { if i < len(timestampMatch) { timestampResult[name] = timestampMatch[i] } } if timestampResult["days"] != "" { days, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["days"], "D"), 10, 32) } if timestampResult["hours"] != "" { hours, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["hours"], "H"), 10, 32) } if timestampResult["minutes"] != "" { minutes, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["minutes"], "M"), 10, 32) } if timestampResult["seconds"] != "" { seconds, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["seconds"], "S"), 10, 32) } totalSeconds = int64((days * 86400) + (hours * 3600) + (minutes * 60) + seconds) } else { totalSeconds = 0 } output, _ := time.ParseDuration(strconv.Itoa(int(totalSeconds)) + "s") return output }
func (persister *Persister) InsertTable(tablename string, colmap map[string]interface{}) error { var insertCols string = "(" var insertParams string = "(" var insertVals []interface{} for colname := range colmap { if colmap[colname] == nil { log.Println("colname ", colname, " has nil value") continue } insertCols = insertCols + colname + "," insertParams = insertParams + "?," value := new(interface{}) *value = colmap[colname] insertVals = append(insertVals, value) } insertCols = strings.TrimSuffix(insertCols, ",") + ")" insertParams = persister.parameterize(strings.TrimSuffix(insertParams, ",") + ")") stmt, err := persister.Db.Prepare("insert into " + tablename + insertCols + " values " + insertParams) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(insertVals...) return err }
func (ta *Table) AddColumn(name string, columnType string, extra string) { index := len(ta.Columns) ta.Columns = append(ta.Columns, TableColumn{Name: name}) if strings.Contains(columnType, "int") || strings.HasPrefix(columnType, "year") { ta.Columns[index].Type = TYPE_NUMBER } else if columnType == "float" || columnType == "double" { ta.Columns[index].Type = TYPE_FLOAT } else if strings.HasPrefix(columnType, "enum") { ta.Columns[index].Type = TYPE_ENUM ta.Columns[index].EnumValues = strings.Split(strings.Replace( strings.TrimSuffix( strings.TrimPrefix( columnType, "enum("), ")"), "'", "", -1), ",") } else if strings.HasPrefix(columnType, "set") { ta.Columns[index].Type = TYPE_SET ta.Columns[index].SetValues = strings.Split(strings.Replace( strings.TrimSuffix( strings.TrimPrefix( columnType, "set("), ")"), "'", "", -1), ",") } else { ta.Columns[index].Type = TYPE_STRING } if extra == "auto_increment" { ta.Columns[index].IsAuto = true } }
// NewEngine creates new services for data from config settings func NewEngine() error { gh := strings.TrimSuffix(viper.GetString("gitlab.url"), "/") d := strings.TrimSuffix(viper.GetString("server.hostname"), "/") gitlab.NewEngine(&gitlab.Config{ BasePath: gh + "/api/v3", Domain: d, Oauth2: &oauth2.Config{ ClientID: viper.GetString("gitlab.client"), ClientSecret: viper.GetString("gitlab.secret"), Endpoint: oauth2.Endpoint{ AuthURL: gh + "/oauth/authorize", TokenURL: gh + "/oauth/token", }, RedirectURL: d + "/assets/html/user/views/oauth.html", }, }) c = redis.NewClient(&redis.Options{ Addr: viper.GetString("redis.addr"), Password: viper.GetString("redis.password"), DB: int64(viper.GetInt("redis.db")), }) _, err := c.Ping().Result() if err != nil { log.Fatalf("Error connection to redis %s", err.Error()) } return nil }
func server(cmd *cobra.Command, args []string) { InitializeConfig() if BaseUrl == "" { BaseUrl = "http://localhost" } if !strings.HasPrefix(BaseUrl, "http://") { BaseUrl = "http://" + BaseUrl } if serverAppend { viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")+":"+strconv.Itoa(serverPort)) } else { viper.Set("BaseUrl", strings.TrimSuffix(BaseUrl, "/")) } build(serverWatch) // Watch runs its own server as part of the routine if serverWatch { jww.FEEDBACK.Println("Watching for changes in", helpers.AbsPathify(viper.GetString("ContentDir"))) err := NewWatcher(serverPort) if err != nil { fmt.Println(err) } } serve(serverPort) }