simple-snippet-search
v1.0.0
Published
A simple search service that allows you search for quick code snippets
Downloads
2
Maintainers
Readme
Simple Snippet Search!
A simple search service that allows searching for quick code snippets, along with optional description of how the
piece of snippet works.
At the moment, it contains just some sample docker image snippets.
Adding more data sources
To add more data sources, simply create either a
- .json file OR
- module> directory containing several JSON files, e.g /file1.json, file2.json, ...
inside the .data directory. (Support for other data formats may be added later).
For example, to create a PHP data source, you can create a php.json file or a php directory
containing JSON files for different aspects of PHP.
During the API calls for searching, if a .json file is found, it will be searched;
otherwise, files inside / directory will be searched instead.
API calls are made to two endpoints:
The /modules endpoint (host:[port]/modules) to list available modules (e.g: php, docker, jenkins, java, etc).
The server reads and returns every available module based on .json and directories.
Modules can be filtered by appending an optional filter query string to the /modules endpoint to return
only modules that match a specified filter text.
Examples:- /modules?filter=d: will return only modules that contain the letter d.
- /modules?filter=docker: will return only the docker module.
The /search/ endpoint allows you search within a given module. It supports a query query string that specifies the search term within the given module:
Examples:- /search/docker?query=del: will return every docker command containing the letters del.
- /search/docker?query=delete image: will return every docker command relating to deleting images (including dangling images).
- /search/docker?query=delete dangling images: will return docker commands relevant to deleting dangling images only.
Response formats
The response format is JSON.
When a request is made to the /modules endpoint, the response is an object containing a modules property
which is an array of available modules, as determined by the files or directories inside the .data/ directory:
{
"modules": [
"jenkins",
"docker",
...
]
}
When a search is performed - /search/?query= - the response is an object with a commands property containing example snippets for achieving the searched functionality. Results, i.e, .json data files, can be encoded as an object holding any of the following:
- an array of strings
- an object with properties: command and description
- an array of objects, with each object having properties: command and description
- Any combination of the above 3 formats (an array of strings and objects):
{
"delete image": [
"docker image rm <image>",
"docker rmi <image>"
],
"list images": {
"command": "docker image ls [OPTIONS] [REPOSITORY[:TAG]]",
"description": [
"[Available Options]\n",
"--all --all, -a - Show all images, including intermediate images\n",
"--digests - Show digests\n",
"--filter, -f - Filter output based on provided conditions\n",
"--format - Pretty-print images using a Go template\n",
"--no-trunc - Don't truncate output\n",
"--quiet, -q - Only show numeric IDs"
]
},
"delete dangling images": [
"docker image prune",
{
"command": "docker rmi $(docker images --quiet --filter \"dangling=true\")",
"description": "Can also be written as: docker rmi $(docker images -q -f dangling=true)"
},
{
"command": "docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi",
"description": "Short form: docker images -qf dangling=true | xargs docker rmi"
}
]
}
Run
- Run with
npm start
(ornode index.js
)
Using the GUI (browser) client
- Run with
npm start
(ornode index.js
) - Navigate to http(s)://HOST[:PORT]. The host and port can be configured in the /lib/config.json file. The defaults are: host:localhost, ports (http, https):
- production: 5000, 5001
- staging: 3000, 3001
- testing: 4000, 4001
Tests
Run with npm run test
(or node tests
)