-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcd.go
64 lines (53 loc) · 1.17 KB
/
cd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package someutils
import (
"fmt"
"github.com/laher/uggo"
"io"
"os"
)
// SomeCd represents and performs a `cd` invocation
type SomeCd struct {
destDir string
}
// Name() returns the name of the util
func (cd *SomeCd) Name() string {
return "cd"
}
// ParseFlags parses flags from a commandline []string
func (cd *SomeCd) ParseFlags(call []string, errPipe io.Writer) error {
flagSet := uggo.NewFlagSetDefault("cd", "[options] [args...]", VERSION)
flagSet.SetOutput(errPipe)
// TODO add flags here
err := flagSet.Parse(call[1:])
if err != nil {
fmt.Fprintf(errPipe, "Flag error: %v\n\n", err.Error())
flagSet.Usage()
return err
}
if flagSet.ProcessHelpOrVersion() {
return nil
}
// TODO: validate and process flagSet.Args()
return nil
}
// Exec actually performs the cd
func (cd *SomeCd) Invoke(invocation *Invocation) (error, int) {
invocation.ErrPipe.Drain()
invocation.AutoHandleSignals()
err := os.Chdir(cd.destDir)
if err != nil {
return err, 1
} else {
return err, 0
}
}
// Factory for *SomeCd
func NewCd() *SomeCd {
return new(SomeCd)
}
// Factory for *SomeCd
func Cd(destDir string) *SomeCd {
cd := NewCd()
cd.destDir = destDir
return cd
}