From ed4bbaafaf1fc5c0d821abe1bce76ba1dcb5c783 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sun, 9 Aug 2015 22:22:18 -0700 Subject: [PATCH] cat rewrite --- cat-main.go | 50 +++++++++++++++++++++++++++++++++++--------------- cat_test.go | 4 ++-- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cat-main.go b/cat-main.go index 0cdfce8b..5726020d 100644 --- a/cat-main.go +++ b/cat-main.go @@ -29,7 +29,7 @@ import ( var catCmd = cli.Command{ Name: "cat", Usage: "Display contents of a file", - Action: runCatCmd, + Action: mainCat, CustomHelpTemplate: `NAME: mc {{.Name}} - {{.Usage}} @@ -59,33 +59,53 @@ EXAMPLES: `, } -func runCatCmd(ctx *cli.Context) { - if !ctx.Args().Present() || ctx.Args().First() == "help" { +func mainCat(ctx *cli.Context) { + stdinMode := false + if globalAliasFlag { + if !ctx.Args().Present() { + stdinMode = true + } + } else if !ctx.Args().Present() || ctx.Args().First() == "help" { cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code } - config := mustGetMcConfig() - // Convert arguments to URLs: expand alias, fix format... - for _, arg := range ctx.Args() { - sourceURL, err := getExpandedURL(arg, config.Aliases) - Fatal(err) - Fatal(doCatCmd(sourceURL)) + + if stdinMode { + Fatal(catOut(os.Stdin)) + } else { + // Convert arguments to URLs: expand alias, fix format... + for _, arg := range ctx.Args() { + Fatal(catURL(arg)) + } } + } -func doCatCmd(sourceURL string) *probe.Error { - sourceClnt, err := source2Client(sourceURL) +func catURL(sourceURL string) *probe.Error { + config := mustGetMcConfig() + + URL, err := getExpandedURL(sourceURL, config.Aliases) if err != nil { - return err.Trace() + return err.Trace(sourceURL) } - // ignore size, since os.Stat() would not return proper size all the time for local filesystem - // for example /proc files. + + 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() } defer reader.Close() + + return catOut(reader).Trace() +} + +func catOut(r io.Reader) *probe.Error { // read till EOF - if _, err := io.Copy(os.Stdout, reader); err != nil { + if _, err := io.Copy(os.Stdout, r); err != nil { switch e := err.(type) { case *os.PathError: if e.Err == syscall.EPIPE { diff --git a/cat_test.go b/cat_test.go index 04acf132..8799d3c6 100644 --- a/cat_test.go +++ b/cat_test.go @@ -48,11 +48,11 @@ func (s *CmdTestSuite) TestCatCmd(c *C) { sourceURLs = append(sourceURLs, objectPath) sourceURLs = append(sourceURLs, objectPathServer) for _, sourceURL := range sourceURLs { - c.Assert(doCatCmd(sourceURL), IsNil) + c.Assert(catURL(sourceURL), IsNil) } objectPath = filepath.Join(root, "object2") - c.Assert(doCatCmd(objectPath), Not(IsNil)) + c.Assert(catURL(objectPath), Not(IsNil)) } func (s *CmdTestSuite) TestCatContext(c *C) {