From da10313e543ecc157087d53318c5f5397635f50f Mon Sep 17 00:00:00 2001 From: Atakan Yenel Date: Sat, 16 Feb 2019 19:33:26 +0100 Subject: [PATCH 1/3] add sleep command --- plugins/sleepcmd.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/sleepcmd.go diff --git a/plugins/sleepcmd.go b/plugins/sleepcmd.go new file mode 100644 index 0000000..ecb7b64 --- /dev/null +++ b/plugins/sleepcmd.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "fmt" + "github.com/vladimirvivien/gosh/api" + "io" + "strconv" + "time" +) + +type sleepCmd string + +func (s sleepCmd) Name() string { + return string(s) +} +func (s sleepCmd) Usage() string { return "Usage: sleep " } +func (s sleepCmd) ShortDesc() string { return "sleeps for seconds" } + +func (s sleepCmd) LongDesc() string { return s.ShortDesc() } + +func (s sleepCmd) Exec(ctx context.Context, args []string) (context.Context, error) { + if len(args) == 2 { + duration, err := strconv.Atoi(args[1]) + if err != nil { + return ctx, err + } + time.Sleep(time.Duration(duration) * time.Second) + return ctx, nil + } + out := ctx.Value("gosh.stdout").(io.Writer) + fmt.Fprintln(out, s.Usage()) + return ctx, nil + +} + +type sleepCmds struct{} + +func (s *sleepCmds) Init(ctx context.Context) error { + out := ctx.Value("gosh.stdout").(io.Writer) + fmt.Fprintln(out, "sleep module loaded") + return nil +} + +func (s *sleepCmds) Registry() map[string]api.Command { + return map[string]api.Command{ + "sleep": sleepCmd("sleep"), + } +} + +var Commands sleepCmds From d8eaef39632864cf5bf824d86be6339d362ee7c6 Mon Sep 17 00:00:00 2001 From: Atakan Yenel Date: Sat, 16 Feb 2019 19:39:00 +0100 Subject: [PATCH 2/3] add mac osx to supported platforms --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd47a18..e8e4337 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ files that implement command plugins. #### Pre-requisites * Go 1.8 or above -* Linux (for now) +* Linux +* Mac OSX Gosh makes it easy to create shell programs. First, download or clone this repository. For a quick start, run the following: From b2b6c038c1e1cc2fc1929fe2ebf71e2e262c0136 Mon Sep 17 00:00:00 2001 From: Atakan Yenel Date: Sat, 16 Feb 2019 20:12:12 +0100 Subject: [PATCH 3/3] fix test & update structure --- .gitignore | 1 + shell/gosh.go => gosh.go | 0 shell/gosh_test.go => gosh_test.go | 10 +++++----- 3 files changed, 6 insertions(+), 5 deletions(-) rename shell/gosh.go => gosh.go (100%) rename shell/gosh_test.go => gosh_test.go (85%) diff --git a/.gitignore b/.gitignore index daf913b..4017d85 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ _testmain.go *.exe *.test *.prof +gosh diff --git a/shell/gosh.go b/gosh.go similarity index 100% rename from shell/gosh.go rename to gosh.go diff --git a/shell/gosh_test.go b/gosh_test.go similarity index 85% rename from shell/gosh_test.go rename to gosh_test.go index 188ee41..a19298d 100644 --- a/shell/gosh_test.go +++ b/gosh_test.go @@ -9,12 +9,12 @@ import ( ) var ( - testPluginsDir = "../plugins" + testPluginsDir = "./plugins" ) func TestShellNew(t *testing.T) { shell := New() - if shell.pluginsDir != pluginsDir { + if shell.pluginsDir != testPluginsDir { t.Error("pluginsDir not set") } } @@ -49,10 +49,10 @@ func TestShellHandle(t *testing.T) { helloOut := bytes.NewBufferString("") shell.ctx = context.WithValue(context.TODO(), "gosh.stdout", helloOut) - if err := shell.handle("testhello"); err == nil { + if _, err := shell.handle(shell.ctx, "testhello"); err == nil { t.Error("this test should have failed with command not found") } - if err := shell.handle("hello"); err != nil { + if _, err := shell.handle(shell.ctx, "hello"); err != nil { t.Error(err) } printedOut := strings.TrimSpace(helloOut.String()) @@ -62,7 +62,7 @@ func TestShellHandle(t *testing.T) { byeOut := bytes.NewBufferString("") shell.ctx = context.WithValue(context.TODO(), "gosh.stdout", byeOut) - if err := shell.handle("goodbye"); err != nil { + if _, err := shell.handle(shell.ctx, "goodbye"); err != nil { t.Error(err) } printedOut = strings.TrimSpace(byeOut.String())