Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(18)

Side by Side Diff: cmd/context.go

Issue 6123049: Moved printUsage to Info
Patch Set: Moved printUsage to Info Created 12 years, 11 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cmd/command.go ('k') | cmd/context_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package cmd 1 package cmd
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "io" 5 "io"
6 "io/ioutil" 6 "io/ioutil"
7 "launchpad.net/gnuflag" 7 "launchpad.net/gnuflag"
8 "launchpad.net/juju/go/log" 8 "launchpad.net/juju/go/log"
9 stdlog "log" 9 stdlog "log"
10 "os" 10 "os"
11 "path/filepath" 11 "path/filepath"
12 "strings"
13 ) 12 )
14 13
15 // Context adds a layer of indirection between a Command and its environment, 14 // Context represents the run context of a Command. Command implementations
16 // to allow cmd.Commands to be run without using the current process's working 15 // should interpret file names relative to Dir (see AbsPath below), and print
17 // directory or output streams. This in turn enables "hosted" Command execution, 16 // output and errors to Stdout and Stderr respectively.
18 // whereby a hook-invoked tool can delegate full responsibility for command
19 // execution to the unit agent process (which holds the state required to
20 // actually execute them) and dumbly produce output (and exit code) returned
21 // from the agent.
22 type Context struct { 17 type Context struct {
23 Dir string 18 Dir string
24 Stdout io.Writer 19 Stdout io.Writer
25 Stderr io.Writer 20 Stderr io.Writer
26 } 21 }
27 22
28 // DefaultContext returns a Context suitable for use in non-hosted situations. 23 // DefaultContext returns a Context suitable for use in non-hosted situations.
29 func DefaultContext() *Context { 24 func DefaultContext() *Context {
30 dir, err := os.Getwd() 25 dir, err := os.Getwd()
31 if err != nil { 26 if err != nil {
(...skipping 29 matching lines...) Expand all
61 target = ctx.Stderr 56 target = ctx.Stderr
62 } 57 }
63 if target != nil { 58 if target != nil {
64 log.Target = stdlog.New(target, "", stdlog.LstdFlags) 59 log.Target = stdlog.New(target, "", stdlog.LstdFlags)
65 } else { 60 } else {
66 log.Target = nil 61 log.Target = nil
67 } 62 }
68 return 63 return
69 } 64 }
70 65
71 // Main will Parse and Run a Command, and return a process exit code. args 66 // Main runs the given Command in the supplied Context with the given
72 // should contain flags and arguments only (and not the top-level command name). 67 // arguments, which should not include the command name. It returns a code
68 // suitable for passing to os.Exit.
73 func Main(c Command, ctx *Context, args []string) int { 69 func Main(c Command, ctx *Context, args []string) int {
74 f := gnuflag.NewFlagSet(c.Info().Name, gnuflag.ContinueOnError) 70 f := gnuflag.NewFlagSet(c.Info().Name, gnuflag.ContinueOnError)
75 f.SetOutput(ioutil.Discard) 71 f.SetOutput(ioutil.Discard)
76 if err := c.Init(f, args); err != nil { 72 if err := c.Init(f, args); err != nil {
77 fmt.Fprintf(ctx.Stderr, "%v\n", err) 73 fmt.Fprintf(ctx.Stderr, "%v\n", err)
78 » » printUsage(c, f, ctx.Stderr) 74 » » ctx.Stderr.Write(c.Info().usage(f))
79 return 2 75 return 2
80 } 76 }
81 if err := c.Run(ctx); err != nil { 77 if err := c.Run(ctx); err != nil {
82 log.Debugf("%s command failed: %s\n", c.Info().Name, err) 78 log.Debugf("%s command failed: %s\n", c.Info().Name, err)
83 fmt.Fprintf(ctx.Stderr, "%v\n", err) 79 fmt.Fprintf(ctx.Stderr, "%v\n", err)
84 return 1 80 return 1
85 } 81 }
86 return 0 82 return 0
87 } 83 }
88
89 // printUsage prints usage information for c to output.
90 func printUsage(c Command, f *gnuflag.FlagSet, output io.Writer) {
91 i := c.Info()
92 fmt.Fprintf(output, "usage: %s\n", i.Usage())
93 if i.Purpose != "" {
94 fmt.Fprintf(output, "purpose: %s\n", i.Purpose)
95 }
96 hasOptions := false
97 f.VisitAll(func(f *gnuflag.Flag) { hasOptions = true })
98 if hasOptions {
99 fmt.Fprintf(output, "\noptions:\n")
100 f.SetOutput(output)
101 f.PrintDefaults()
102 f.SetOutput(ioutil.Discard)
103 }
104 if i.Doc != "" {
105 fmt.Fprintf(output, "\n%s\n", strings.TrimSpace(i.Doc))
106 }
107 }
OLDNEW
« no previous file with comments | « cmd/command.go ('k') | cmd/context_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b