From ca7f612ac49769380bcc4cf0e0cf37d4c6e65411 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 8 Oct 2015 23:01:58 -0700 Subject: [PATCH 1/2] Interpret - as stdin --- cat-main.go | 102 +++++++++++++++++++++++++++++++++------------------- cp-url.go | 8 ++--- 2 files changed, 69 insertions(+), 41 deletions(-) diff --git a/cat-main.go b/cat-main.go index e1c21a99..8389a2cd 100644 --- a/cat-main.go +++ b/cat-main.go @@ -17,7 +17,11 @@ package main import ( + "bufio" + "errors" + "fmt" "io" + "io/ioutil" "os" "strings" "syscall" @@ -41,16 +45,16 @@ EXAMPLES: 1. Concantenate an object from Amazon S3 cloud storage to mplayer standard input. $ mc {{.Name}} https://s3.amazonaws.com/ferenginar/klingon_opera_aktuh_maylotah.ogg | mplayer - - 2. Concantenate a file from local filesystem to standard output. - $ mc {{.Name}} khitomer-accords.txt + 2. Concantenate contents of file1.txt and stdin to standard output. + $ mc {{.Name}} file1.txt - > file.txt - 3. Concantenate multiple files from local filesystem to standard output. - $ mc {{.Name}} *.txt > newfile.txt + 3. Concantenate multiple files from the local filesystem to standard output. + $ mc {{.Name}} part.* > complete.img 4. Concatenate a non english file name from Amazon S3 cloud storage. $ mc {{.Name}} s3/andoria/本語 > /tmp/本語 - 5. Behave like operating system tool ‘cat’, used for shell aliases. + 5. Behave like operating system ‘cat’ tool. Useful for alias cat='mc --mimic cat'. $ echo "Hello, World!" | mc --mimic {{.Name}} Hello, World! @@ -59,58 +63,51 @@ EXAMPLES: `, } +// checkCatSyntax performs command-line input validation for cat command. func checkCatSyntax(ctx *cli.Context) { if (!ctx.Args().Present() && !globalMimicFlag) || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } + for _, arg := range ctx.Args() { - if strings.TrimSpace(arg) == "" { - fatalIf(errInvalidArgument().Trace(), "Unable to validate empty argument.") + if strings.HasPrefix(arg, "-") && len(arg) > 1 { + fatalIf(probe.NewError(errors.New("")), fmt.Sprintf("Unknown flag ‘%s’ passed.", arg)) } } } -func mainCat(ctx *cli.Context) { - checkCatSyntax(ctx) - - stdinMode := false - if globalMimicFlag && !ctx.Args().Present() { - stdinMode = true - } - - // handle std input data - if stdinMode { - fatalIf(catOut(os.Stdin).Trace(), "Unable to read from standard input.") - return - } - - // Convert arguments to URLs: expand alias, fix format... - for _, arg := range ctx.Args() { - fatalIf(catURL(arg).Trace(arg), "Unable to read from ‘"+arg+"’.") - } -} - +// catURL displays contents of a URL to stdout. func catURL(sourceURL string) *probe.Error { config := mustGetMcConfig() URL := getAliasURL(sourceURL, config.Aliases) - sourceClnt, err := source2Client(URL) - if err != nil { - return err.Trace(URL) - } - - // Ignore size, since os.Stat() would not return proper size all the time for local filesystem for example /proc files. - reader, _, err := sourceClnt.GetObject(0, 0) - if err != nil { - return err.Trace(URL) + var reader io.ReadCloser + switch URL { + case "-": + reader = ioutil.NopCloser(bufio.NewReader(os.Stdin)) + default: + sourceClnt, err := source2Client(URL) + if err != nil { + return err.Trace(URL) + } + // Ignore size, since os.Stat() would not return proper size all the time for local filesystem for example /proc files. + reader, _, err = sourceClnt.GetObject(0, 0) + if err != nil { + return err.Trace(URL) + } } defer reader.Close() return catOut(reader).Trace(URL) } +// catOut reads from reader stream and writes to stdout. func catOut(r io.Reader) *probe.Error { - // read till EOF + // Do not forget to flush after stdout. + // out := bufio.NewWriter(os.Stdout) + // defer out.Flush() + + // Read till EOF. if _, err := io.Copy(os.Stdout, r); err != nil { switch e := err.(type) { case *os.PathError: @@ -125,3 +122,34 @@ func catOut(r io.Reader) *probe.Error { } return nil } + +// mainCat is the main entry point for cat command. +func mainCat(ctx *cli.Context) { + checkCatSyntax(ctx) + + stdinMode := false + if globalMimicFlag && !ctx.Args().Present() { + stdinMode = true + } + + // handle std input data + if stdinMode { + fatalIf(catOut(os.Stdin).Trace(), "Unable to read from standard input.") + return + } + + // if Args contain ‘-’, we need to preserve its order specially. + args := []string{} + if ctx.Args().First() == "-" { + for i, arg := range os.Args { + if arg == "cat" { + args = os.Args[i+1:] + break + } + } + } + // Convert arguments to URLs: expand alias, fix format... + for _, arg := range args { + fatalIf(catURL(arg).Trace(arg), "Unable to read from ‘"+arg+"’.") + } +} diff --git a/cp-url.go b/cp-url.go index 2e54db0d..ea724e30 100644 --- a/cp-url.go +++ b/cp-url.go @@ -36,10 +36,10 @@ type copyURLsType uint8 const ( copyURLsTypeInvalid copyURLsType = iota - copyURLsTypeA - copyURLsTypeB - copyURLsTypeC - copyURLsTypeD + copyURLsTypeA // file to file + copyURLsTypeB // file to dir + copyURLsTypeC // recursive to dir + copyURLsTypeD // complex to dir ) // NOTE: All the parse rules should reduced to A: Copy(Source, Target). From 4782259e575185af043820804eb39063740cea15 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Thu, 8 Oct 2015 23:29:27 -0700 Subject: [PATCH 2/2] Interpret - as stdin --- cat-main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cat-main.go b/cat-main.go index 8389a2cd..19a95fc4 100644 --- a/cat-main.go +++ b/cat-main.go @@ -139,10 +139,11 @@ func mainCat(ctx *cli.Context) { } // if Args contain ‘-’, we need to preserve its order specially. - args := []string{} + args := []string(ctx.Args()) if ctx.Args().First() == "-" { for i, arg := range os.Args { if arg == "cat" { + // Overwrite ctx.Args with os.Args. args = os.Args[i+1:] break }