예제 #1
0
파일: main.go 프로젝트: ikeee/dockersql
func mainAction(context *cli.Context) {
	db, err := loadDatabase(context)
	if err != nil {
		logger.Fatal(err)
	}
	defer db.Close()
	ln.SetMultiline(true)
	ln.SetCompletionHandler(completion)
	for {
		query, err := ln.Line("> ")
		if err != nil {
			if err != ln.KillSignalError {
				logger.Error(err)
			}
			return
		}
		if query == "" {
			continue
		}
		if err := ln.AddHistory(query); err != nil {
			logger.Error(err)
		}
		rows, err := db.Query(query)
		if err != nil {
			logger.Warn(err)
			continue
		}
		if err := DisplayResults(rows); err != nil {
			db.Close()
			logger.Fatal(err)
		}
	}
}
예제 #2
0
파일: main.go 프로젝트: lovek323/bclog
func main() {
	statistics = make(map[string][]events.LogEventInterface)

	loadConfig()

	linenoise.SetCompletionHandler(func(in string) []string {
		availableCommands := []string{"clear", "help", "reload", "show", "quit", "summary"}
		matchedCommands := []string{}

		for _, command := range availableCommands {
			if len(in) <= len(command) && strings.Index(command, in) == 0 {
				matchedCommands = append(matchedCommands, command)
			} else if len(in) > len("show") && in[0:len("show")] == "show" {
				for summary, _ := range statistics {
					if len(in[len("show "):]) <= len(summary) && strings.Index(summary, in[len("show "):]) == 0 {
						matchedCommands = append(matchedCommands, "show "+summary)
					}
				}
			}
		}

		return matchedCommands
	})

	go func() {
		for {
			lastPrompt = time.Now()

			line, err := linenoise.Line("> ")

			if err != nil {
				if err == linenoise.KillSignalError {
					quit()
				} else {
					log.Fatalf("Could not read line: %s\n", err)
				}
			}

			err = linenoise.AddHistory(line)

			if err != nil {
				log.Printf("Failed to add %s to history (%s)\n", line, err)
			}

			args := strings.Split(line, " ")

			if len(args) == 0 {
				continue
			}

			switch args[0] {
			case "":
				summary([]string{"last-prompt"})
				break
			case "clear":
				linenoise.Clear()
				break
			case "help":
				help()
			case "quit":
				quit()
				break
			case "reload":
				loadConfig()
				break
			case "show":
				show(args[1:])
				break
			case "summary":
				summary(args[1:])
				break

			default:
				index, err := strconv.ParseInt(line, 10, 32)

				if err == nil {
					event := history[index]
					event.PrintFull()
				} else {
					fmt.Printf("Unrecognised command: %s\n\n", line)
				}
			}
		}
	}()

	readLog()
}