Beispiel #1
0
// ServerMode is the server process of bashistdb.
func ServerMode() error {
	var err error
	db, err = database.New()
	if err != nil {
		return err
	}
	defer db.Close()

	s, err := net.Listen("tcp", conf.Address)
	if err != nil {
		return err
	}
	log.Info.Println("Started listening on:", conf.Address)
	for {
		conn, err := s.Accept()
		if err != nil {
			log.Info.Println("ERROR:", err.Error())
		}
		log.Info.Printf("Connection from %s.\n", conn.RemoteAddr())
		err = db.LogConn(conn.RemoteAddr())
		if err != nil {
			log.Info.Println("ERROR:", err.Error())
		}
		go handleConn(conn)
	}
	//	return nil // go vet doesn't like this...
}
Beispiel #2
0
// Run is the local process of bashistdb.
func Run() error {
	db, err := database.New()
	if err != nil {
		return errors.New("Failed to load database: " + err.Error())
	}
	defer db.Close()

	log = conf.Log

	switch conf.Operation {
	case conf.OP_IMPORT:
		r := bufio.NewReader(os.Stdin)
		stats, err := db.AddFromBuffer(r, conf.User, conf.Hostname)
		if err != nil {
			return errors.New("Error while processing stdin: " +
				err.Error())
		}
		// We print to log because we usually want this to be quiet
		// as we may run it every time we hit ENTER in a bash prompt.
		log.Info.Println(stats)
	case conf.OP_QUERY:
		res, err := db.RunQuery(conf.QParams)
		if err != nil {
			return err
		}
		fmt.Println(string(res))
	}
	return nil
}