func cmdExchange(cmd command) { switch cmd.Action { case "PRINT", "DISPLAY": com.ColorLog("[%s] DEBUG( %s ) --> %s\n", levelToCmdFormat(cmd.Level), getTitle(cmd), watchParametersToStr(cmd.Parameters)) case "BREAK": if App.PrintStack { com.ColorLog("[%s] BREAK:\n# %s #", levelToCmdFormat(cmd.Level), cmd.Parameters["go.stack"]) if !App.SkipSuspend && App.PrintSource { fmt.Print("press ENTER to view source...") fmt.Scanln() } } else { com.ColorLog("[%s] BREAK: 'print_stack' disenabled.\n", levelToCmdFormat(cmd.Level)) } if App.PrintSource { line, _ := strconv.Atoi(cmd.Parameters["go.line"]) fmt.Println() com.ColorLog("[%s] Source( %s ):\n%s", levelToCmdFormat(cmd.Level), cmd.Parameters["go.file"], getFileSource(cmd.Parameters["go.file"], line)) } if !App.SkipSuspend { fmt.Print("press ENTER to continue...") fmt.Scanln() } } }
func main() { com.ColorLog("[INFO] Go Walker Local v%s.\n", APP_VER) var err error srcPath, err = utils.GetAppPath("github.com/Unknwon/gowalker/gw-local", "conf") if err != nil { com.ColorLog("[ERRO] Cannot assign 'srcPath'[ %s ]\n", err) return } com.ColorLog("[INFO] File server( %s )\n", srcPath) // Get 'args'. args := os.Args[1:] if len(args) > 0 { hp, err := strconv.Atoi(args[0]) if err == nil { httpPort = hp } } http.HandleFunc("/", serverHome) http.HandleFunc("/index", serverIndex) http.HandleFunc("/funcs", serverFuncs) http.HandleFunc("/about", serverAbout) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(srcPath+"static/")))) http.ListenAndServe(":"+fmt.Sprint(httpPort), nil) }
// Display sends variable name,value pairs to the debugger. Values are formatted using %#v. // The parameter 'nameValuePairs' must be even sized. func (wp *WatchPoint) Display(nameValuePairs ...interface{}) *WatchPoint { if wp.disabled { return wp } _, file, line, ok := runtime.Caller(wp.offset) cmd := command{ Action: "DISPLAY", Level: levelToStr(wp.watchLevel), } if ok { cmd.addParam("go.file", file) cmd.addParam("go.line", fmt.Sprint(line)) } l := len(nameValuePairs) if l%2 == 0 { for i := 0; i < l; i += 2 { k := nameValuePairs[i] v := nameValuePairs[i+1] cmd.addParam(fmt.Sprint(k), fmt.Sprintf("%#v", v)) } } else { com.ColorLog("[WARN] BW: Missing variable for Display(...) in: %v:%v.\n", file, line) wp.disabled = true return wp } channelExchangeCommands(wp.watchLevel, cmd) return wp }
func catchExit() { sigTerm := syscall.Signal(15) sig := make(chan os.Signal) signal.Notify(sig, os.Interrupt, sigTerm) for { switch <-sig { case os.Interrupt, sigTerm: fmt.Println() com.ColorLog("[WARN] INTERRUPT SIGNAL DETECTED!!!\n") routers.FlushCache() com.ColorLog("[WARN] READY TO EXIT\n") os.Exit(0) } } }
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { timer := time.AfterFunc(*requestTimeout, func() { t.t.CancelRequest(req) com.ColorLog("[WARN] Canceled request for %s, please interrupt the program.\n", req.URL) }) defer timer.Stop() resp, err := t.t.RoundTrip(req) return resp, err }
func parseconf(filename string) bool { var err error if !com.IsExist(filename) { com.WriteFile(filename, []byte(confTpl)) } cfg, err := goconfig.LoadConfigFile(filename) if err != nil { com.ColorLog("[ERROR] Fail to load (%s) [%s]\n", filename, err) return false } gUsername = cfg.MustValue("main", "username") if len(gUsername) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'username'\n", filename) return false } gPassword = cfg.MustValue("main", "password") if len(gPassword) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'password'\n", filename) return false } gCommand = cfg.MustValue("main", "command") if len(gCommand) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'command'\n", filename) return false } gTitle = cfg.MustValue("mail", "title") if len(gTitle) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'title'\n", filename) return false } gBody = cfg.MustValue("mail", "body") if len(gBody) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'body'\n", filename) return false } gMode = cfg.MustValue("mail", "mode") if len(gMode) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'mode'\n", filename) return false } gMaillist = cfg.MustValue("mail", "maillist") mailUsername = cfg.MustValue("mail", "username") if len(mailUsername) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'username'\n", filename) com.ColorLog("[HINT] Please set mail 'username' in '%s'\n", filename) return false } mailPassword = cfg.MustValue("mail", "password") if len(mailPassword) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'password'\n", filename) com.ColorLog("[HINT] Please set mail 'password' in '%s'\n", filename) return false } mailHost = cfg.MustValue("mail", "host") if len(mailHost) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'host'\n", filename) com.ColorLog("[HINT] Please set mail 'host' in '%s'\n", filename) return false } mailPort = cfg.MustInt("mail", "port") if mailPort < 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'port'\n", filename) com.ColorLog("[HINT] Please set mail 'port' in '%s'\n", filename) return false } gFr_addr = cfg.MustValue("mail", "fr_addr") if len(gFr_addr) == 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'fr_addr'\n", filename) return false } gTimeout = cfg.MustInt("main", "timeout") if gTimeout <= 0 { com.ColorLog("[ERROR] No valid setting in '%s' key 'timeout'\n", filename) return false } return true }
// getGoogleDoc downloads raw files from code.google.com. func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath string, nod *Node, ctx *cli.Context) ([]string, error) { setupGoogleMatch(match) // Check version control. if err := getGoogleVCS(client, match); err != nil { return nil, errors.New("fail to get vcs " + nod.ImportPath + " : " + err.Error()) } switch nod.Type { case BRANCH: if len(nod.Value) == 0 { match["tag"] = defaultTags[match["vcs"]] // Only get and check revision with the latest version. p, err := com.HttpGetBytes(client, com.Expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/?r={tag}", match), nil) if err != nil { log.Error("GET", "Fail to get revision") log.Error("", err.Error()) break } if m := googleRevisionRe.FindSubmatch(p); m == nil { log.Error("GET", "Fail to get revision") log.Error("", err.Error()) } else { etag := string(m[1]) if etag == nod.Revision { log.Log("GET Package hasn't changed: %s", nod.ImportPath) return nil, nil } nod.Revision = etag } } else { match["tag"] = nod.Value } case TAG, COMMIT: match["tag"] = nod.Value default: return nil, errors.New("Unknown node type: " + nod.Type) } var installPath string projectPath := GetProjectPath(nod.ImportPath) if nod.ImportPath == nod.DownloadURL { suf := "." + nod.Value if len(suf) == 1 { suf = "" } installPath = installRepoPath + "/" + projectPath + suf } else { installPath = installRepoPath + "/" + projectPath } // Remove old files. os.RemoveAll(installPath + "/") os.MkdirAll(installPath+"/", os.ModePerm) if match["vcs"] == "svn" { com.ColorLog("[WARN] SVN detected, may take very long time.\n") rootPath := com.Expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}", match) d, f := path.Split(rootPath) err := downloadFiles(client, match, d, installPath+"/", match["tag"], []string{f + "/"}) if err != nil { return nil, errors.New("Fail to download " + nod.ImportPath + " : " + err.Error()) } } p, err := com.HttpGetBytes(client, com.Expand("http://{subrepo}{dot}{repo}.googlecode.com/archive/{tag}.zip", match), nil) if err != nil { return nil, errors.New("Fail to download " + nod.ImportPath + " : " + err.Error()) } r, err := zip.NewReader(bytes.NewReader(p), int64(len(p))) if err != nil { return nil, errors.New(nod.ImportPath + " -> new zip: " + err.Error()) } nameLen := strings.Index(r.File[0].Name, "/") dirPrefix := match["dir"] if len(dirPrefix) != 0 { dirPrefix = dirPrefix[1:] + "/" } dirs := make([]string, 0, 5) for _, f := range r.File { absPath := strings.Replace(f.Name, f.Name[:nameLen], installPath, 1) // Create diretory before create file. dir := path.Dir(absPath) if !checkDir(dir, dirs) && !(!ctx.Bool("example") && strings.Contains(absPath, "example")) { dirs = append(dirs, dir+"/") os.MkdirAll(dir+"/", os.ModePerm) } // Get file from archive. r, err := f.Open() if err != nil { return nil, err } fbytes := make([]byte, f.FileInfo().Size()) _, err = io.ReadFull(r, fbytes) if err != nil { return nil, err } _, err = com.SaveFile(absPath, fbytes) if err != nil { return nil, err } } var imports []string // Check if need to check imports. if nod.IsGetDeps { for _, d := range dirs { importPkgs, err := CheckImports(d, match["importPath"], nod) if err != nil { return nil, err } imports = append(imports, importPkgs...) } } return imports, err }
func ExampleColorLog() { com.ColorLog(fmt.Sprintf( "[WARN] This is a tesing log that should be colored, path( %s ),"+ " highlight # %s #, error [ %s ].", "path to somewhere", "highlighted content", "tesing error")) }