Beispiel #1
0
func dump(w http.ResponseWriter, r *http.Request) {
	var jsonData interface{}

	defer r.Body.Close()

	r.Header.Set("Host", r.Host)

	for _, header := range sortHeaderKeys(r.Header) {
		fmt.Printf("%s %s: %s\n", color.YellowString("[H]"), header, r.Header.Get(header))
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Printf("error: unable to read request body: %s", err)
		return
	}

	if getContentType(r) == "application/json" {
		if err := json.Unmarshal(body, &jsonData); err != nil {
			fmt.Printf("%s error: request Content-Type is application/json, but unable to process JSON %s\n", color.RedString("[!]"), err)
			fmt.Printf("%s %s\n", color.CyanString("[B]"), body)
		} else {
			jf := prettyjson.NewFormatter()
			j, _ := jf.Format(body)
			fmt.Printf("%s %s\n", color.CyanString("[B]"), j)
		}
	} else {
		fmt.Printf("%s %s\n", color.CyanString("[B]"), body)
	}
}
Beispiel #2
0
func showResult(commits []*commit, keyword string) {

	repoWidth := maxRepoWidth(commits)
	repoFmt := fmt.Sprintf("%%-%ds", repoWidth)

	urlWidth := maxURLWidth(commits)
	urlFmt := fmt.Sprintf("%%-%ds", urlWidth)

	msgWidth := maxMessageWidth(commits)

	fmt.Printf(" %s | %s | %s | message \n",
		color.BlueString(repoFmt, "Repository"),
		color.CyanString("%-7s", "sha1"),
		fmt.Sprintf(urlFmt, "url"),
	)
	fmt.Println(strings.Repeat("-", repoWidth+msgWidth+urlWidth+18))

	for _, c := range commits {
		fmt.Printf(" %s | %7s | %s | %s\n",
			color.BlueString(repoFmt, c.Repo),
			color.CyanString(c.Sha1),
			fmt.Sprintf(urlFmt, c.CommitURL),
			highlightWords(c.Message, keyword),
		)
	}
}
Beispiel #3
0
//
// required for serach options
//
func getSearchOpt() (SearchType, FilterType) {
	var (
		search SearchType = SearchBySubject
		filter FilterType = SearchFilterAll
	)

	for {
		color.White("Please select search range:\n")
		for k := SearchFilterAll; k <= SearchConference; k++ {
			fmt.Fprintf(color.Output, "\t %s: %s\n", color.CyanString("%d", k), searchFilterHints[k])
		}

		fmt.Fprintf(color.Output, "$ %s", color.CyanString("select: "))
		s := getInputString()
		if len(s) > 0 {
			selected, err := strconv.ParseInt(s, 16, 32)
			if err != nil || selected < int64(SearchFilterAll) || selected > int64(SearchConference) {
				color.Red("Invalid selection\n")
				continue
			}

			filter = FilterType(selected)
		}

		break
	}

	return search, filter
}
Beispiel #4
0
func (o *Decorator) DecorateRequest(req model.Request) {
	fmt.Println()
	c := color.New(color.FgGreen, color.Bold)
	c.Println("Request")

	fmt.Printf(" • Query: %s", color.CyanString(req.Query))
	fmt.Println()
	fmt.Printf(" • Time start: %s", color.CyanString(fmt.Sprint(req.TimeStart.Format(time.Stamp))))
	fmt.Println()
	fmt.Printf(" • Time end: %s", color.CyanString(fmt.Sprint(req.TimeEnd.Format(time.Stamp))))
	fmt.Println()
	fmt.Printf(" • Size: %s", color.CyanString(fmt.Sprint(req.Size)))
	fmt.Println()
	fmt.Println()
}
Beispiel #5
0
func (fs *fsSort) sortAllFiles() error {
	fs.verbf("sorting files...")
	//perform sort
	if fs.DryRun {
		log.Println(color.CyanString("[Dryrun]"))
	}
	//sort concurrency-many files at a time,
	//wait for all to complete and show errors
	queue := make(chan bool, fs.Concurrency)
	wg := &sync.WaitGroup{}
	sortFile := func(file *fileSort) {
		if err := fs.sortFile(file); err != nil {
			log.Printf("[#%d/%d] %s\n  └─> %s\n", file.id, len(fs.sorts), color.RedString(file.path), err)
		}
		<-queue
		wg.Done()
	}
	for _, file := range fs.sorts {
		wg.Add(1)
		queue <- true
		go sortFile(file)
	}
	wg.Wait()
	return nil
}
Beispiel #6
0
func printProject(project *Project, depth int, buffer *bytes.Buffer, noColor bool, showpath bool) {
	color.NoColor = noColor

	buffer.WriteString(strings.Repeat("    ", depth))
	buffer.WriteString(color.GreenString(project.ArtifactID))
	buffer.WriteString(" (")
	buffer.WriteString(project.Version)
	buffer.WriteString(")")

	if project.MismatchParentVersion != "" {
		buffer.WriteString(color.YellowString(" Warning: looking for parent version: "))
		buffer.WriteString(project.MismatchParentVersion)
	} else if project.MissingParent != "" {
		buffer.WriteString(color.YellowString(" Warning: parent not found: "))
		buffer.WriteString(project.MissingParent)
	}
	if showpath {
		buffer.WriteString(color.CyanString(" " + project.FullPath))
	}
	buffer.WriteString("\n")
	sort.Sort(project.Children)
	for _, child := range project.Children {
		printProject(child, depth+1, buffer, noColor, showpath)
	}
}
Beispiel #7
0
func (obj *Decorator) replacePlaceholders(
	str string,
	placeholders map[string]interface{},
) string {
	r := regexp.MustCompile(":\\w+")
	for _, key := range r.FindAllString(str, -1) {
		name := strings.Replace(key, ":", "", -1)

		if value, ok := placeholders[name]; ok {
			switch reflect.TypeOf(value).String() {
			case "string":
				value = color.CyanString(fmt.Sprint(value))
				break
			case "int64":
				value = color.BlueString(fmt.Sprintf("%d", value))
			case "float64":
				if regexp.MustCompile("^[0-9]+(.[0]+)?").MatchString(fmt.Sprintf("%f", value)) {
					value = fmt.Sprintf("%d", int64(value.(float64)))
				} else {
					value = fmt.Sprintf("%f", value)
				}

				value = color.BlueString(value.(string))
				break
			default:
				value = fmt.Sprint(value)
			}

			str = strings.Replace(str, key, value.(string), -1)
		}
	}

	return str
}
Beispiel #8
0
func (this *Options) Echo() {
	output := color.CyanString("replacing:   ")
	output += color.GreenString("%s\n", this.From)
	output += color.CyanString("with:        ")
	output += color.GreenString("%s\n", this.To)
	output += color.CyanString("in files:    ")
	output += color.GreenString("%s\n", this.File)
	output += color.CyanString("starting in: ")
	output += color.GreenString("%s\n", this.Dir)

	if this.Ignore != "" {
		output += color.CyanString("ignoring:    ")
		output += color.GreenString("%s\n", this.Ignore)
	}

	fmt.Print(output)
}
Beispiel #9
0
func (c container) cacheHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		var cacheStatus string
		var urlComponents = []string{
			"http://",
			config.backend,
			r.URL.Path,
		}
		backendURL := strings.Join(urlComponents, "")

		body, _ := ioutil.ReadAll(r.Body)
		identifier := []byte(fmt.Sprintf("%s%s", body, r.URL.Path))
		sum := md5.Sum(identifier)
		hash := hex.EncodeToString(sum[:16])

		redisConn := c.pool.Get()
		defer redisConn.Close()

		repl, err := redisConn.Do("GET", hash)
		if err != nil {
			log.Error(err.Error())
			return
		}
		if repl == nil {
			log.Debug(fmt.Sprintf("%s %s", hash, color.YellowString("MISS")))
			w.Header().Set("X-postcache", "MISS")
			response, cacheError := c.updateCache(hash, string(body), backendURL, false)
			if cacheError != nil {
				log.Error(cacheError.Error())
			}
			w.Write([]byte(response))
		} else {
			ttlrepl, ttlerr := redisConn.Do("TTL", hash)
			if ttlerr != nil {
				log.Error("key is gone? maybe the TTL expired before we got here.")
			} else {
				if ttlrepl.(int64) < int64((config.expire - config.freshness)) {
					cacheStatus = color.YellowString("STALE")
					go c.updateCache(hash, string(body), backendURL, true)
				} else {
					cacheStatus = color.CyanString("HIT")
				}
			}
			log.Debug(fmt.Sprintf("%s %s ", hash, cacheStatus))
			w.Header().Set("X-postcache", cacheStatus)
			w.Write(repl.([]byte))
		}
	} else {
		w.Header().Set("X-postcache", "CANT-CACHE")
		proxy := &httputil.ReverseProxy{
			Director: func(req *http.Request) {
				req.URL.Scheme = "http"
				req.URL.Host = config.backend
			},
		}
		proxy.ServeHTTP(w, r)
	}
}
Beispiel #10
0
// PrettyID returns a terminal colored format of the connection ID.
func (c *Conn) PrettyID() string {
	var remote string
	if c.Peer != nil {
		remote = c.Peer.Id
	} else {
		remote = c.RemoteAddr().String()
	}
	return color.CyanString(remote)
}
Beispiel #11
0
func (e *LogoCmd) Exec(args []string) error {

	Server.Exec(client.StatusChannel, func(g *gocui.Gui, v *gocui.View, s *client.Server) error {
		fmt.Fprintln(v, color.CyanString(ui.Logo))
		fmt.Fprintln(v, color.GreenString(ui.VersionLine))
		return nil
	})

	return nil
}
Beispiel #12
0
func main() {
	manifestFile := kingpin.Flag("manifest", "Path to manifest.yml file.").Default("manifest.yml").String()
	plugin := kingpin.Arg("plugin", "Plugin name for run.").String()
	vars := *kingpin.Flag("var", "key=value pairs with manifest vars.").StringMap()
	dryRun := kingpin.Flag("dry-run", "Show manifest section only").Bool()
	noColor := kingpin.Flag("no-color", "Disable colored output").Bool()
	pluginData := kingpin.Flag("plugin-data", "Data for plugin").String()

	kingpin.Version(version)
	kingpin.Parse()

	var plugins []manifest.PluginData
	var err error
	var manifestData *manifest.Manifest

	color.NoColor = *noColor

	if *pluginData != "" {
		manifestData = manifest.LoadJSON(*pluginData)
	} else {
		manifestData = manifest.Load(*manifestFile, vars)
	}

	if *plugin == "" && *dryRun {
		fmt.Printf("%s\n%s\n%s\n",
			color.GreenString(">>> manifest:"),
			manifestData.String(),
			color.GreenString("<<< manifest: OK\n"))
		return
	}

	if *pluginData != "" {
		plugins = []manifest.PluginData{manifestData.GetPluginWithData(*plugin)}
	} else {
		plugins, err = manifestData.FindPlugins(*plugin)
	}

	if err != nil {
		log.Fatalln(color.RedString("Error find plugins for '%s': %v", *plugin, err))
	}

	for _, pair := range plugins {
		log.Printf("%s\n%s\n\n", color.GreenString(">>> %s:", pair.PluginName), color.CyanString("%s", pair.Data))

		if !*dryRun {
			if err := pair.Plugin.Run(pair.Data); err != nil {
				fmt.Println("")
				log.Fatalln(color.RedString("Error on run plugin `%s`: %v", pair.PluginName, err))
			} else {
				log.Println(color.GreenString("<<< %s: OK", pair.PluginName))
			}
		}
	}
}
Beispiel #13
0
//
// required for serach options
//
func getSearchOpt() *searchOption {

	seletor := func(min, max, defaultValue int8, hint string, optHints map[int8]string) int8 {
		for {
			fmt.Fprintf(color.Output, "%s:\n", color.GreenString(hint))
			for k := min; k <= max; k++ {
				if k == defaultValue {

					fmt.Fprintf(color.Output, "\t %s: %s (%s)\n", color.CyanString("%d", k), optHints[k], color.GreenString("DEFAULT"))
				} else {
					fmt.Fprintf(color.Output, "\t %s: %s\n", color.CyanString("%d", k), optHints[k])
				}
			}

			fmt.Fprintf(color.Output, "$ %s", color.CyanString("select: "))
			s := getInputString()
			if len(s) > 0 {
				selected, err := strconv.ParseInt(s, 16, 32)
				if err != nil || selected < int64(min) || selected > int64(max) {
					color.Red("Invalid selection\n")
					continue
				}
				return int8(selected)
			}
			break
		}
		return defaultValue
	}

	// now , let the user to choose
	filter := seletor(SearchBySubject, SearchByKeyword, SearchBySubject, "What's you input means?", searchFilterHints)
	database := seletor(SearchAllDoc, SearchConference, SearchAllDoc, "Which database you wanna query?", searchRangeHints)
	order := seletor(OrderByDownloadedTime, OrderBySubject, OrderByDownloadedTime, "How should I sort the result?", searchOrderHints)

	opt := &searchOption{
		filter:  searchFilterDefs[filter],
		databse: searchRangeDefs[database],
		order:   searchOrderDefs[order],
	}
	return opt
}
Beispiel #14
0
func (e *Entry) Row() []string {
	y, m, d := e.Time.Date()
	date := color.BlueString("%d/%d/%d", m, d, y)
	ip := color.RedString("%s", e.Ip)
	attempts := color.GreenString("%d", e.Attempts)
	user := color.YellowString("%s", e.User)
	auth := color.WhiteString("%s", e.AuthType)
	proto := color.CyanString("%s", e.Protocol)
	port := e.Port
	server := e.Server
	return []string{date, ip, attempts, user, auth, proto, port, server}
}
Beispiel #15
0
// Logo with color
func Logo() string {
	var logo string

	logo += "\n\n"
	logo += color.GreenString("  ██╗  ██╗ ██████╗ ███╗   ███╗ █████╗ ███╗   ██╗██████╗  █████╗\n")
	logo += color.MagentaString("  ██║ ██╔╝██╔═══██╗████╗ ████║██╔══██╗████╗  ██║██╔══██╗██╔══██╗\n")
	logo += color.YellowString("  █████╔╝ ██║   ██║██╔████╔██║███████║██╔██╗ ██║██║  ██║███████║\n")
	logo += color.CyanString("  ██╔═██╗ ██║   ██║██║╚██╔╝██║██╔══██║██║╚██╗██║██║  ██║██╔══██║\n")
	logo += color.BlueString("  ██║  ██╗╚██████╔╝██║ ╚═╝ ██║██║  ██║██║ ╚████║██████╔╝██║  ██║\n")
	logo += color.RedString("  ╚═╝  ╚═╝ ╚═════╝ ╚═╝     ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝╚═════╝ ╚═╝  ╚═╝")

	return logo
}
Beispiel #16
0
func showResult(result QueryResult, url, keyword string, page int) {
	commits := result.Commits
	if len(commits) == 0 {
		fmt.Println("No Results Found.")
		fmt.Printf("  url: %s\n\n", url)
		return
	}
	fmt.Printf("Search Result : %s : %d/%s pages\n",
		result.ResultCount,
		page,
		result.TotalPages,
	)
	fmt.Printf("  url: %s\n\n", url)

	repoWidth := maxRepoWidth(commits)
	repoFmt := fmt.Sprintf("%%-%ds", repoWidth)

	urlWidth := maxURLWidth(commits)
	urlFmt := fmt.Sprintf("%%-%ds", urlWidth)

	msgWidth := maxMessageWidth(commits)

	fmt.Fprintf(color.Output, " %s | %s | %s | message \n",
		color.BlueString(repoFmt, "Repository"),
		color.CyanString("%-7s", "sha1"),
		fmt.Sprintf(urlFmt, "url"),
	)
	fmt.Println(strings.Repeat("-", repoWidth+msgWidth+urlWidth+18))

	for _, c := range commits {
		fmt.Fprintf(color.Output, " %s | %7s | %s | %s\n",
			color.BlueString(repoFmt, c.Repo),
			color.CyanString(c.Sha1),
			fmt.Sprintf(urlFmt, c.CommitURL),
			highlightWords(c.Message, keyword),
		)
	}
}
Beispiel #17
0
//
// print a set of articles
//
func printArticles(page int, articles []Article) {
	fmt.Fprintf(color.Output, "\n-----------------------------------------------------------(%s)--\n", color.MagentaString("page:%d", page))
	for id, entry := range articles {
		source := entry.Information.SourceName
		if len(source) == 0 {
			source = "N/A"
		}
		fmt.Fprintf(color.Output, "%s: %s (%s)\n",
			color.CyanString("%02d", id+1),
			color.WhiteString(entry.Information.Title),
			color.YellowString("%s", source))
	}
	fmt.Fprintf(color.Output, "-----------------------------------------------------------(%s)--\n\n", color.MagentaString("page%d", page))
}
Beispiel #18
0
// PrintSearchResponse prints out a nice formatted list of responses
func (wd *WhatDownloader) PrintSearchResponse(r gozelle.TorrentSearchResponse) {
	for i, x := range r.Response.Results {
		// Print torrent group
		bold.Printf("\n%s - %s [%d] [%s]\n", x.Artist, html.UnescapeString(x.GroupName), x.GroupYear, x.ReleaseType)

		currentRemaster := ""
		for k, y := range x.Torrents {

			// Print release info
			if !y.Remastered && currentRemaster != "Original Release" {
				currentRemaster = "Original Release"
				fmt.Fprintf(color.Output, "%3s | %s / %s\n", "", color.CyanString(currentRemaster), color.CyanString(y.Media))
			} else if y.Remastered && currentRemaster != y.RemasterTitle {
				currentRemaster = y.RemasterTitle
				fmt.Fprintf(color.Output, "%3s | %s / %s / %s\n", "", color.CyanString(strconv.Itoa(y.RemasterYear)),
					color.CyanString(html.UnescapeString(y.RemasterTitle)), color.CyanString(y.Media))
			}

			// Print the torrent
			fmt.Printf("%3s | %-4s / %-8s | %6.2f MB | %d\n", fmt.Sprintf("%d.%d", i, k), y.Format, y.Encoding,
				float64(y.Size)/1024.0/1024.0, y.Seeders)
		}
	}
}
Beispiel #19
0
func newServer(port int, peers []string, diskAllocated int) (*server, error) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	s := &server{
		Logger: log.New(os.Stdout,
			color.CyanString(":%d ", port),
			log.Flags()),
		diskAllocated: diskAllocated,
		port:          port,
	}

	if err := s.init(); err != nil {
		return nil, err
	}
	go s.connectPeers(peers)
	return s, nil
}
Beispiel #20
0
func Main(port int, peers []string, diskAllocated int) {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	s := &server{
		Logger: log.New(os.Stdout,
			color.CyanString(":%d ", port),
			log.Flags()),
		diskAllocated: diskAllocated,
		port:          port,
	}

	if err := s.init(); err != nil {
		s.Fatal(err)
	}

	go s.connectPeers(peers)
	s.Fatal(s.network.Listen())
}
Beispiel #21
0
//ConsolePrint outputs the result of each audit
func ConsolePrint(res actuary.Result) {
	var status string
	bold := color.New(color.Bold).SprintFunc()
	if res.Status == "PASS" {
		status = color.GreenString("[PASS]")
	} else if res.Status == "WARN" {
		status = color.RedString("[WARN]")
	} else if res.Status == "SKIP" {
		status = color.YellowString("[SKIP]")
	} else {
		status = color.CyanString("[INFO]")
	}

	fmt.Printf("%s - %s \n", status, bold(res.Name))

	if res.Output != "" {
		fmt.Printf("\t %s\n\n", res.Output)
	}
}
Beispiel #22
0
func (e *ClearCmd) Exec(args []string) error {

	Server.Exec(Server.CurrentChannel, func(g *gocui.Gui, v *gocui.View, s *client.Server) error {
		v.Autoscroll = true
		v.Clear()
		v.SetCursor(0, 0)

		if Server.CurrentChannel == client.StatusChannel {
			fmt.Fprintln(v, "")
			fmt.Fprintln(v, color.CyanString(ui.Logo))
			fmt.Fprintln(v, color.RedString(ui.VersionLine))
		} else {
			fmt.Fprintln(v, "\n\n")
		}

		return nil
	})

	return nil
}
Beispiel #23
0
func makeDesc(desc []string, colors ...string) (str string) {
	var color string
	if len(colors) > 0 {
		color = colors[0]
	}

	for i, _ := range desc {
		switch color {
		case "green":
			str += fcolor.GreenString(desc[i]) + "\n"
		case "yellow":
			str += fcolor.YellowString(desc[i]) + "\n"
		case "cyan":
			str += fcolor.CyanString(desc[i]) + "\n"
		default:
			str += desc[i] + "\n"
		}
	}
	return
}
Beispiel #24
0
func (r *Reporter) writeReports(name string) {
	for _, err := range r.Errors {
		fmt.Printf("%s ERR: %s\n", name, err.Error())
	}
	for _, report := range r.Reports {
		out := color.Output

		stat := color.GreenString("PASS")
		if report.Warn {
			stat = color.RedString("FAIL")
		} else {
			if report.Result == "" {
				report.Result = "OK"
			}
		}
		fmt.Fprintf(out, "%s: %s \n   %s - %s\n", color.CyanString(name), report.Name, stat, report.Result)
	}
	for _, c := range r.Children {
		c.writeReports(name + "." + c.Name)
	}
}
Beispiel #25
0
func healthCheck() {
	fmt.Println("== health check")

	fleetAPI := os.Getenv("FH_FLEET_API")
	if fleetAPI == "" {
		fleetAPI = "127.0.0.1"
	}
	url := "http://" + fleetAPI + "/fleet/v1/state"

	response, err := http.Get(url)
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	} else {
		defer response.Body.Close()

		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			panic(err.Error())
		}

		var data FleetState
		json.Unmarshal(body, &data)

		for _, unit := range data.States {
			var unitState string
			if unit.SystemdSubState != "running" {
				unitDown := color.CyanString(unit.Name)
				if unit.SystemdSubState == "failed" {
					unitState = color.RedString(unit.SystemdSubState)
				} else {
					unitState = color.YellowString(unit.SystemdSubState)
				}

				fmt.Println(unitDown + " : " + unitState)
			}
		}
	}

}
Beispiel #26
0
func (obj *Decorator) colorizeLevel(level log.LogLevel) string {
	var str string

	switch level.Code {
	case log.DEBUG_CODE:
		str = color.BlackString(log.DEBUG_SHORT_STRING)
		break
	case log.INFO_CODE:
		str = color.BlueString(log.INFO_SHORT_STRING)
		break
	case log.NOTICE_CODE:
		str = color.CyanString(log.NOTICE_SHORT_STRING)
		break
	case log.WARNING_CODE:
		str = color.YellowString(log.WARNING_SHORT_STRING)
		break
	case log.ERROR_CODE:
		str = color.RedString(log.ERROR_SHORT_STRING)
		break
	case log.CRITICAL_CODE:
		s := color.New(color.FgWhite, color.BgRed).SprintFunc()
		str = fmt.Sprint(s(log.CRITICAL_SHORT_STRING))
		break
	case log.ALERT_CODE:
		s := color.New(color.FgWhite, color.BgRed).SprintFunc()
		str = fmt.Sprint(s(log.ALERT_SHORT_STRING))
		break
	case log.EMERGENCY_CODE:
		s := color.New(color.FgWhite, color.Bold, color.BgHiRed).SprintFunc()
		str = fmt.Sprint(s(log.EMERGENCY_SHORT_STRING))
		break
	default:
		str = string(level.Code)
	}

	return str
}
Beispiel #27
0
func (fs *fsSort) watch() error {
	if len(fs.dirs) == 0 {
		return errors.New("No directories to watch")
	}
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return fmt.Errorf("Failed to create file watcher: %s", err)
	}
	for dir, _ := range fs.dirs {
		if err := watcher.Add(dir); err != nil {
			return fmt.Errorf("Failed to watch directory: %s", err)
		}
		log.Printf("Watching %s for changes...", color.CyanString(dir))
	}
	select {
	case <-watcher.Events:
	case err := <-watcher.Errors:
		fs.verbf("watch error detected: %s", err)
	}
	go watcher.Close()
	log.Printf("Change detected, re-sorting in %s...", fs.WatchDelay)
	time.Sleep(fs.WatchDelay)
	return nil
}
Beispiel #28
0
func Report(cl component.Client, duration int) {
	object := `Psp.Module.Notification.Components.Adapter.PastabankAdapter`

	var request model.Request
	var logs []log.Log

	c := color.New(color.FgGreen, color.Bold)
	c.Println("Pastabank")

	// Received mails
	request = worker.Request(object, "Created mail with id :id", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" • %-30s %s", "Mail (Received / Processed):", color.CyanString(fmt.Sprint(cap(logs))))
	fmt.Println()

	// Processed mails
	request = worker.Request(object, "Found mail with id :id", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" • %-30s %s", "Processed:", color.CyanString(fmt.Sprint(cap(logs))))
	fmt.Println()

	// Files
	request = worker.Request(object, "Starts parsing attach :file", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" • %-30s %s", "Files (found / unknown):", color.CyanString(fmt.Sprint(cap(logs))))

	request = worker.Request(object, "Unknown file :file", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" / %s", color.BlackString(fmt.Sprint(cap(logs))))
	fmt.Println()

	// Processed chb
	request = worker.Request(object, "Processed :c1 and stored :c2 chargebacks", "", duration)
	logs = cl.FindLogs(request)

	var chargeback int
	for _, log := range logs {
		chargeback += int(log.Source["c1"].(float64))
	}

	fmt.Printf(" • %-30s %s", "Chargebacks (new / similar):", color.RedString(fmt.Sprint(chargeback)))

	// Similar chb
	request = worker.Request(object, "Found similar chargeback with id :id", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" / %s", color.BlackString(fmt.Sprint(cap(logs))))
	fmt.Println()

	// Processed alerts
	request = worker.Request(object, "Processed :c1 and stored :c2 alerts", "", duration)
	logs = cl.FindLogs(request)

	var alerts int
	for _, log := range logs {
		alerts += int(log.Source["c1"].(float64))
	}

	fmt.Printf(" • %-30s %s", "Alerts (new / similar):", color.RedString(fmt.Sprint(alerts)))

	// Similar alerts
	request = worker.Request(object, "Found similar alert with id :id", "", duration)
	logs = cl.FindLogs(request)

	fmt.Printf(" / %s", color.BlackString(fmt.Sprint(cap(logs))))
	fmt.Println()

	// Total
	request = worker.Request(object, "Send :count rows", ` NOT count: "0"`, duration)
	logs = cl.FindLogs(request)

	var sendCount int
	for _, log := range logs {
		sendCount += int(log.Source["count"].(float64))
	}

	fmt.Printf(" • %-30s %s", "Send:", color.CyanString(fmt.Sprint(sendCount)))
	fmt.Println()

	// Instances
	request = worker.Request(object, "Start pasta bank adapter", "", 1)
	logs = cl.FindLogs(request)

	instances := map[string]string{}

	for _, log := range logs {
		if _, ok := instances[log.ScriptId]; !ok {
			instances[log.ScriptId] = ""
		}
	}

	fmt.Printf(" • %-30s %s", "Instances (last h):", color.CyanString(fmt.Sprint(len(instances))))
	fmt.Println()
}
Beispiel #29
0
func LogInfo(format string, a ...interface{}) {
	log.Println("[INFO]", color.CyanString(format, a...))
}
Beispiel #30
0
//
// lord commander
//
func main() {
	color.Cyan("***************************************************************************\n")
	color.Cyan("****  Welcome use CNKI-Downloader, Let's f**k these knowledge mongers  ****\n")
	color.Cyan("****                            Good luck.                             ****\n")
	color.Cyan("***************************************************************************\n")

	defer func() {
		color.Yellow("** Bye.\n")
	}()

	//
	// note
	//
	fmt.Println()
	fmt.Println("** NOTE: if you cannot download any document, maybe the service of")
	fmt.Println("**       CNKI is unavailable again, in this situation, nothing ")
	fmt.Println("**       we can do but wait, please do not open a issue on GitHub, thanks")
	fmt.Println("**")

	//
	// update
	//
	v := update()
	if !v {
		return
	}

	//
	// login
	//
	downloader := &CNKIDownloader{
		username:    "******",
		password:    "******",
		http_client: &http.Client{},
	}

	fmt.Printf("** Login...")
	err := downloader.Auth()
	if err != nil {
		fmt.Fprintf(color.Output, "%s : %s \n", color.RedString("Failure"), err.Error())
		return
	} else {
		fmt.Fprintf(color.Output, "%s\n\n", color.GreenString("Success"))
	}

	for {

		fmt.Fprintf(color.Output, "$ %s", color.CyanString("input anything you wanna search: "))

		s := getInputString()
		if len(s) == 0 {
			continue
		}

		//
		// search first page
		//
		opt := getSearchOpt()

		result, err := downloader.SearchFirst(s, opt)
		if err != nil {
			fmt.Fprintf(color.Output, "Search %s %s (%s)\n", "zzr", color.RedString("Failure"), err.Error())
			continue
		}
		printArticles(1, result.GetPageData())

		//
		// tips
		//
		fmt.Fprintf(color.Output, "We got (%s) entries in total. (if u don't know how to do next, just type '%s') \n",
			color.GreenString("%d", result.GetRecordInfo()), color.RedString("help"))

		for {
			out := false

			ctx, err := downloader.CurrentPage()
			if err != nil {
				break
			}

			psize, pindex, pcount := ctx.GetPageInfo()
			fmt.Fprintf(color.Output, "$ [%d/%d] %s", pindex, pcount, color.CyanString("command: "))

			s = getInputString()
			cmd_parts := strings.Split(s, " ")
			switch strings.ToLower(cmd_parts[0]) {
			case "help":
				{
					fmt.Fprintf(color.Output, "Support follow commands:\n")
					fmt.Fprintf(color.Output, "\t %s: show page's information\n", color.YellowString("INFO"))
					fmt.Fprintf(color.Output, "\t %s: turn to next page\n", color.YellowString("NEXT"))
					fmt.Fprintf(color.Output, "\t %s: turn to previous page\n", color.YellowString("PREV"))
					fmt.Fprintf(color.Output, "\t  %s: (GET ID), download the specified item in this page, eg: GET 1, GET 14...etc\n", color.YellowString("GET"))
					fmt.Fprintf(color.Output, "\t %s: (SHOW ID), show the information about specified item, eg: SHOW 2, SHOW 9...etc\n", color.YellowString("SHOW"))
					fmt.Fprintf(color.Output, "\t%s: break out, and search the other papers\n", color.YellowString("BREAK"))
				}
			case "info":
				{
					color.White("  page size: %d\n page index: %d\ntotal pages: %d\n", psize, pindex, pcount)
				}
			case "next":
				{
					next_page, err := downloader.SearchNext(pindex + 1)
					if err != nil {
						fmt.Fprintf(color.Output, "Next page is invalid (%s)\n", color.RedString(err.Error()))
					} else {
						_, index, _ := next_page.GetPageInfo()
						printArticles(index, next_page.GetPageData())
					}
				}
			case "prev":
				{
					prev_page, err := downloader.SearchPrev()
					if err != nil {
						color.Red("Previous page is invalid")
					} else {
						_, index, _ := prev_page.GetPageInfo()
						printArticles(index, prev_page.GetPageData())
					}
				}
			case "show":
				{

					if len(cmd_parts) < 2 {
						color.Red("Invalid input")
						break
					}

					id, err := strconv.ParseInt(cmd_parts[1], 10, 32)
					if err != nil {
						fmt.Fprintf(color.Output, "Invalid input %s\n", color.RedString(err.Error()))
						break
					}
					id--

					entries := ctx.GetPageData()
					entry := entries[id]

					fmt.Println()
					fmt.Fprintf(color.Output, "*       PAGE: %s\n", color.WhiteString("%d", pindex))
					fmt.Fprintf(color.Output, "*         ID: %s\n", color.WhiteString("%d", id+1))
					fmt.Fprintf(color.Output, "*      Title: %s\n", color.WhiteString(entry.Information.Title))
					fmt.Fprintf(color.Output, "*    Created: %s\n", color.WhiteString(entry.Information.CreateTime))
					fmt.Fprintf(color.Output, "*    Authors: %s\n", color.GreenString(strings.Join(entry.Information.Creator, " ")))
					fmt.Fprintf(color.Output, "*     Source: %s\n", color.GreenString("%s(%s)", entry.Information.SourceName, entry.Information.SourceAlias))
					fmt.Fprintf(color.Output, "*       Code: %s\n", color.WhiteString("%s.%s", entry.Information.ClassifyName, entry.Information.ClassifyCode))
					fmt.Fprintf(color.Output, "*  Reference: %s\n", color.RedString("%d", entry.Information.RefCount))
					fmt.Fprintf(color.Output, "* Downloaded: %s\n", color.WhiteString("%d", entry.Information.DownloadCount))
					fmt.Fprintf(color.Output, "*Description: \n")

					//text := mahonia.NewDecoder("gbk").ConvertString(entry.Information.Description)
					textSeq := []rune(entry.Information.Description)
					for j := 0; j < len(textSeq); {
						end := j + 40
						if len(textSeq)-j < 40 {
							end = len(textSeq) - 1
						}
						fmt.Printf("*             %s\n", string(textSeq[j:end]))
						j = end + 1
					}
					fmt.Println()

				}
			case "get":
				{
					if len(cmd_parts) < 2 {
						color.Red("Invalid input")
						break
					}

					id, err := strconv.ParseInt(cmd_parts[1], 10, 32)
					if err != nil {
						fmt.Fprintf(color.Output, "Invalid input %s\n", color.RedString(err.Error()))
						break
					}
					id--

					entries := ctx.GetPageData()

					color.White("Downloading... %s\n", entries[id].Information.Title)
					path, err := downloader.Download(&entries[id])
					if err != nil {
						fmt.Fprintf(color.Output, "Download failed %s\n", color.RedString(err.Error()))
						break
					}

					fmt.Fprintf(color.Output, "Download success (%s) \n", color.GreenString(path))
				}
			case "break":
				{
					downloader.SearchStop()
					color.YellowString("Break out.\n")
					out = true
				}
			}

			if out {
				break
			}
		}
	}

	return
}