Skip to content

Commit

Permalink
Initial support for interactive mode #14
Browse files Browse the repository at this point in the history
For now, there is a possible bug with goline, we will talk about it
before merge it to develop.
  • Loading branch information
filhodanuvem committed Apr 25, 2014
1 parent 64ab944 commit 2189673
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 3 additions & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

var path *string
var query string

func init() {
parseCommandLine()
Expand Down Expand Up @@ -42,13 +41,14 @@ func printTables() {
func parseCommandLine() {
path = flag.String("p", ".", "The (optional) path to run gitql")
version := flag.Bool("v", false, "The version of gitql")
interactive = flag.Bool("i", false, "Enter to interactive mode")
showTables := flag.Bool("show-tables", false, "Show all tables")
flag.Usage = usage
flag.Parse()

if *version {
// @todo refactor to dynamic value
fmt.Println("Gitql 1.0.0")
fmt.Println("Gitql 1.0.1")
os.Exit(0)
}

Expand All @@ -59,7 +59,7 @@ func parseCommandLine() {
}

query = flag.Arg(0)
if flag.NArg() != 1 {
if !*interactive && flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
Expand Down
27 changes: 27 additions & 0 deletions gitql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,44 @@ import (
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/runtime"
"github.com/cloudson/gitql/semantical"
"github.com/nemith/goline"
"log"
"path/filepath"
)

// global vars set in init() function
var query string
var interactive *bool

func main() {
folder, errFile := filepath.Abs(*path)

if errFile != nil {
log.Fatalln(errFile)
}

if !*interactive {
runQuery(query, folder)
return
}

gl := goline.NewGoLine(goline.StringPrompt("gitql> "))
for {
data, err := gl.Line()
if err != nil {
if err != goline.UserTerminatedError {
log.Fatalln(err)
}
}

if err == goline.UserTerminatedError || data == "exit" || data == "quit" {
return
}
runQuery(data, folder)
}
}

func runQuery(query, folder string) {
parser.New(query)
ast, errGit := parser.AST()
if errGit != nil {
Expand Down
4 changes: 3 additions & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func printTable(rows []tableRow, fields []string) {
for _, r := range rows {
table.AddRow(r)
}
fmt.Println()
table.Print()
}

Expand Down Expand Up @@ -322,8 +323,9 @@ func metadataReference(identifier string, object *git.Reference) string {
if object.IsTag() {
return REFERENCE_TYPE_TAG
}
log.Fatalf("Unexcpeted object %s\n", object.Name())
}
log.Fatalf("Field %s not implemented yet\n", identifier)
log.Fatalf("Field '%s' not implemented yet\n", identifier)

return ""
}
Expand Down

0 comments on commit 2189673

Please sign in to comment.