mpneuried
2/10/2017 - 9:01 AM

parse and filter redis clients

parse and filter redis clients

querystring = require('querystring')
crypto = require('crypto')

parseRedisClients = ( raw, filter={} )->
	clients = []
	for line in raw.split( "\n" ) when line.length > 1
		cl = querystring.parse( line, " ", "=" )
		if Object.keys( filter ).length
			_match = true
			for _k, _v of filter when cl[ _k ] isnt _v
				_match = false
			clients.push cl if _match
		else
			clients.push cl
		
	return clients
	
getSelfRedisClient = ( redis, cb )->
	rCliName = "client-" + crypto.randomBytes( 5 ).toString( "hex" )
	redis.client "SETNAME", rCliName, ( err, info )->
		if err
			cb( err )
			return
			
		redis.client "list", ( err, clients )->
			if err
				cb( err )
				return
			[ selfCli ] = parseRedisClients( clients, { name: rCliName } )
			if not selfCli?
				cb( new Error( "client-not-found" ) )
				return
				
			cb( null, selfCli )
			
			return
		return
	return