Пример #1
0
func Grep(call []string) error {

	options := GrepOptions{}
	flagSet := uggo.NewFlagSetDefault("grep", "[options] PATTERN [files...]", VERSION)
	flagSet.AliasedBoolVar(&options.IsPerl, []string{"P", "perl-regexp"}, false, "Perl-style regex")
	flagSet.AliasedBoolVar(&options.IsExtended, []string{"E", "extended-regexp"}, true, "Extended regex (default)")
	flagSet.AliasedBoolVar(&options.IsIgnoreCase, []string{"i", "ignore-case"}, false, "ignore case")
	flagSet.AliasedBoolVar(&options.IsPrintFilename, []string{"H", "with-filename"}, true, "print the file name for each match")
	flagSet.AliasedBoolVar(&options.IsPrintLineNumber, []string{"n", "line-number"}, false, "print the line number for each match")
	flagSet.AliasedBoolVar(&options.IsInvertMatch, []string{"v", "invert-match"}, false, "invert match")
	// disable for now
	//	flagSet.AliasedBoolVar(&options.IsRecurse, []string{"r", "recurse"}, false, "recurse into subdirectories")

	err := flagSet.Parse(call[1:])
	if err != nil {
		flagSet.Usage()
		return err
	}
	if flagSet.ProcessHelpOrVersion() {
		return nil
	}
	args := flagSet.Args()
	if len(args) < 1 {
		flagSet.Usage()
		return errors.New("Not enough args")
	}
	pattern := args[0]
	reg, err := compile(pattern, options)
	if err != nil {
		return err
	}

	globs := []string{}
	if len(args) > 1 {
		globs = args[1:]
		files := []string{}
		for _, glob := range globs {
			results, err := filepath.Glob(glob)
			if err != nil {
				return err
			}
			if len(results) < 1 { //no match
				return errors.New("grep: cannot access " + glob + ": No such file or directory")
			}
			files = append(files, results...)
		}
		return grep(reg, files, options)
	} else {
		if uggo.IsPipingStdin() {
			//check STDIN
			return grepReader(os.Stdin, "", reg, options)
		} else {
			//NOT piping.
			return errors.New("Not enough args")
		}
	}
}
Пример #2
0
func getDirList(globs []string, ls *SomeLs, inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) ([]string, error) {
	if len(globs) <= 0 {
		if uggo.IsPipingStdin() {
			//check STDIN
			bio := bufio.NewReader(inPipe)
			//defer bio.Close()
			line, hasMoreInLine, err := bio.ReadLine()
			if err == nil {
				//adding from stdin
				globs = append(globs, strings.TrimSpace(string(line)))
			} else {
				//ok
			}
			for hasMoreInLine {
				line, hasMoreInLine, err = bio.ReadLine()
				if err == nil {
					//adding from stdin
					globs = append(globs, string(line))
				} else {
					//finish
				}
			}
		} else {
			//NOT piping. Just use cwd by default.
			cwd, err := os.Getwd()
			return []string{cwd}, err
		}
	}

	args := []string{}
	for _, glob := range globs {
		results, err := filepath.Glob(glob)
		if err != nil {
			return args, err
		}
		if len(results) < 1 { //no match
			return args, errors.New("ls: cannot access " + glob + ": No such file or directory")
		}
		args = append(args, results...)
	}
	return args, nil
}
Пример #3
0
// Parse CLI flags
func (w *Wgetter) ParseFlags(call []string, errPipe io.Writer) (error, int) {

	flagSet := uggo.NewFlagSetDefault("wget", "[options] URL", VERSION)
	flagSet.SetOutput(errPipe)
	flagSet.AliasedBoolVar(&w.IsContinue, []string{"c", "continue"}, false, "continue")
	flagSet.AliasedStringVar(&w.OutputFilename, []string{"O", "output-document"}, "", "specify filename")
	flagSet.StringVar(&w.DefaultPage, "default-page", "index.html", "default page name")
	flagSet.BoolVar(&w.IsNoCheckCertificate, "no-check-certificate", false, "skip certificate checks")

	//some features are available in go-1.2+ only
	extraOptions(flagSet, w)
	err, code := flagSet.ParsePlus(call[1:])
	if err != nil {
		return err, code
	}

	//fmt.Fprintf(errPipe, "%+v\n", w)
	args := flagSet.Args()
	if len(args) < 1 {
		flagSet.Usage()
		return errors.New("Not enough args"), 1
	}
	if len(args) > 0 {
		w.links = args
		//return wget(links, w)
		return nil, 0
	} else {
		if w.AlwaysPipeStdin || uggo.IsPipingStdin() {
			//check STDIN
			//return wget([]string{}, options)
			return nil, 0
		} else {
			//NOT piping.
			flagSet.Usage()
			return errors.New("Not enough args"), 1
		}
	}
}
Пример #4
0
// ParseFlags parses flags from a commandline []string
func (gunzip *SomeGunzip) ParseFlags(call []string, errPipe io.Writer) (error, int) {
	flagSet := uggo.NewFlagSetDefault("gunzip", "[options] file.gz [list...]", someutils.VERSION)
	flagSet.SetOutput(errPipe)
	flagSet.AliasedBoolVar(&gunzip.IsTest, []string{"t", "test"}, false, "test archive data")
	flagSet.AliasedBoolVar(&gunzip.IsKeep, []string{"k", "keep"}, false, "keep gzip file")
	flagSet.AliasedBoolVar(&gunzip.IsPipeOut, []string{"c", "stdout", "is-stdout"}, false, "output will go to the standard output")

	err, code := flagSet.ParsePlus(call[1:])
	if err != nil {
		return err, code
	}
	args := flagSet.Args()
	//TODO STDIN support
	if len(args) > 0 {
		//OK
	} else if uggo.IsPipingStdin() {
		//OK
	} else {
		return errors.New("No gzip filename given"), 1
	}
	gunzip.Filenames = args
	return nil, 0
}
Пример #5
0
// Exec actually performs the grep
func (grep *SomeGrep) Invoke(invocation *someutils.Invocation) (error, int) {
	invocation.ErrPipe.Drain()
	invocation.AutoHandleSignals()
	reg, err := compile(grep.pattern, grep)
	if err != nil {
		return err, 1
	}
	if len(grep.globs) > 0 {
		files := []string{}
		for _, glob := range grep.globs {
			results, err := filepath.Glob(glob)
			if err != nil {
				return err, 1
			}
			if len(results) < 1 { //no match
				return errors.New("grep: cannot access " + glob + ": No such file or directory"), 1
			}
			files = append(files, results...)
		}
		err = grepAll(reg, files, grep, invocation.MainPipe.Out)
		if err != nil {
			return err, 1
		}
	} else {
		if uggo.IsPipingStdin() {
			//check STDIN
			err = grepReader(invocation.MainPipe.In, "", reg, grep, invocation.MainPipe.Out)
			if err != nil {
				return err, 1
			}
		} else {
			//NOT piping.
			return errors.New("Not enough args"), 1
		}
	}
	return nil, 0
}