Ejemplo n.º 1
0
// Exec actually performs the tee
func (tee *SomeTee) Invoke(invocation *someutils.Invocation) (error, int) {
	invocation.ErrPipe.Drain()
	invocation.AutoHandleSignals()
	flag := os.O_CREATE
	if tee.isAppend {
		flag = flag | os.O_APPEND
	}
	writeables := uggo.ToWriteableOpeners(tee.args, flag, 0666)
	files, err := uggo.OpenAll(writeables)
	if err != nil {
		return err, 1
	}
	writers := []io.Writer{invocation.MainPipe.Out}
	for _, file := range files {
		writers = append(writers, file)
	}
	multiwriter := io.MultiWriter(writers...)
	_, err = io.Copy(multiwriter, invocation.MainPipe.In)
	if err != nil {
		return err, 1
	}
	for _, file := range files {
		err = file.Close()
		if err != nil {
			return err, 1
		}
	}
	return nil, 0

}
Ejemplo n.º 2
0
func Tee(call []string) error {
	options := TeeOptions{}
	flagSet := uggo.NewFlagSetDefault("tee", "", VERSION)
	flagSet.AliasedBoolVar(&options.isAppend, []string{"a", "append"}, false, "Append instead of overwrite")

	err := flagSet.Parse(call[1:])
	if err != nil {
		return err
	}
	if flagSet.ProcessHelpOrVersion() {
		return nil
	}
	flag := os.O_CREATE
	if options.isAppend {
		flag = flag | os.O_APPEND
	}
	writeables := flagSet.ArgsAsWriteables(flag, 0666)
	files, err := uggo.OpenAll(writeables)
	if err != nil {
		return err
	}
	writers := []io.Writer{os.Stdout}
	for _, file := range files {
		writers = append(writers, file)
	}
	multiwriter := io.MultiWriter(writers...)
	_, err = io.Copy(multiwriter, os.Stdin)
	if err != nil {
		return err
	}
	for _, file := range files {
		err = file.Close()
		if err != nil {
			return err
		}
	}
	return nil
}