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} }
// // 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)) }
func (f *Fissile) listPropertiesForHuman() { // Human readable output. for _, release := range f.releases { f.UI.Println(color.GreenString("Dev release %s (%s)", color.YellowString(release.Name), color.MagentaString(release.Version))) for _, job := range release.Jobs { f.UI.Printf("%s (%s): %s\n", color.YellowString(job.Name), color.WhiteString(job.Version), job.Description) for _, property := range job.Properties { f.UI.Printf("\t%s: %v\n", color.YellowString(property.Name), property.Default) } } } }
// ListJobs will list all jobs within a list of dev releases func (f *Fissile) ListJobs() error { if len(f.releases) == 0 { return fmt.Errorf("Releases not loaded") } for _, release := range f.releases { f.UI.Println(color.GreenString("Dev release %s (%s)", color.YellowString(release.Name), color.MagentaString(release.Version))) for _, job := range release.Jobs { f.UI.Printf("%s (%s): %s\n", color.YellowString(job.Name), color.WhiteString(job.Version), job.Description) } f.UI.Printf( "There are %s jobs present.\n\n", color.GreenString("%d", len(release.Jobs)), ) } return nil }
// ListPackages will list all BOSH packages within a list of dev releases func (f *Fissile) ListPackages() error { if len(f.releases) == 0 { return fmt.Errorf("Releases not loaded") } for _, release := range f.releases { f.UI.Println(color.GreenString("Dev release %s (%s)", color.YellowString(release.Name), color.MagentaString(release.Version))) for _, pkg := range release.Packages { f.UI.Printf("%s (%s)\n", color.YellowString(pkg.Name), color.WhiteString(pkg.Version)) } f.UI.Printf( "There are %s packages present.\n\n", color.GreenString("%d", len(release.Packages)), ) } return nil }
// // 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 }
// // try to update // func update() (allowContinue bool) { allowContinue = true fmt.Println("** Checking update information current: ", VersionString) // // http query the information // info, err := getUpdateInfo() if err == nil { newVersion := false if info.Major > MajorVersion || info.Minor > MinorVersion { newVersion = true } if newVersion { // // show version information // verName := "" if info.IsAlpha { verName = fmt.Sprintf("%d.%d-alpha", info.Major, info.Minor) } else { verName = fmt.Sprintf("%d.%d-release", info.Major, info.Minor) } fmt.Fprintf(color.Output, "\t* version: %s\n", color.GreenString(verName)) fmt.Fprintf(color.Output, "\t* time: %s\n", color.WhiteString(info.ReleaseTime)) if len(info.Reasons) > 0 { for i, v := range info.Reasons { if i == 0 { fmt.Fprintf(color.Output, "\t* update: - %s\n", color.WhiteString(v)) } else { fmt.Fprintf(color.Output, "\t* - %s\n", color.WhiteString(v)) } } } else { fmt.Fprintf(color.Output, "\t* update: unknown\n") } if info.IsRequired { fmt.Fprintf(color.Output, "** You %s update this version, or you cannot continue to use current program\n", color.RedString("have to")) } else { fmt.Println("** This version is not nacessary to be update, but I recommand you to update now") } fmt.Printf("** update now? [y/n]: ") s := getInputString() if strings.ToLower(s) != "y" { // // choose to skip // allowContinue = !info.IsRequired } else { // // choose to update // switch runtime.GOOS { case "windows": { // rundll32 url.dll,FileProtocolHandler target-url runDll32 := filepath.Join(os.Getenv("SYSTEMROOT"), "System32", "rundll32.exe") cmd := exec.Command(runDll32, "url.dll,FileProtocolHandler", info.Url.Windows) cmd.Start() } case "darwin": { cmd := exec.Command("open", info.Url.Mac) cmd.Run() } case "linux": { //fmt.Fprintf(color.Output, "** url: %s \n", color.RedString(info.Url.Linux)) } } fmt.Println("** if your browser couldn't be opened, please visit the project page to download:") fmt.Fprintf(color.Output, "** --> %s \n", color.GreenString(FixedDownloadViewUrl)) allowContinue = false } } else { fmt.Println("** already is the last version") } } else { fmt.Fprintf(color.Output, "** Check %s : %s \n", color.RedString("failure"), err.Error()) } return }
// ColoredBuildStringFunc returns a formatting function for colorizing strings. func ColoredBuildStringFunc(buildName string) StringFormatter { return func(s string) string { return color.GreenString("build-%s > %s", color.MagentaString(buildName), color.WhiteString("%s", s)) } }
package lion import ( "bytes" "time" "unicode" "github.com/fatih/color" ) var ( levelToColorString = map[Level]string{ LevelNone: color.BlueString(LevelNone.String()), LevelDebug: color.WhiteString(LevelDebug.String()), LevelInfo: color.BlueString(LevelInfo.String()), LevelWarn: color.YellowString(LevelWarn.String()), LevelError: color.RedString(LevelError.String()), LevelFatal: color.RedString(LevelFatal.String()), LevelPanic: color.RedString(LevelPanic.String()), } // TODO(pedge): clean up fourLetterLevels = map[Level]bool{ LevelNone: true, LevelInfo: true, LevelWarn: true, } ) type textMarshaller struct { disableTime bool disableLevel bool
func (c *Compilator) compilePackage(pkg *model.Package) (err error) { // Prepare input dir (package plus deps) if err := c.createCompilationDirStructure(pkg); err != nil { return err } if err := c.copyDependencies(pkg); err != nil { return err } // Generate a compilation script targetScriptName := "compile.sh" hostScriptPath := filepath.Join(pkg.GetTargetPackageSourcesDir(c.hostWorkDir), targetScriptName) containerScriptPath := filepath.Join(docker.ContainerInPath, targetScriptName) if err := compilation.SaveScript(c.baseType, compilation.CompilationScript, hostScriptPath); err != nil { return err } // Extract package extractDir := c.getSourcePackageDir(pkg) if _, err := pkg.Extract(extractDir); err != nil { return err } // Run compilation in container containerName := c.getPackageContainerName(pkg) // in-memory buffer of the log log := new(bytes.Buffer) stdoutWriter := docker.NewFormattingWriter( log, func(line string) string { return color.GreenString("compilation-%s > %s", color.MagentaString("%s", pkg.Name), color.WhiteString("%s", line)) }, ) stderrWriter := docker.NewFormattingWriter( log, func(line string) string { return color.GreenString("compilation-%s > %s", color.MagentaString("%s", pkg.Name), color.RedString("%s", line)) }, ) sourceMountName := fmt.Sprintf("source_mount-%s", uuid.New()) mounts := map[string]string{ pkg.GetTargetPackageSourcesDir(c.hostWorkDir): docker.ContainerInPath, pkg.GetPackageCompiledTempDir(c.hostWorkDir): docker.ContainerOutPath, // Add the volume mount to work around AUFS issues. We will clean // the volume up (as long as we're not trying to keep the container // around for debugging). We don't give it an actual directory to mount // from, so it will be in some docker-maintained storage. sourceMountName: ContainerSourceDir, } exitCode, container, err := c.dockerManager.RunInContainer(docker.RunInContainerOpts{ ContainerName: containerName, ImageName: c.BaseImageName(), Cmd: []string{"bash", containerScriptPath, pkg.Name, pkg.Version}, Mounts: mounts, Volumes: map[string]map[string]string{sourceMountName: nil}, KeepContainer: c.keepContainer, StdoutWriter: stdoutWriter, StderrWriter: stderrWriter, }) if container != nil && (!c.keepContainer || err == nil || exitCode == 0) { // Attention. While the assignments to 'err' in the // deferal below take effect after the 'return' // statements coming later they are visible to the // caller, i.e. override the 'return'ed value, // because 'err' is a __named__ return parameter. defer func() { // Remove container - dockerManager.RemoveContainer does a force-rm if removeErr := c.dockerManager.RemoveContainer(container.ID); removeErr != nil { if err == nil { err = removeErr } else { err = fmt.Errorf("Error compiling package: %s. Error removing package: %s", err.Error(), removeErr.Error()) } } if removeErr := c.dockerManager.RemoveVolumes(container); removeErr != nil { if err == nil { err = removeErr } else { err = fmt.Errorf("%s: Error removing volumes for package %s: %s", err, pkg.Name, removeErr) } } }() } if err != nil { log.WriteTo(c.ui) return fmt.Errorf("Error compiling package %s: %s", pkg.Name, err.Error()) } if exitCode != 0 { log.WriteTo(c.ui) return fmt.Errorf("Error - compilation for package %s exited with code %d", pkg.Name, exitCode) } return os.Rename( pkg.GetPackageCompiledTempDir(c.hostWorkDir), pkg.GetPackageCompiledDir(c.hostWorkDir)) }
// CreateCompilationBase will create the compiler container func (c *Compilator) CreateCompilationBase(baseImageName string) (image *dockerClient.Image, err error) { imageTag := c.baseCompilationImageTag() imageName := c.BaseImageName() c.ui.Println(color.GreenString("Using %s as a compilation image name", color.YellowString(imageName))) containerName := c.baseCompilationContainerName() c.ui.Println(color.GreenString("Using %s as a compilation container name", color.YellowString(containerName))) image, err = c.dockerManager.FindImage(imageName) if err != nil { c.ui.Println("Image doesn't exist, it will be created ...") } else { c.ui.Println(color.GreenString( "Compilation image %s with ID %s already exists. Doing nothing.", color.YellowString(imageName), color.YellowString(image.ID), )) return image, nil } tempScriptDir, err := util.TempDir("", "fissile-compilation") if err != nil { return nil, fmt.Errorf("Could not create temp dir %s: %s", tempScriptDir, err.Error()) } defer os.RemoveAll(tempScriptDir) targetScriptName := "compilation-prerequisites.sh" containerScriptPath := filepath.Join(docker.ContainerInPath, targetScriptName) hostScriptPath := filepath.Join(tempScriptDir, targetScriptName) if err = compilation.SaveScript(c.baseType, compilation.PrerequisitesScript, hostScriptPath); err != nil { return nil, fmt.Errorf("Error saving script asset: %s", err.Error()) } // in-memory buffer of the log log := new(bytes.Buffer) stdoutWriter := docker.NewFormattingWriter( log, func(line string) string { return color.GreenString("compilation-container > %s", color.WhiteString("%s", line)) }, ) stderrWriter := docker.NewFormattingWriter( log, func(line string) string { return color.GreenString("compilation-container > %s", color.RedString("%s", line)) }, ) exitCode, container, err := c.dockerManager.RunInContainer(docker.RunInContainerOpts{ ContainerName: containerName, ImageName: baseImageName, Cmd: []string{"bash", "-c", containerScriptPath}, Mounts: map[string]string{tempScriptDir: docker.ContainerInPath}, KeepContainer: false, // There is never a need to keep this container on failure StdoutWriter: stdoutWriter, StderrWriter: stderrWriter, }) if container != nil { defer func() { removeErr := c.dockerManager.RemoveContainer(container.ID) if removeErr != nil { if err == nil { err = removeErr } else { err = fmt.Errorf( "Image creation error: %s. Image removal error: %s", err, removeErr, ) } } }() } if err != nil { log.WriteTo(c.ui) return nil, fmt.Errorf("Error running script: %s", err.Error()) } if exitCode != 0 { log.WriteTo(c.ui) return nil, fmt.Errorf("Error - script script exited with code %d", exitCode) } image, err = c.dockerManager.CreateImage( container.ID, c.baseCompilationImageRepository(), imageTag, "", []string{}, ) if err != nil { return nil, fmt.Errorf("Error creating image %s", err.Error()) } c.ui.Println(color.GreenString( "Image %s with ID %s created successfully.", color.YellowString(c.BaseImageName()), color.YellowString(image.ID))) return image, nil }
func (o *Decorator) DecorateDetails(entity log.Log) { fmt.Println() c := color.New(color.FgGreen, color.Bold) c.Println("Response") fmt.Printf(" • Id: %s", color.GreenString(entity.Id)) fmt.Println() level := o.colorizeLevel(entity.Level) fmt.Printf(" • Level: %s", level) fmt.Println() date := entity.Time.Format(time.Stamp) fmt.Printf(" • Time: %s", color.WhiteString(date)) fmt.Println() fmt.Printf(" • Host: %s", color.WhiteString(entity.Host)) fmt.Println() if len(entity.SessionId) > 0 { fmt.Printf(" • Session id: %s", color.GreenString(entity.SessionId)) fmt.Println() } message := o.replacePlaceholders(entity.Message, entity.Source) fmt.Printf(" • Message: %s", color.CyanString(message)) fmt.Println() fmt.Printf(" • Script id: %s", color.YellowString(entity.ScriptId)) fmt.Println() fmt.Printf(" • Object: %s", (entity.Object)) fmt.Println() fmt.Println(" • Source:") for key, value := range entity.Source { if value == nil || key == "log-level" || key == "message" || key == "script-id" || key == "@version" || key == "@timestamp" || key == "object" || key == "type" || key == "host" { continue } 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) } fmt.Printf(" • %s: %s", color.MagentaString(key), value) fmt.Println() } if entity.Exception != nil && entity.Exception.Code != 0 { fmt.Println(" • Exception:") style1 := color.New(color.FgWhite, color.BgBlack) style1.Printf(" • Message: %s", entity.Exception.Message) style1.Println() style1.Printf(" • Code: %d", entity.Exception.Code) style1.Println() for _, trace := range entity.Exception.Trace { style1.Printf(" • File: %s:%d", trace.File, trace.Line) style1.Println() } } fmt.Println() }