Пример #1
0
func createListFileContent(filePaths []string, includeHeader bool) string {
	output := ""
	header := ""

	if includeHeader {
		// NOTE: kr/text.Wrap returns lines separated by \n for all platforms.
		// So here hard-code \n too. Later it will be changed to \r\n for Windows.
		header = text.Wrap("Please change the filenames that need to be renamed and save the file. Lines that are not changed will be ignored (no file will be renamed), so will empty lines.", LINE_LENGTH-3)
		header += "\n"
		header += "\n" + text.Wrap("You may delete a file by putting \"//\" at the beginning of the line. Note that this operation cannot be undone (though the file can be recovered from the trash on Windows and OSX).", LINE_LENGTH-3)
		header += "\n"
		header += "\n" + text.Wrap("Please do not swap the order of lines as this is what is used to match the original filenames to the new ones. Also do not delete lines as the rename operation will be cancelled due to a mismatch between the number of filenames before and after saving the file. You may test the effect of the rename operation using the --dry-run parameter.", LINE_LENGTH-3)
		header += "\n"
		header += "\n" + text.Wrap("Caveats: "+APPNAME+" expects filenames to be reasonably sane. Filenames that include newlines or non-printable characters for example will probably not work.", LINE_LENGTH-3)

		headerLines := strings.Split(header, "\n")
		temp := ""
		for _, line := range headerLines {
			if temp != "" {
				temp += newline()
			}
			temp += "// " + line
		}
		header = temp + newline() + newline()
	}

	for _, filePath := range filePaths {
		output += filepath.Base(filePath) + newline()
	}

	return header + output
}
Пример #2
0
// wrapDescription wraps the text in a cliflags.FlagInfo.Description.
func wrapDescription(s string) string {
	var result bytes.Buffer

	// split returns the parts of the string before and after the first occurrence
	// of the tag.
	split := func(str, tag string) (before, after string) {
		pieces := strings.SplitN(str, tag, 2)
		switch len(pieces) {
		case 0:
			return "", ""
		case 1:
			return pieces[0], ""
		default:
			return pieces[0], pieces[1]
		}
	}

	for len(s) > 0 {
		var toWrap, dontWrap string
		// Wrap everything up to the next stop wrap tag.
		toWrap, s = split(s, "<PRE>")
		result.WriteString(text.Wrap(toWrap, wrapWidth))
		// Copy everything up to the next start wrap tag.
		dontWrap, s = split(s, "</PRE>")
		result.WriteString(dontWrap)
	}
	return result.String()
}
Пример #3
0
func drawnode(f *os.File, r *node, prefix []int) {
	r.depth = len(prefix)
	s := prefToString(prefix)

	kvs := map[string]string{}

	kvs["label"] = fmt.Sprintf("\"%s\"", text.Wrap(r.label, 20))
	if r.colour != "" {
		kvs["style"] = "filled"
		kvs["color"] = r.colour
	}
	var strs []string
	for k, v := range kvs {
		strs = append(strs, k+"="+v)
	}
	fmt.Fprintf(f, "    %s [%s]\n", s, strings.Join(strs, ","))
	if len(r.children) > 0 {
		lastAnd := ""
		for i, c := range r.children {
			newp := append(prefix, i+1)
			if lastAnd != "" {
				fmt.Fprintf(f, "    %s -> %s [label=\"and\",constraint=false]\n", lastAnd, prefToString(newp))
				lastAnd = ""
			}
			if c.refine == "AND" {
				lastAnd = prefToString(newp)
			}
			drawnode(f, c, newp)
			fmt.Fprintf(f, "    %s -> %s\n", s, prefToString(newp))
		}
	} else {
	}
}
Пример #4
0
// this isn't real efficient, but that's not a problem here
func wrap(s string, indent int) string {
	if indent > 3 {
		indent = 3
	}

	wrapped := text.Wrap(s, maxLine)
	lines := strings.SplitN(wrapped, "\n", 2)
	if len(lines) == 1 {
		return lines[0]
	}

	if (maxLine - indentLen(indent)) <= 0 {
		panic("too much indentation")
	}

	rest := strings.Join(lines[1:], " ")
	wrapped = text.Wrap(rest, maxLine-indentLen(indent))
	return lines[0] + "\n" + text.Indent(wrapped, makeIndent(indent))
}
Пример #5
0
func outputErrors(errors []compilercommon.SourceError) {
	sort.Sort(ErrorsSlice(errors))

	highlight := color.New(color.FgRed, color.Bold)
	location := color.New(color.FgWhite)
	message := color.New(color.FgHiWhite)

	for _, err := range errors {
		highlight.Print("ERROR: ")
		location.Printf("At %v:%v:%v:\n", err.SourceAndLocation().Source(), err.SourceAndLocation().Location().LineNumber()+1, err.SourceAndLocation().Location().ColumnPosition()+1)
		message.Printf("%s\n\n", text.Indent(text.Wrap(err.Error(), 80), "  "))
	}
}
Пример #6
0
func outputWarnings(warnings []compilercommon.SourceWarning) {
	sort.Sort(WarningsSlice(warnings))

	highlight := color.New(color.FgYellow, color.Bold)
	location := color.New(color.FgWhite)
	message := color.New(color.FgHiWhite)

	for _, warning := range warnings {
		highlight.Print("WARNING: ")
		location.Printf("At %v:%v:%v:\n", warning.SourceAndLocation().Source(), warning.SourceAndLocation().Location().LineNumber()+1, warning.SourceAndLocation().Location().ColumnPosition()+1)
		message.Printf("%s\n\n", text.Indent(text.Wrap(warning.String(), 80), "  "))
	}
}
Пример #7
0
func main() {
	minLogLevel_ = 1

	// -----------------------------------------------------------------------------------
	// Handle SIGINT (Ctrl + C)
	// -----------------------------------------------------------------------------------

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt, os.Kill)
	go func() {
		<-signalChan
		logInfo("Operation has been aborted.")
		onExit()
		os.Exit(2)
	}()

	defer onExit()

	// -----------------------------------------------------------------------------------
	// Parse arguments
	// -----------------------------------------------------------------------------------

	var opts CommandLineOptions
	flagParser_ = flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
	args, err := flagParser_.Parse()
	if err != nil {
		t := err.(*flags.Error).Type
		if t == flags.ErrHelp {
			printHelp()
			return
		} else {
			criticalError(err)
		}
	}

	if opts.Verbose {
		minLogLevel_ = 0
	}

	err = profileOpen()
	if err != nil {
		logError(fmt.Sprintf("%s", err))
	}

	// -----------------------------------------------------------------------------------
	// Handle selected command
	// -----------------------------------------------------------------------------------

	var commandName string
	if opts.Config {
		commandName = "config"
	} else if opts.Undo {
		commandName = "undo"
	} else if opts.Version {
		commandName = "version"
	} else {
		commandName = "rename"
	}

	var commandErr error
	switch commandName {
	case "config":
		commandErr = handleConfigCommand(&opts, args)
	case "undo":
		commandErr = handleUndoCommand(&opts, args)
	case "version":
		commandErr = handleVersionCommand(&opts, args)
	}

	if commandErr != nil {
		criticalError(commandErr)
	}

	if commandName != "rename" {
		return
	}

	filePaths, err := filePathsFromArgs(args)

	if err != nil {
		criticalError(err)
	}

	if len(filePaths) == 0 {
		criticalError(errors.New("no file to rename"))
	}

	// -----------------------------------------------------------------------------------
	// Build file list
	// -----------------------------------------------------------------------------------

	listFileContent := ""
	baseFilename := ""

	// NOTE: kr/text.Wrap returns lines separated by \n for all platforms.
	// So here hard-code \n too. Later it will be changed to \r\n for Windows.
	header := text.Wrap("Please change the filenames that need to be renamed and save the file. Lines that are not changed will be ignored (no file will be renamed), so will empty lines.", LINE_LENGTH-3)
	header += "\n"
	header += "\n" + text.Wrap("You may delete a file by putting \"//\" at the beginning of the line. Note that this operation cannot be undone (though the file can be recovered from the trash on Windows and OSX).", LINE_LENGTH-3)
	header += "\n"
	header += "\n" + text.Wrap("Please do not swap the order of lines as this is what is used to match the original filenames to the new ones. Also do not delete lines as the rename operation will be cancelled due to a mismatch between the number of filenames before and after saving the file. You may test the effect of the rename operation using the --dry-run parameter.", LINE_LENGTH-3)
	header += "\n"
	header += "\n" + text.Wrap("Caveats: "+APPNAME+" expects filenames to be reasonably sane. Filenames that include newlines or non-printable characters for example will probably not work.", LINE_LENGTH-3)

	headerLines := strings.Split(header, "\n")
	temp := ""
	for _, line := range headerLines {
		if temp != "" {
			temp += newline()
		}
		temp += "// " + line
	}
	header = temp

	for _, filePath := range filePaths {
		if listFileContent != "" {
			listFileContent += newline()
		}
		listFileContent += filepath.Base(filePath)
		baseFilename += filePath + "|"
	}

	baseFilename = stringHash(baseFilename)
	listFilePath := filepath.Join(tempFolder(), baseFilename+".files.txt")

	listFileContent = header + newline() + newline() + listFileContent
	ioutil.WriteFile(listFilePath, []byte(listFileContent), PROFILE_PERM)

	// -----------------------------------------------------------------------------------
	// Watch for changes in file list
	// -----------------------------------------------------------------------------------

	waitForFileChange := make(chan bool)
	waitForCommand := make(chan bool)

	go func(doneChan chan bool) {
		defer func() {
			doneChan <- true
		}()

		logInfo("Waiting for file list to be saved... (Press Ctrl + C to abort)")
		err := watchFile(listFilePath)
		if err != nil {
			criticalError(err)
		}
	}(waitForFileChange)

	// -----------------------------------------------------------------------------------
	// Launch text editor
	// -----------------------------------------------------------------------------------

	go func(doneChan chan bool) {
		defer func() {
			doneChan <- true
		}()

		err := editFile(listFilePath)
		if err != nil {
			criticalError(err)
		}
	}(waitForCommand)

	<-waitForCommand
	<-waitForFileChange

	// -----------------------------------------------------------------------------------
	// Check that the filenames have not been changed while the list was being edited
	// -----------------------------------------------------------------------------------

	for _, filePath := range filePaths {
		if _, err := os.Stat(filePath); os.IsNotExist(err) {
			criticalError(errors.New("Filenames have been changed or some files have been deleted or moved while the list was being edited. To avoid any data loss, the operation has been aborted. You may resume it by running the same command."))
		}
	}

	// -----------------------------------------------------------------------------------
	// Get new filenames from list file
	// -----------------------------------------------------------------------------------

	changedContent, err := ioutil.ReadFile(listFilePath)
	if err != nil {
		criticalError(err)
	}

	actions, err := fileActions(filePaths, string(changedContent))
	if err != nil {
		criticalError(err)
	}

	// -----------------------------------------------------------------------------------
	// Process the files
	// -----------------------------------------------------------------------------------

	err = processFileActions(actions, opts.DryRun)
	if err != nil {
		criticalError(err)
	}
}
Пример #8
0
func wrapText(s string) string {
	return text.Wrap(s, wrapWidth)
}
Пример #9
0
are preserved across updating *if and only if* the primary keys provided by
IMDb don't change. Unfortunately, IMDb primary keys can change (for example,
by adding a title to an episode). This results in stale rows in the 'atom' and
'name' tables (but will be hidden from search results).
`,
	flags: flag.NewFlagSet("load", flag.ExitOnError),
	run:   cmd_load,
	addFlags: func(c *command) {
		c.flags.StringVar(&flagLoadDownload, "download", flagLoadDownload,
			"When set, the data retrieved will be stored in the directory\n"+
				"specified. Then goim will quit.")
		c.flags.BoolVar(&flagLoadUrls, "urls", flagLoadUrls,
			"When set, the URLs for downloading the lists specified will\n"+
				"be printed to stdout, each on their own line. Then goim\n"+
				"will quit.")
		lists := text.Wrap(strings.Join(loadLists, ", "), 80)
		c.flags.StringVar(&flagLoadLists, "lists", flagLoadLists,
			"Set to a comma separated list of IMDB movie lists to load, with\n"+
				"no whitespace. Only lists named here will be loaded. If not\n"+
				"specified, then only the 'movie' list is loaded.\n"+
				"Use 'all' to load all lists or 'attr' to load all attribute\n"+
				"lists (e.g., quotes, running times, etc.).\n"+
				"Available lists: "+lists)
		c.flags.BoolVar(&flagWarnings, "warn", flagWarnings,
			"When set, warnings messages about the data will be shown.\n"+
				"When enabled, this can produce a lot of output saying that\n"+
				"an identifier could not be found for some entries. This is\n"+
				"(likely) a result of inconsistent data in IMDb's text files.")
	},
}
Пример #10
0
func wrappedIndent(s string, indentS string) string {
	return text.Indent(text.Wrap(s, 80-len(indentS)), indentS)
}
Пример #11
0
func wrap(s string) string {
	return text.Wrap(s, 80)
}
Пример #12
0
func wrap(limit int, s interface{}) string {
	return text.Wrap(sf("%s", s), limit)
}
Пример #13
0
func AnalyzeLocalImage(imageName string, minSeverity types.Priority, endpoint, myAddress, tmpPath string) error {
	// Save image.
	log.Printf("Saving %s to local disk (this may take some time)", imageName)
	err := save(imageName, tmpPath)
	if err != nil {
		return fmt.Errorf("Could not save image: %s", err)
	}

	// Retrieve history.
	log.Println("Retrieving image history")
	layerIDs, err := historyFromManifest(tmpPath)
	if err != nil {
		layerIDs, err = historyFromCommand(imageName)
	}
	if err != nil || len(layerIDs) == 0 {
		return fmt.Errorf("Could not get image's history: %s", err)
	}

	// Setup a simple HTTP server if Clair is not local.
	if !strings.Contains(endpoint, "127.0.0.1") && !strings.Contains(endpoint, "localhost") {
		allowedHost := strings.TrimPrefix(endpoint, "http://")
		portIndex := strings.Index(allowedHost, ":")
		if portIndex >= 0 {
			allowedHost = allowedHost[:portIndex]
		}

		log.Printf("Setting up HTTP server (allowing: %s)\n", allowedHost)

		ch := make(chan error)
		go listenHTTP(tmpPath, allowedHost, ch)
		select {
		case err := <-ch:
			return fmt.Errorf("An error occured when starting HTTP server: %s", err)
		case <-time.After(100 * time.Millisecond):
			break
		}

		tmpPath = "http://" + myAddress + ":" + strconv.Itoa(httpPort)
	}

	// Analyze layers.
	log.Printf("Analyzing %d layers... \n", len(layerIDs))
	for i := 0; i < len(layerIDs); i++ {
		log.Printf("Analyzing %s\n", layerIDs[i])

		if i > 0 {
			err = analyzeLayer(endpoint, tmpPath+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], layerIDs[i-1])
		} else {
			err = analyzeLayer(endpoint, tmpPath+"/"+layerIDs[i]+"/layer.tar", layerIDs[i], "")
		}
		if err != nil {
			return fmt.Errorf("Could not analyze layer: %s", err)
		}
	}

	// Get vulnerabilities.
	log.Println("Retrieving image's vulnerabilities")
	layer, err := getLayer(endpoint, layerIDs[len(layerIDs)-1])
	if err != nil {
		return fmt.Errorf("Could not get layer information: %s", err)
	}

	// Print report.
	fmt.Printf("Clair report for image %s (%s)\n", imageName, time.Now().UTC())

	if len(layer.Features) == 0 {
		fmt.Printf("%s No features have been detected in the image. This usually means that the image isn't supported by Clair.\n", color.YellowString("NOTE:"))
		return nil
	}

	isSafe := true
	hasVisibleVulnerabilities := false

	var vulnerabilities = make([]vulnerabilityInfo, 0)
	for _, feature := range layer.Features {
		if len(feature.Vulnerabilities) > 0 {
			for _, vulnerability := range feature.Vulnerabilities {
				severity := types.Priority(vulnerability.Severity)
				isSafe = false

				if minSeverity.Compare(severity) > 0 {
					continue
				}

				hasVisibleVulnerabilities = true
				vulnerabilities = append(vulnerabilities, vulnerabilityInfo{vulnerability, feature, severity})
			}
		}
	}

	// Sort vulnerabilitiy by severity.
	priority := func(v1, v2 vulnerabilityInfo) bool {
		return v1.severity.Compare(v2.severity) >= 0
	}

	By(priority).Sort(vulnerabilities)

	for _, vulnerabilityInfo := range vulnerabilities {
		vulnerability := vulnerabilityInfo.vulnerability
		feature := vulnerabilityInfo.feature
		severity := vulnerabilityInfo.severity

		fmt.Printf("%s (%s)\n", vulnerability.Name, coloredSeverity(severity))

		if vulnerability.Description != "" {
			fmt.Printf("%s\n\n", text.Indent(text.Wrap(vulnerability.Description, 80), "\t"))
		}

		fmt.Printf("\tPackage:       %s @ %s\n", feature.Name, feature.Version)

		if vulnerability.FixedBy != "" {
			fmt.Printf("\tFixed version: %s\n", vulnerability.FixedBy)
		}

		if vulnerability.Link != "" {
			fmt.Printf("\tLink:          %s\n", vulnerability.Link)
		}

		fmt.Printf("\tLayer:         %s\n", feature.AddedBy)
		fmt.Println("")
	}

	if isSafe {
		fmt.Printf("%s No vulnerabilities were detected in your image\n", color.GreenString("Success!"))
	} else if !hasVisibleVulnerabilities {
		fmt.Printf("%s No vulnerabilities matching the minimum severity level were detected in your image\n", color.YellowString("NOTE:"))
	}

	return nil
}
Пример #14
0
func init() {
	var directives []string
	for _, cmd := range search.Commands {
		s := cmd.Name
		if len(cmd.Synonyms) > 0 {
			s += sf(" (synonyms: %s)", strings.Join(cmd.Synonyms, ", "))
		}
		s += "\n"
		s += text.Indent(text.Wrap(cmd.Description, 78), "  ")
		directives = append(directives, s)
	}
	cmdDoc := strings.Join(directives, "\n\n")

	cmdSearch.help = sf(`
