-
Notifications
You must be signed in to change notification settings - Fork 4
/
cd.go
73 lines (59 loc) · 1.23 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
65
66
67
68
69
70
71
72
73
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package cd
import (
"fmt"
"os"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/lang"
)
type Command struct {
last string
}
func (*Command) String() string { return "cd" }
func (*Command) Usage() string { return "cd [- | DIRECTORY]" }
func (*Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "change the current directory",
}
}
func (*Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
DESCRIPTION
Change the working directory to the given name or the last one if '-'.
`,
}
}
func (*Command) Kind() cmd.Kind { return cmd.DontFork | cmd.CantPipe }
func (cd *Command) Main(args ...string) error {
var dir string
if len(args) > 1 {
return fmt.Errorf("%v: unexpected", args[1:])
}
t, err := os.Getwd()
if err != nil {
return err
}
if len(args) == 0 {
dir = os.Getenv("HOME")
if len(dir) == 0 {
dir = "/root"
}
} else if args[0] == "-" {
if len(cd.last) > 0 {
dir = cd.last
}
} else {
dir = args[0]
}
if len(dir) > 0 {
err := os.Chdir(dir)
if err == nil {
cd.last = t
}
return err
}
return nil
}