Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjqiu committed Aug 2, 2018
1 parent 16b81e7 commit 2c0c781
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
promcli
.idea/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
37 changes: 10 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
13 changes: 13 additions & 0 deletions pkg/completer.go
Original file line number Diff line number Diff line change
@@ -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)
}

21 changes: 21 additions & 0 deletions pkg/executor.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
}

0 comments on commit 2c0c781

Please sign in to comment.