This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this commit adds an interactive CLI for the inspect tool and solves #64
- Loading branch information
1 parent
c598e6e
commit 489a05d
Showing
6 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/xqueries/xdb/internal/inspect" | ||
|
||
"github.com/c-bata/go-prompt" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/cobra" | ||
"github.com/xqueries/xdb/internal/engine" | ||
"github.com/xqueries/xdb/internal/engine/storage" | ||
) | ||
|
||
func completer(d prompt.Document) []prompt.Suggest { | ||
s := []prompt.Suggest{} | ||
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true) | ||
} | ||
|
||
func inspectXDB(cmd *cobra.Command, args []string) { | ||
log := cmd.Context().Value(ctxKeyLog).(zerolog.Logger) | ||
|
||
// The file is opened in an O_RDONLY mode, | ||
// complying with the policies of the tool. | ||
file, err := os.Open(args[0]) | ||
if err != nil { | ||
log.Err(err) | ||
} | ||
f, err := storage.Open(file) | ||
if err != nil { | ||
log.Err(err) | ||
} | ||
_, err = engine.New(f, engine.WithLogger(log)) | ||
if err != nil { | ||
log.Err(err) | ||
} | ||
|
||
beginning := true | ||
for { | ||
if beginning { | ||
fmt.Println("Welcome to xdb inspect.\nType \"help\" to get list of available commands.\n") | ||
beginning = false | ||
} | ||
t := prompt.Input("xdb inspect> ", completer) | ||
if t == "q" || t == "Q" { | ||
fmt.Println("Exiting xdb inspect, seeya.\n") | ||
return | ||
} | ||
if t != "" { | ||
// The command that was input to the CLI is | ||
// passed to the inspect package. The returned | ||
// data/string is formatted and only needs to be | ||
// printed straightaway. | ||
res := inspect.Inspect(t) | ||
fmt.Println(res) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package inspect | ||
|
||
import "fmt" | ||
|
||
func Inspect(cmd string) string { | ||
fmt.Printf("You wrote: %s\n", cmd) | ||
return "Nice work" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters