Skip to content

Commit

Permalink
add tee (nao1215#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
polynomialspace committed Mar 21, 2022
1 parent 3b70399 commit 6915a0a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/applets/applet.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import (
"github.com/nao1215/mimixbox/internal/applets/shellutils/serial"
"github.com/nao1215/mimixbox/internal/applets/shellutils/sleep"
"github.com/nao1215/mimixbox/internal/applets/shellutils/sync"
"github.com/nao1215/mimixbox/internal/applets/shellutils/tee"
"github.com/nao1215/mimixbox/internal/applets/shellutils/true"
"github.com/nao1215/mimixbox/internal/applets/shellutils/uuidgen"
"github.com/nao1215/mimixbox/internal/applets/shellutils/wget"
Expand Down Expand Up @@ -155,6 +156,7 @@ func init() {
"sync": {sync.Run, "Synchronize cached writes to persistent storage"},
"tac": {tac.Run, "Print the file contents from the end to the beginning"},
"tail": {tail.Run, "Print the last NUMBER(default=10) lines"},
"tee": {tee.Run, "Read from standard input and write to standard output and files"},
"touch": {touch.Run, "Update the access and modification times of each FILE to the current time"},
"tr": {tr.Run, "Translate or delete characters"},
"true": {true.Run, "Do nothing. Return success(0)"},
Expand Down
75 changes: 75 additions & 0 deletions internal/applets/shellutils/tee/tee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tee

import (
"io"
"os"
"os/signal"
"syscall"

"github.com/jessevdk/go-flags"
mb "github.com/nao1215/mimixbox/internal/lib"
)

type options struct {
Version bool `short:"v" long:"version" description:"print version and exit"`
Append bool `short:"a" long:"append" description:"append to files, do not overwrite"`
IgnoreSIGINT bool `short:"i" long:"ignore-interrupts" description:"ignore SIGINT"`
}

const cmdName string = "tee"
const version = "1.0.1"

func initParser(opts *options) *flags.Parser {
parser := flags.NewParser(opts, flags.Default)
parser.Name = cmdName
parser.Usage = "[OPTIONS]"

return parser
}

func Run() (int, error) {
var opts options
args, err := initParser(&opts).Parse()
if err != nil {
return mb.ExitFailure, err
}

if opts.Version {
mb.ShowVersion(cmdName, version)
os.Exit(mb.ExitSuccess)
}

openFlags := os.O_WRONLY | os.O_CREATE
if opts.Append {
openFlags |= os.O_APPEND
}

if opts.IgnoreSIGINT {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
for range sigs {
}
}()

}

files := make([]io.Writer, 0, 1+len(args))
files = append(files, os.Stdout)
for _, filename := range args {
f, err := os.OpenFile(filename, openFlags, 0644)
if err != nil {
return mb.ExitFailure, err
}
files = append(files, f)
defer f.Close()
}

multiwriter := io.MultiWriter(files...)
_, err = io.Copy(multiwriter, os.Stdin)
if err != nil {
return mb.ExitFailure, err
}

return mb.ExitSuccess, nil
}

0 comments on commit 6915a0a

Please sign in to comment.