The search command exposes a flexible interface for quickly searching IMDb
for entities, where entities includes movies, TV shows, episodes and actors.

A search query has two different components: text to search the names of 
entities in the database and directives to do additional filtering on 
attributes of entities (like year released, episode number, cast/credits, 
etc.). Included in those directives are options to sort the results or specify 
a limit on the number of results returned.

The search query is composed of whitespace delimited tokens. Each token that 
starts and ends with a '{' and '}' is a directive. All other tokens are used as 
text to search the names of entities.

If you're using PostgreSQL with the 'pg_trgm' extension enabled, then text 
searching is fuzzy. Otherwise, text may contain the wildcard '%%' which matches 
any sequence of characters or the wildcard '_' which matches any single 
character. Whenever a wildcard character is used, fuzzy search is disabled (and 
the search will be case insensitive).

Directives have the form '{NAME[:ARGUMENT]}', where NAME is the name of the 
directive and ARGUMENT is an argument for the directive. Each directive either 
requires no argument or requires a single argument.

Examples
--------
The following are some example query strings. They can be used in 'goim search'
as is. Note that examples without wildcards assume that a PostgreSQL database 
is used with the 'pg_trgm' extension enabled. Some also assume that your 
database has certain data (for example, the 'actors' list must be loaded to use 
the 'cast' and 'credits' directives).

