Git Product home page Git Product logo

lrange-sadd's Introduction

lrange-sadd

Containerized Redis CLI utility to range a list into a set.

Use case

Sample data

redis-cli lpush mylist:l some_error
redis-cli lpush mylist:l other_error

We wish to lrange and sadd to a set as follows using bash and redis-cli

for item in `redis-cli lrange mylist:l 0 -1`
do
  redis-cli sadd myset:s $item
done

Usage

Config

See app/config.js

    list: {
        description: 'the list to lrange from',
    },
    start: {
        description: 'the start index to lrange from',
    },
    stop: {
        description: 'the stop index to lrange to',
    },
    set: {
        description: 'the set to sadd into',
    },
    limit: {
        description: 'the maximum number of keys to add',
        note: 'zero means unlimited',
        default: 0
    },
    host: {
        description: 'the Redis host',
        default: 'localhost'
    }
    port: {
        description: 'the Redis port',
        default: 6379
    }

Implementation

See app/index.js

    const [lrange] = await multiExecAsync(client, multi => {
        multi.lrange(config.list, config.start, config.stop);
    });

Docker

Having audited the Dockerfile and code, you can build and run as follows:

docker build -t hget https://github.com/evanx/lrange-sadd.git

where we tag the image as hget

docker run --network=host -e list=mylist -e set=myset lrange-sadd

where --network-host connects the container to your localhost bridge. The default host and port are valid in that case i.e. localhost:6379 works in that case.

Prebuilt image demo

evan@dijkstra:~$ docker run --network=test-redis-network \
  -e host=$host \
  -e pattern='authbot:*' -e field=role -e format=both \
  evanxsummers/lrange-sadd

where rather than using --network=host we have a Redis container with IP address $host on a network bridge called test-hget-redis-network

Test Redis instance

See scripts/demo.sh

docker network create -d bridge test-hget-redis-network
container=`docker run --network=test-hget-redis-network \
  --name test-redis-hget -d tutum/redis`
redisPass=`docker logs $container | grep '^\s*redis-cli -a' |
  sed -e 's/^\s*redis-cli -a \(\w*\) .*$/\1/'`
redisHost=`docker inspect $container |
  grep '"IPAddress":' | tail -1 | sed 's/.*"\([0-9\.]*\)",/\1/'`
redisUrl="redis://:$redisPass@$redisHost:6379"
redis-cli -a $redisPass -h $redisHost lpush mylist:l some_item
redis-cli -a $redisPass -h $redisHost lpush mylist:l other_item
redis-cli -a $redisPass -h $redisHost lrange mylist:l
docker run --network=test-hget-redis-network -e redisUrl=$redisUrl \
  -e list=mylist:l -e field=err -e format=both evanxsummers/hget
docker rm -f `docker ps -q -f name=test-redis-hget`
docker network rm test-hget-redis-network

where we:

  • create an isolated bridge network test-hget-redis-network for the demo
  • docker run tutum/redis for an isolated test Redis container
  • from the logs of that instance to get its password into redisPass
  • docker inspect that instance to get its IP number into redisHost
  • build redisUrl from redisPass and redisHost and default port 6379
  • use redis-cli to create some test keys in the Redis container e.g. mylist:l
  • docker run evanxsummers/hget to run our utility against that Redis container
  • remove the test Redis container
  • remove the test network

See docs/demo.out

docker run --network=test-hget-redis-network
-e redisUrl=redis://:[email protected]:6379
-e format=both -e list=mylist:l -e field=err evanxsummers/hget

where we have specified format=both to print hashes key and field value for field=err

mylist:l other_error
mylist:l some_error

https://twitter.com/@evanxsummers

lrange-sadd's People

Contributors

evanx avatar strongbyte-es avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.