Skip to content

Commit 8b51590

Browse files
committed
Command will return error now.
1 parent eaf5593 commit 8b51590

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

commands/command.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package commands
22

33
import (
4+
"errors"
45
"fmt"
5-
"os"
66
)
77

8-
type Command func(args []string)
8+
type Command func(args []string) error
99

1010
func CommandFunc(cmd string) Command {
1111
switch cmd {
@@ -22,7 +22,7 @@ func Empty() {
2222
}
2323

2424
func badCommand(cmd string) Command {
25-
return func(args []string) {
26-
fmt.Fprintf(os.Stderr, "Command \"%s\" does not exists, see \"jg help\" for more information.\n", cmd)
25+
return func(args []string) error {
26+
return errors.New(fmt.Sprintf(`Command "%s" does not exists, see "jg help" for more information.`, cmd))
2727
}
2828
}

commands/help.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package commands
22

33
import (
4+
"errors"
45
"fmt"
56
)
67

7-
func jgHelp(argstrs []string) {
8-
fmt.Println("Not implemented yet.")
8+
func jgHelp(argstrs []string) error {
9+
return errors.New(fmt.Sprint("Not implemented yet."))
910
}

commands/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type serverArgs struct {
1818
StaticFilePath string
1919
}
2020

21-
func jgServer(argstrs []string) {
21+
func jgServer(argstrs []string) error {
2222
var args serverArgs
2323
flagSet := flag.NewFlagSet("jg-server", flag.ExitOnError)
2424
flagSet.UintVar(&args.Port, "p", defaultPort, "Specify the port.")
@@ -30,4 +30,5 @@ func jgServer(argstrs []string) {
3030
Handler: backend.ServeMux(args.StaticFilePath),
3131
}
3232
log.Fatal(server.ListenAndServe())
33+
return nil
3334
}

main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/cwahbong/jg/commands"
56
"os"
67
)
@@ -9,6 +10,9 @@ func main() {
910
if len(os.Args) <= 1 {
1011
commands.Empty()
1112
} else {
12-
commands.CommandFunc(os.Args[1])(os.Args[2:])
13+
err := commands.CommandFunc(os.Args[1])(os.Args[2:])
14+
if err != nil {
15+
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
16+
}
1317
}
1418
}

0 commit comments

Comments
 (0)