diff --git a/.gitignore b/.gitignore index 4ec2ce0..697867f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out promcli +.idea/ diff --git a/Makefile b/Makefile index 6d5b26a..a280b9b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -build: +build: proxy go build proxy: - ln -s $(pwd)/_proxy/proxy.go $(pwd)/vendor/github.com/prometheus/prometheus/promql/proxy.go + ln -s $$(pwd)/_proxy/proxy.go $$(pwd)/vendor/github.com/prometheus/prometheus/promql/proxy.go || true diff --git a/main.go b/main.go index 46095d2..6ea0a47 100644 --- a/main.go +++ b/main.go @@ -2,42 +2,25 @@ package main import ( "github.com/c-bata/go-prompt" + "github.com/kevinjqiu/promcli/pkg" "fmt" - "github.com/prometheus/prometheus/promql" ) -func executor(input string) { - storage := promql.NewTestStorage() - fmt.Println("Your input: " + input) - cmds, err := promql.ParseTestCommand(input) - if err != nil { - panic(err) - } - fmt.Println(cmds) - for _, cmd := range cmds { - storage, err = promql.ExecuteTestCommand(cmd, storage) - if err != nil { - panic(err) - } - } -} +const Banner = ` + ____ ____ _ ___ +| _ \ _ __ ___ _ __ ___ / ___| | |_ _| +| |_) | '__/ _ \| '_ _ \| | | | | | +| __/| | | (_) | | | | | | |___| |___ | | +|_| |_| \___/|_| |_| |_|\____|_____|___|` -func completer(d prompt.Document) []prompt.Suggest { - s := []prompt.Suggest{ - {Text: "users", Description: "Users table"}, - {Text: "articles", Description: "Articles table"}, - {Text: "comments", Description: "Comments table"}, - } - return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) -} func main() { + fmt.Println(Banner) p := prompt.New( - executor, - completer, + pkg.Executor, + pkg.Completer, prompt.OptionPrefix(">>> "), prompt.OptionTitle("PromCLI"), ) - p.Run() } diff --git a/pkg/completer.go b/pkg/completer.go new file mode 100644 index 0000000..f22cc65 --- /dev/null +++ b/pkg/completer.go @@ -0,0 +1,13 @@ +package pkg + +import "github.com/c-bata/go-prompt" + +func Completer(d prompt.Document) []prompt.Suggest { + s := []prompt.Suggest{ + {Text: "clear", Description: "Clear the database"}, + {Text: "load", Description: "Enter the fixture loading state"}, + {Text: "eval", Description: "Evaluate expressions"}, + } + return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) +} + diff --git a/pkg/executor.go b/pkg/executor.go new file mode 100644 index 0000000..e5fe51b --- /dev/null +++ b/pkg/executor.go @@ -0,0 +1,21 @@ +package pkg + +import ( + "fmt" + "github.com/prometheus/prometheus/promql" +) + +func Executor(input string) { + storage := promql.NewTestStorage() + cmds, err := promql.ParseTestCommand(input) + if err != nil { + fmt.Println(err.Error()) + } + for _, cmd := range cmds { + storage, err = promql.ExecuteTestCommand(cmd, storage) + if err != nil { + fmt.Println(err.Error()) + } + } +} +