forked from vladimirvivien/gosh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dircmd.go
91 lines (77 loc) · 2.06 KB
/
dircmd.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/donrudo/gosh/api"
)
type pwdCmd string
func (t pwdCmd) Name() string { return string(t) }
func (t pwdCmd) Usage() string { return `pwd` }
func (t pwdCmd) ShortDesc() string { return `finds working directory"` }
func (t pwdCmd) LongDesc() string { return t.ShortDesc() }
func (t pwdCmd) Exec(ctx context.Context, args []string) (context.Context, error) {
customPWD()
return ctx, nil
}
type dirCmd string
func (t dirCmd) Name() string { return string(t) }
func (t dirCmd) Usage() string { return `dir` }
func (t dirCmd) ShortDesc() string { return `lists files in dir` }
func (t dirCmd) LongDesc() string { return t.ShortDesc() }
func (t dirCmd) Exec(ctx context.Context, args []string) (context.Context, error) {
cmdArgs := strings.Join(args[1:], " ")
if cmdArgs == "" {
cmdArgs = "."
}
files, err := ioutil.ReadDir(cmdArgs)
if err != nil {
log.Println(err)
}
for _, f := range files {
fmt.Println(f.Name())
}
return ctx, nil
}
type cdCmd string
func (t cdCmd) Name() string { return string(t) }
func (t cdCmd) Usage() string { return `cd` }
func (t cdCmd) ShortDesc() string { return `change dir` }
func (t cdCmd) LongDesc() string { return t.ShortDesc() }
func (t cdCmd) Exec(ctx context.Context, args []string) (context.Context, error) {
cmdArgs := strings.Join(args[1:], " ")
os.Chdir(cmdArgs)
pwd, err := os.Getwd()
if err != nil {
log.Println(err)
}
return context.WithValue(ctx, "gosh.prompt", "gosh "+pwd+" >"), nil
}
// command module
type dirCmds struct{}
func (t *dirCmds) Init(ctx context.Context) error {
out := ctx.Value("gosh.stdout").(io.Writer)
fmt.Fprintln(out, "dir module loaded OK")
return nil
}
func (t *dirCmds) Registry() map[string]api.Command {
return map[string]api.Command{
"dir": dirCmd("dir"),
"pwd": pwdCmd("pwd"),
"cd": cdCmd("cd"),
}
}
// Commands just dir
var Commands dirCmds
func customPWD() {
var mydir string
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
fmt.Println(mydir)
}