Skip to content

Commit c6bd70e

Browse files
committed
Add comments on exported fields.
1 parent c1dd439 commit c6bd70e

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (b *Builder) expand(v string, ctx map[string]string) string {
2424
return os.Expand(v, b.getConfig(ctx))
2525
}
2626

27+
// Command returns the result of the templates as a single string.
2728
func (b *Builder) Command() string {
2829
parts := []string{}
2930
for _, t := range b.Templates {
@@ -33,6 +34,9 @@ func (b *Builder) Command() string {
3334
return strings.Join(parts, " ")
3435
}
3536

37+
// CommandContext returns the result of the templates as a single
38+
// string, but allows providing an environment context as a
39+
// map[string]string for expansions.
3640
func (b *Builder) CommandContext(ctx map[string]string) string {
3741
// Build our environment context by starting with our Builder
3842
// context and overlay the passed in context map.

env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
)
88

9+
// ParseEnv takes an environment []string and converts it to a map[string]string.
910
func ParseEnv(environ []string) map[string]string {
1011
env := make(map[string]string)
1112
for _, e := range environ {
@@ -19,6 +20,10 @@ func ParseEnv(environ []string) map[string]string {
1920
return env
2021
}
2122

23+
// Env takes a map[string]string and converts it to a []string that
24+
// can be used with exec.Cmd. The useEnv boolean flag will include the
25+
// current process environment, overlaying the provided env
26+
// map[string]string.
2227
func Env(env map[string]string, useEnv bool) []string {
2328
envlist := []string{}
2429

parse.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ import (
88
shlex "github.com/flynn/go-shlex"
99
)
1010

11+
// SplitCommand parses a command and splits it into lexical arguments
12+
// like a shell, returning a []string that can be used as arguments to
13+
// exec.Command.
1114
func SplitCommand(cmd string) []string {
1215
return SplitCommandEnv(cmd, os.Getenv)
1316
}
1417

18+
// SplitCommandEnv parses a command and splits it into lexical
19+
// arguments like a shell, returning a []string that can be used as
20+
// arguments to exec.Command. It also allows providing an expansion
21+
// function that will be used when expanding values within the parsed
22+
// arguments.
1523
func SplitCommandEnv(cmd string, getenv func(key string) string) []string {
1624
parts, err := shlex.Split(strings.TrimSpace(strings.Trim(os.Expand(cmd, getenv), "`")))
1725
if err != nil {

process.go

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212
)
1313

14+
// Process is intended to be used like exec.Cmd where possible.
1415
type Process struct {
1516
// CmdString takes a string and parses it into the relevant cmds
1617
CmdString string
@@ -118,6 +119,8 @@ func (p *Process) findCmds() {
118119
p.addCmd(cmd)
119120
}
120121

122+
// lineReader takes will read a line in the io.Reader and write to the
123+
// Process output buffer and use any OutputHandler that exists.
121124
func (p *Process) lineReader(wg *sync.WaitGroup, r io.Reader) {
122125
defer wg.Done()
123126

@@ -152,12 +155,16 @@ func (p *Process) lineReader(wg *sync.WaitGroup, r io.Reader) {
152155
}
153156
}
154157

158+
// checkErr shortens the creation of the pipes by bailing out with a
159+
// log.Fatal.
155160
func checkErr(msg string, err error) {
156161
if err != nil {
157162
log.Fatalf("%s: %s", msg, err)
158163
}
159164
}
160165

166+
// setupOutputHandler configures the last cmd in the list of cmds to
167+
// use the output handler defined.
161168
func (p *Process) setupOutputHandler(cmd *exec.Cmd) error {
162169
stdout, err := cmd.StdoutPipe()
163170
checkErr("error creating stdout pipe", err)
@@ -222,37 +229,7 @@ func (p *Process) wait() error {
222229
return nil
223230
}
224231

225-
func (p *Process) call(index int, wait bool) error {
226-
// This hasn't already been started so start it
227-
if p.Cmds[index].Process == nil {
228-
if err := p.Cmds[index].Start(); err != nil {
229-
return err
230-
}
231-
}
232-
233-
// See if we have more cmds to run and start them by recursively calling Call
234-
if len(p.Cmds[index:]) > 1 {
235-
err := p.Cmds[index+1].Start()
236-
if err != nil {
237-
return err
238-
}
239-
240-
defer func() {
241-
if err == nil {
242-
p.Pipes[index].Close()
243-
err = p.call(index+1, wait)
244-
}
245-
}()
246-
}
247-
248-
last := len(p.Cmds) - 1
249-
if index == last && wait == true {
250-
return nil
251-
}
252-
253-
return p.Cmds[index].Wait()
254-
}
255-
232+
// Run executes the cmds and returns the output as a string and any error.
256233
func (p *Process) Run() (string, error) {
257234
p.findCmds()
258235
p.setupPipes()
@@ -268,16 +245,11 @@ func (p *Process) Run() (string, error) {
268245
return p.outBuffer.String(), nil
269246
}
270247

248+
// Start will start the list of cmds.
271249
func (p *Process) Start() error {
272250
p.findCmds()
273251
p.setupPipes()
274252

275-
// if err := p.call(0, true); err != nil {
276-
// fmt.Printf("error calling command: %q\n", err)
277-
// fmt.Println(string(p.errBuffer.Bytes()))
278-
// return err
279-
// }
280-
281253
if err := p.start(); err != nil {
282254
fmt.Printf("error calling command: %q\n", err)
283255
fmt.Println(string(p.errBuffer.Bytes()))
@@ -299,6 +271,7 @@ func (p *Process) Start() error {
299271
return nil
300272
}
301273

274+
// Wait will block, waiting for the commands to finish.
302275
func (p *Process) Wait() error {
303276
if p.outputWait != nil {
304277
p.outputWait.Wait()
@@ -308,6 +281,7 @@ func (p *Process) Wait() error {
308281
return p.Cmds[last].Wait()
309282
}
310283

284+
// Output returns the buffered output as a string.
311285
func (p *Process) Output() string {
312286
return p.outBuffer.String()
313287
}

0 commit comments

Comments
 (0)