Find all entities with names beginning with 'The Matrix' (case insensitive):

  'the matrix%%'

Now restrict those results to only movies:

  'the matrix%%' {movie}

Or restrict them further by only listing movies where Keanu Reeves is a 
credited cast member:

  'the matrix%%' {movie} {cast:keanu reeves}

Finally, sort the list of movies by IMDb rank and restrict the results to only
movies with 10,000 votes or more:

  'the matrix%%' {movie} {cast:keanu reeves} {sort:rank desc} {votes:10000-}

We could also search in the other direction, for example, by finding the top
5 credits in the movie The Matrix:

  {credits:the matrix} {billing:1-5} {sort:billing asc}

If you try this with 'goim search', then you'll get a prompt that 'credits is
ambiguous' with a list of entities to choose. This can be rather inconvenient
to see every time. Luckily, directives like 'credits' and 'cast' are actually
entire sub-searches that support directives themselves. For example, we can 
specify that the matrix is a movie, which should be enough to make an 
umabiguous selection:

  {credits:the matrix {movie}} {billing:1-5} {sort:billing asc}

Let's switch gears and look at searching episodes for television shows. For 
example, we can list the episode names for the first season of The Simpsons:

  {show:simpsons} {seasons:1} {sort:episode asc}

Note here that there is no text to search here. But we could add some if we 
wanted to, for example, to see all episodes in the entire series with 'bart' in 
the title:

  {show:simpsons} {sort:season asc} {sort:episode asc} '%%bart%%' {limit:1000}

Note the changes here: we removed the restriction on the first season, added
a limit of 1000 (since the default limit is 30, but there may be more than 30 
episodes with 'bart' in the title) and added an additional sorting criterion.
In this case, we want to sort by season first and then by episode. (The order
in which they appear in the query matters.)

We can view this data in a lot of different ways, for example, by finding the
top 10 best ranked Simpsons episodes with more than 500 votes:

  {show:simpsons} {sort:rank desc} {limit:10} {votes:500-}

All search directives
---------------------
%s
`, cmdDoc)
}