Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save history correctly #119

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vendorbuild: clean fmt
clean:
@rm -rf ${name} vendor

clean_all:
clean-all:
@rm -rf ${name} vendor box/blob.go

fmt:
Expand Down
8 changes: 4 additions & 4 deletions cli/icli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"log"
"os"

"github.com/dutor/liner"
"github.com/jievince/liner"
"github.com/vesoft-inc/nebula-console/completer"
)

Expand All @@ -38,7 +38,7 @@ func NewiCli(historyFile, user string) Cli {

f, err := os.OpenFile(historyFile, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
log.Panicf("Open history file %s failed, %s", historyFile, err.Error())
log.Panicf("Open or create history file %s failed, %s", historyFile, err.Error())
}
defer f.Close()
c.ReadHistory(f)
Expand Down Expand Up @@ -121,9 +121,9 @@ func (l iCli) IsPlayingData() bool {

func (l *iCli) Close() {
defer l.terminal.Close()
f, err := os.Create(l.status.historyFile)
f, err := os.OpenFile(l.status.historyFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Panicf("Write history file %s failed, %s", l.status.historyFile, err.Error())
log.Panicf("Open or create history file %s failed, %s", l.status.historyFile, err.Error())
}
defer f.Close()
l.terminal.WriteHistory(f)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/vesoft-inc/nebula-console
go 1.11

require (
github.com/dutor/liner v1.2.2
github.com/jedib0t/go-pretty/v6 v6.0.5
github.com/jievince/liner v1.2.3
github.com/vesoft-inc/nebula-go/v2 v2.0.0-20210701060243-a0577f67f375
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dutor/liner v1.2.2 h1:bet1doARkm1DKTuQ45DgMmgaKLJ9S4taUWQGH3kjlyo=
github.com/dutor/liner v1.2.2/go.mod h1:TLqJOMU/a2J4ualIteF7J7DkL2Tv3WE8hZs5o+ogcDI=
github.com/facebook/fbthrift v0.31.1-0.20210223140454-614a73a42488 h1:A4KCT0mvTBkvb93gGN+efLPkrgTqmqMeaLDG51KVhMM=
github.com/facebook/fbthrift v0.31.1-0.20210223140454-614a73a42488/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU=
github.com/jedib0t/go-pretty/v6 v6.0.5 h1:oOo0/jSb3NEYKT6l1hhFXoX2UZnkanMuCE2DVT1mqnE=
github.com/jedib0t/go-pretty/v6 v6.0.5/go.mod h1:MTr6FgcfNdnN5wPVBzJ6mhJeDyiF0yBvS2TMXEV/XSU=
github.com/jievince/liner v1.2.3 h1:hpMEqBKkIg/RHzHCHZqbs19MDq8bT8b5o85D3o/Yggk=
github.com/jievince/liner v1.2.3/go.mod h1:6szfFB+ea00sIHdOn/4gDFoD6sa2UaUHR28Ca8QGj9U=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand Down
37 changes: 13 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,20 @@ func playData(data string) (string, error) {
boxfilePath := "/" + data + ".ngql"
posixfilePath := "./data/" + data + ".ngql"
var c cli.Cli
// First find it in embeded box. If not found, then find it in the directory ./data/
if box.Has(boxfilePath) {
// First find it in directory ./data/. If not found, then find it in the embeded box
if fd, err := os.Open(posixfilePath); err == nil {
c = cli.NewnCli(fd, false, "", func() { fd.Close() })
} else if box.Has(boxfilePath) {
fileStr := string(box.Get(boxfilePath))
c = cli.NewnCli(strings.NewReader(fileStr), false, "", nil)
} else if fd, err := os.Open(posixfilePath); err == nil {
c = cli.NewnCli(fd, false, "", func() { fd.Close() })
} else {
return "", fmt.Errorf("file %s.ngql not existed in embed box and file directory ./data/ ", data)
}

c.PlayingData(true)
defer c.PlayingData(false)
fmt.Printf("Start loading dataset %s...\n", data)
childSession, err := pool.GetSession(*username, *password)
if err != nil {
log.Panicf("Fail to create a new session from connection pool, %s", err.Error())
}
defer childSession.Release()
err = loop(childSession, c)
err := loop(c)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -241,7 +236,7 @@ func printResultSet(res *nebula.ResultSet, startTime time.Time) (duration time.D
// Loop the request util fatal or timeout
// We treat one line as one query
// Add line break yourself as `SHOW \<CR>HOSTS`
func loop(session *nebula.Session, c cli.Cli) error {
func loop(c cli.Cli) error {
for {
line, exit, err := c.ReadLine()
if err != nil {
Expand All @@ -251,7 +246,7 @@ func loop(session *nebula.Session, c cli.Cli) error {
fmt.Println()
return nil
}
if len(line) == 0 {
if len(line) == 0 { // 1). The line input is empty, or 2). user presses ctrlC so the input is truncated
continue
}
// Console side command
Expand Down Expand Up @@ -328,16 +323,6 @@ func init() {
flag.StringVar(file, "file", "", "The nGQL script file name")
}

func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}

func validateFlags() {
if *port == -1 {
log.Panicf("Error: argument port is missed!")
Expand All @@ -352,6 +337,8 @@ func validateFlags() {

var pool *nebula.ConnectionPool

var session *nebula.Session

func main() {
flag.Parse()

Expand Down Expand Up @@ -389,7 +376,7 @@ func main() {
}
defer pool.Close()

session, err := pool.GetSession(*username, *password)
session, err = pool.GetSession(*username, *password)
if err != nil {
log.Panicf("Fail to create a new session from connection pool, %s", err.Error())
}
Expand Down Expand Up @@ -418,7 +405,9 @@ func main() {
}

defer c.Close()
err = loop(session, c)

err = loop(c)

if err != nil {
log.Panicf("Loop error, %s", err.Error())
}
Expand Down