/
gobot.go
105 lines (88 loc) · 3.08 KB
/
gobot.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* ▄████ ▒█████ ▄▄▄▄ ▒█████ ▄▄▄█████▓
* ██▒ ▀█▒▒██▒ ██▒▓█████▄ ▒██▒ ██▒▓ ██▒ ▓▒
* ▒██░▄▄▄░▒██░ ██▒▒██▒ ▄██▒██░ ██▒▒ ▓██░ ▒░
* ░▓█ ██▓▒██ ██░▒██░█▀ ▒██ ██░░ ▓██▓ ░
* ░▒▓███▀▒░ ████▓▒░░▓█ ▀█▓░ ████▓▒░ ▒██▒ ░
* ░▒ ▒ ░ ▒░▒░▒░ ░▒▓███▀▒░ ▒░▒░▒░ ▒ ░░
* ░ ░ ░ ▒ ▒░ ▒░▒ ░ ░ ▒ ▒░ ░
* ░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ░
* ░ ░ ░ ░ ░ ░
* ░
*
* Interactive Bot, Gobot!
*
* Author : Vishal Telangre
* Source : http://github.com/vishaltelangre/gobot
* License: MIT
*
*/
package main
import (
"flag"
"fmt"
"github.com/vishaltelangre/gobot/extensions"
"github.com/vishaltelangre/gobot/readline"
"strings"
)
const VERSION = "0.0.1"
var (
input string // input is the input string entered by the user on stdio.
isInteractive *bool = flag.Bool("i", false, "Start Gobot in interactive mode")
useCommandString *bool = flag.Bool("c", true, "Make Gobot to execute commands directly from shell")
id *string = flag.String("name", "Gobo", "Name your bot")
showVersion *bool = flag.Bool("version", false, "Show version info")
showHelp *bool = flag.Bool("help", false, "Show help and usage of gobot command")
)
func printCommandUsage() {
fmt.Println("gobot is a bot!\n")
fmt.Println("Usage:")
fmt.Println("\t gobot [options] [command string]\n")
fmt.Println("The options are:")
fmt.Println("\t -i \t start gobot interactive-mode")
fmt.Println("\t --name \t name of gobot appear in interactive-mode")
fmt.Println("\t --version \t displays version of gobot")
fmt.Println("\t --help \t show this information")
fmt.Println("\n The command string can be any of following:\n")
cliQueryHandler("help me")
}
func cliQueryHandler(input string) { extensions.EvalQuery(input) }
func interactiveQueryHandler() {
for {
// displays the bot's specified name on the left of stdio screen
botName := fmt.Sprintf("\x1b[01;33m%s > \x1b[0m", *id)
inpLine := readline.ReadLine(&botName)
if inpLine == nil {
fmt.Println("There seems some issue.")
continue
}
if input = strings.TrimSpace(*inpLine); input != "" {
readline.AddHistory(input)
}
extensions.EvalQuery(input)
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("Gobot - v%s\n", VERSION)
return
}
if *showHelp {
printCommandUsage()
return
}
if *isInteractive {
fmt.Printf("Howdy, greetings from %s.\n", *id)
interactiveQueryHandler()
return
}
if *useCommandString {
inp := strings.Join(flag.Args(), " ")
if trimmedInp := strings.TrimSpace(inp); trimmedInp != "" {
cliQueryHandler(trimmedInp)
return
}
}
printCommandUsage()
}