func NewTaobaoESStore(c Config) *TaobaoESStore { var esstor = &TaobaoESStore{} var err error var m = mongodb.MongodbConf{} esstor.client, err = elastic.NewClient(elastic.SetURL(strings.Split(c.ESHost, ",")...)) if err != nil { log.Fatal(err) } m.Db = "xu_precise" m.Host = c.MgoStoreHost m.Port = c.MgoStorePort m.UName = c.mgoStoreUname m.Upwd = c.mgoStoreUpwd esstor.store, err = mongodb.NewMongodb(m) if err != nil { log.Fatal(err) } esstor.prefix = c.TablePrefixe esstor.initCategory() esstor.geohost = c.GeoHost esstor.bulk = esstor.client.Bulk() return esstor }
func Action(c *cli.Context) { var err error if err = config.Load(c.String("conf")); err != nil { log.Fatal(err) } if err = models.InitDB(); err != nil { log.Fatal(err) } cfg := config.Config xrpc.HandleRpc() m := newMartini() routers.Register(m) http.Handle("/", m) if err = models.ResetAllTaskStatus(); err != nil { log.Fatalf("reset all task status: %v", err) } go drainTask() listenAddr := fmt.Sprintf("%s:%d", cfg.Server.Addr, cfg.Server.Port) log.Printf("listen %s\n", strconv.Quote(listenAddr)) log.Fatal(http.ListenAndServe(listenAddr, nil)) }
func main() { flag.Usage = func() { fmt.Printf(`Usage: fswatch [OPTIONS...] <arg...> OPTIONS: -ext="" Extenton seprated by , eg: cpp,c,h -v Show verbose info Example: fswatch -ext cpp,c,h make -f Makefile `) } flagParse() if len(os.Args) == 1 && !confExists { fmt.Printf("Create %s file [y/n]: ", strconv.Quote(JSONCONF)) var yn string = "y" fmt.Scan(&yn) gw.Command = "echo helloworld" if strings.ToUpper(strings.TrimSpace(yn)) == "Y" { data, _ := json.MarshalIndent(gw, "", " ") ioutil.WriteFile(JSONCONF, data, 0644) fmt.Printf("use notepad++ or vim to edit %s\n", strconv.Quote(JSONCONF)) } return } if flag.NArg() > 0 { gw.Command = []string(flag.Args()) } if extInclude != "" { for _, ext := range strings.Split(extInclude, ",") { gw.Include = append(gw.Include, "\\."+ext+"$") } } switch gw.Command.(type) { default: log.Fatal("check you config file. \"command\" must be string or []string") case string: if runtime.GOOS == "windows" { gw.cmd = []string{"cmd", "/c", gw.Command.(string)} } else { gw.cmd = []string{"bash", "-c", gw.Command.(string)} } case []string: gw.cmd = gw.Command.([]string) } log.Fatal(gw.Watch()) }
func (this *gowatch) Watch() (err error) { if this.w, err = fsnotify.NewWatcher(); err != nil { return } for _, path := range this.Paths { if err = this.watchDirAndChildren(os.ExpandEnv(path)); err != nil { log.Fatal(err) } } this.modtime = make(map[string]time.Time) this.sig = make(chan string) for _, patten := range this.Exclude { this.reExclude = append(this.reExclude, regexp.MustCompile(patten)) } for _, patten := range this.Include { this.reInclude = append(this.reInclude, regexp.MustCompile(patten)) } this.sigOS = make(chan os.Signal, 1) signal.Notify(this.sigOS, syscall.SIGINT) go this.drainExec() this.drainEvent() return }
func main() { addr := flag.String("http", ":8877", "HTTP service address") hookby := flag.String("hookby", "gogs", "hook by which service") flag.Parse() log.Printf("Hook for %s, Listening on %s\n", *hookby, *addr) log.Fatal(http.ListenAndServe(*addr, nil)) }
func main() { gw := &gowatch{ Paths: []string{"."}, Depth: 2, Exclude: []string{}, Include: []string{"\\.(go|py|php|java|cpp|h|rb)$"}, AutoRestart: false, RestartInterval: 0, KillSignal: "KILL", } gw.Env = map[string]string{"POWERD_BY": "github.com/codeskyblue/fswatch"} // load JSONCONF if fd, err := os.Open(JSONCONF); err == nil { if er := json.NewDecoder(fd).Decode(gw); er != nil { log.Fatal(er) } for key, val := range gw.Env { os.Setenv(key, val) } } flag.DurationVar(&gw.RestartInterval, "ri", gw.RestartInterval, "restart interval") flag.BoolVar(&gw.AutoRestart, "r", gw.AutoRestart, "enable autorestart") flag.StringVar(&gw.KillSignal, "k", gw.KillSignal, "kill signal") flag.Parse() if flag.NArg() > 0 { gw.Command = flag.Args() } if len(gw.Command) == 0 { fmt.Printf("initial %s file [y/n]: ", JSONCONF) var yn string = "y" fmt.Scan(&yn) gw.Command = []string{"bash", "-c", "whoami"} if strings.ToUpper(strings.TrimSpace(yn)) == "Y" { data, _ := json.MarshalIndent(gw, "", " ") ioutil.WriteFile(JSONCONF, data, 0644) fmt.Printf("use notepad++ or vim to edit %s\n", strconv.Quote(JSONCONF)) } return } log.Fatal(gw.Watch()) }
func Action(c *cli.Context) { var goos, goarch = c.String("os"), c.String("arch") //var depth = c.Int("depth") var output = c.String("output") var gom = c.String("gom") var nobuild = c.Bool("nobuild") var adds = c.StringSlice("add") var rmflag = c.Bool("rm") if c.Bool("debug") { log.SetOutputLevel(log.Ldebug) } if c.Bool("init") { data, _ := goyaml.Marshal(DefaultPcfg) fmt.Print(string(data)) return } log.Debugf("os: %s, arch: %s", goos, goarch) var err error defer func() { if err != nil { log.Fatal(err) } }() sess := sh.NewSession() sess.SetEnv("GOOS", goos) sess.SetEnv("GOARCH", goarch) sess.ShowCMD = true gomarr := strings.Fields(gom) if len(gomarr) >= 1 { sess.Alias("go", gomarr[0], gomarr[1:]...) } // parse yaml pcfg, err := ReadPkgConfig(RCFILE) if err != nil { return } pwd, _ := os.Getwd() gobin := filepath.Join(pwd, sanitizedName(pcfg.Settings.TargetDir)) sess.SetEnv("GOBIN", gobin) log.Debugf("set env GOBIN=%s", gobin) log.Debug("config:", pcfg) // pcfg.Filesets.Includes = append(pcfg.Filesets.Includes, adds...) var skips []*regexp.Regexp for _, str := range pcfg.Filesets.Excludes { skips = append(skips, regexp.MustCompile("^"+str+"$")) } var needs []*regexp.Regexp for _, str := range pcfg.Filesets.Includes { needs = append(needs, regexp.MustCompile("^"+str+"$")) } //log.Infof("archive file to: %s", output) var z Archiver hasExt := func(ext string) bool { return strings.HasSuffix(output, ext) } switch { case hasExt(".zip"): fmt.Println("zip format") z, err = CreateZip(output) case hasExt(".tar"): fmt.Println("tar format") z, err = CreateTar(output) case hasExt(".tgz"): fallthrough case hasExt(".tar.gz"): fmt.Println("tar.gz format") z, err = CreateTgz(output) default: fmt.Println("unsupport file archive format") os.Exit(1) } if err != nil { return } var files = []string{} var buildFiles = pcfg.Settings.Outfiles // build source if !nobuild { targetDir := sanitizedName(pcfg.Settings.TargetDir) if !sh.Test("dir", targetDir) { os.MkdirAll(targetDir, 0755) } symdir := filepath.Join(targetDir, goos+"_"+goarch) if err = os.Symlink(gobin, symdir); err != nil { log.Fatalf("os symlink error: %s", err) } defer os.Remove(symdir) // opts := []string{"install", "-v"} // opts = append(opts, strings.Fields(pcfg.Settings.Addopts)...) // TODO: here need to use shell args parse lib if pcfg.Settings.Build == "" { pcfg.Settings.Build = DEFAULT_BUILD } if err = sess.Command("bash", "-c", pcfg.Settings.Build).Run(); err != nil { // if err = sess.Command("go", opts).Run(); err != nil { return } os.Remove(symdir) // I have to do it twice if len(buildFiles) == 0 { cwd, _ := os.Getwd() program := filepath.Base(cwd) buildFiles = append(buildFiles, program) } for _, filename := range buildFiles { if goos == "windows" { filename += ".exe" } files = append(files, filename) } } log.Debug("archive files") depth := pcfg.Filesets.Depth for _, filename := range pcfg.Filesets.Includes { fs, err := findFiles(filename, depth, skips) if err != nil { return } files = append(files, fs...) } // adds - parse by cli files = append(files, adds...) uniqset := make(map[string]bool, len(files)) for _, file := range files { file = sanitizedName(file) if uniqset[file] { continue } uniqset[file] = true log.Infof("zip add file: %v", file) if err = z.Add(file); err != nil { return } } log.Info("finish archive file") err = z.Close() if rmflag { for _, filename := range buildFiles { if goos == "windows" { filename += ".exe" } os.Remove(filename) } } }
func Action(c *cli.Context) { var goos, goarch = c.String("os"), c.String("arch") var depth = c.Int("depth") var output = c.String("output") var gom = c.String("gom") var nobuild = c.Bool("nobuild") var adds = c.StringSlice("add") if goos == "" { goos = runtime.GOOS } if goarch == "" { goarch = runtime.GOARCH } var err error defer func() { if err != nil { log.Fatal(err) } }() sess := sh.NewSession() sess.SetEnv("GOOS", goos) sess.SetEnv("GOARCH", goarch) sess.ShowCMD = true gomarr := strings.Fields(gom) if len(gomarr) >= 1 { sess.Alias("go", gomarr[0], gomarr[1:]...) } // parse yaml pcfg, err := config.ReadPkgConfig(config.RCFILE) if err != nil { return } // var pcfg = new(config.PackageConfig) // if sh.Test("file", config.RCFILE) { // data, er := ioutil.ReadFile(config.RCFILE) // if er != nil { // err = er // return // } // if err = goyaml.Unmarshal(data, pcfg); err != nil { // return // } // } else { // pcfg = config.DefaultPcfg // } pwd, _ := os.Getwd() gobin := filepath.Join(pwd, sanitizedName(pcfg.Settings.TargetDir)) sess.SetEnv("GOBIN", gobin) log.Debugf("set env GOBIN=%s", gobin) log.Debug("config:", pcfg) pcfg.Filesets.Includes = append(pcfg.Filesets.Includes, adds...) var skips []*regexp.Regexp for _, str := range pcfg.Filesets.Excludes { skips = append(skips, regexp.MustCompile("^"+str+"$")) } log.Infof("archive file to: %s", output) var z Archiver hasExt := func(ext string) bool { return strings.HasSuffix(output, ext) } switch { case hasExt(".zip"): fmt.Println("zip format") z, err = CreateZip(output) case hasExt(".tar"): fmt.Println("tar format") z, err = CreateTar(output) case hasExt(".tgz"): fallthrough case hasExt(".tar.gz"): fmt.Println("tar.gz format") z, err = CreateTgz(output) default: fmt.Println("unsupport file archive format") os.Exit(1) } if err != nil { return } var files []string // build source if !nobuild { targetDir := sanitizedName(pcfg.Settings.TargetDir) if !sh.Test("dir", targetDir) { os.MkdirAll(targetDir, 0755) } symdir := filepath.Join(targetDir, goos+"_"+goarch) if err = os.Symlink(gobin, symdir); err != nil { log.Fatalf("os symlink error: %s", err) } defer os.Remove(symdir) opts := []string{"install", "-v"} opts = append(opts, strings.Fields(pcfg.Settings.Addopts)...) // TODO: here need to use shell args parse lib if err = sess.Command("go", opts).Run(); err != nil { return } os.Remove(symdir) // I have to do it twice cwd, _ := os.Getwd() program := filepath.Base(cwd) if goos == "windows" { program += ".exe" } if sh.Test("file", program) { files = append(files, program) } } log.Debug("archive files") for _, filename := range pcfg.Filesets.Includes { fs, err := findFiles(filename, depth, skips) if err != nil { return } files = append(files, fs...) } for _, file := range files { log.Infof("zip add file: %v", file) if err = z.Add(file); err != nil { return } } log.Info("finish archive file") err = z.Close() }