diff --git a/Dockerfile b/Dockerfile index 228883e..50aa746 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,5 +4,5 @@ RUN apk -U add bind-tools bash RUN mkdir /bundle WORKDIR /bundle -COPY dig.sh /bundle/ +COPY *.sh /bundle/ RUN chmod +x /bundle/*.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..b264388 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# net - Simple network utilities (0.3.3) + + + +## Installation + +In chat: + +``` +@cog bundle install net +``` + +Via cogctl: + +``` +cogctl bundle install net +``` + +For more details about how to install and configure bundles see: + +* [Installing Bundles](https://cog-book.operable.io/#_installing_bundles) +* [Dynamic Command Configuration](https://cog-book.operable.io/#_dynamic_command_configuration) + +## Commands + +The following commands are included with the bundle. For usage info +about each command see the `help` builtin command: `help net:`. + +* `dig` + > DNS lookup utility + +* `whois` + > Whois query utility diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..5975177 --- /dev/null +++ b/Rakefile @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'yaml' + +# Make sure we're running from the directory the Rakefile lives in since +# many things (ie: docker build, config reading) use relative paths. +Dir.chdir(File.dirname(__FILE__)) + +BUNDLE_CONFIG = YAML.safe_load(File.read('config.yaml')) +BUNDLE_VERSION = BUNDLE_CONFIG['version'] +BUNDLE_IMAGE = "#{BUNDLE_CONFIG['docker']['image']}:#{BUNDLE_CONFIG['docker']['tag']}" + +task :image do |_t| + sh 'docker', 'build', '-t', BUNDLE_IMAGE, '.' +end + +task push: [:image] do |_t| + sh 'docker', 'push', BUNDLE_IMAGE +end + +task release: [:push] do |_t| + sh 'git', 'tag', BUNDLE_VERSION + sh 'git', 'push', 'origin', BUNDLE_VERSION +end + +task install: [:push] do |_t| + sh 'cogctl', 'bundle', 'install', 'config.yaml', '--enable', '--relay-group', + 'default', '--force' +end + +task :readme do |_t| + name = BUNDLE_CONFIG['name'] + description = BUNDLE_CONFIG['description'] + version = BUNDLE_CONFIG['version'] + long_description = BUNDLE_CONFIG['long_description'] + env = BUNDLE_CONFIG.fetch('config', {}).fetch('env', []).map { |e| "* `#{e['var']}`\n > #{e['description']}" }.join("\n\n") + commands = BUNDLE_CONFIG['commands'].map { |n, c| "* `#{n}`\n > #{c['description']}" }.join("\n\n") + + readme = <<~END + # #{name} - #{description} (#{version}) + + #{long_description} + + ## Installation + + In chat: + + ``` + @cog bundle install #{name} + ``` + + Via cogctl: + + ``` + cogctl bundle install #{name} + ``` + + For more details about how to install and configure bundles see: + + * [Installing Bundles](https://cog-book.operable.io/#_installing_bundles) + * [Dynamic Command Configuration](https://cog-book.operable.io/#_dynamic_command_configuration) + + ## Commands + + The following commands are included with the bundle. For usage info + about each command see the `help` builtin command: `help #{name}:`. + + #{commands} + END + + File.write('README.md', readme) +end diff --git a/config.yaml b/config.yaml index 94386d3..60d123c 100644 --- a/config.yaml +++ b/config.yaml @@ -4,10 +4,10 @@ name: net description: "Simple network utilities" author: "Operable " homepage: "https://github.com/cogcmd/net" -version: "0.2" +version: "0.3.3" docker: image: "cogcmd/net" - tag: "0.2" + tag: "0.3.3" commands: dig: executable: "/bundle/dig.sh" @@ -36,11 +36,34 @@ commands: description: "Enable the 'trace' query option. Disabled by default" type: bool required: false + whois: + executable: "/bundle/whois.sh" + description: "Whois query utility" + long_description: | + Simple wrapper for the `whois` command line utility. + arguments: "" + rules: + - "when command is net:whois allow" + options: + server: + description: "Whois server to query against." + type: string + required: false + port: + description: "Port on whois server" + type: string + required: false + short: + description: "Returns a terse answer only containing contact information" + type: bool + required: false templates: dig: body: | ~each var=$results~ - ```~each var=$item.body as=l~ + ``` + ~each var=$item.body as=l~ ~$l~ - ~end~``` + ~end~ + ``` ~end~ diff --git a/whois.sh b/whois.sh new file mode 100755 index 0000000..4a6d0f4 --- /dev/null +++ b/whois.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +declare -a arguments + +if [ "$COG_ARGV_0" == "" ]; then + echo "Error: You must supply a domain to query" +fi + +if [ "$COG_OPT_SERVER" != "" ]; then + arguments+=("-h") + arguments+=("${COG_OPT_SERVER}") +fi + +if [ "$COG_OPT_PORT" != "" ]; then + arguments+=("-p") + arguments+=("${COG_OPT_PORT}") +fi + +# Set template to use for rendering output +echo "COG_TEMPLATE: dig" + +# Run the whois command. +result="$(whois "${arguments[@]}" "$COG_ARGV_0")" + +echo $COG_OPT_SHORT +if [ "$COG_OPT_SHORT" != "" ]; then + echo -e "${result}" | grep -E "^(Registrant|Admin|Tech)" +else + echo -e "${result}" +fi