Skip to content

Instantly share code, notes, and snippets.

@jhass
Last active June 5, 2017 11:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jhass/739bc0f6ec53d0d44f56 to your computer and use it in GitHub Desktop.
bxctl - Small helper to manage my boxcars configuration
/.crystal/
/bxctl
/pkg/
/src/
*.tar.xz
require "json"
CONFIG = ENV["BOXCARS_CONFIG"]? || "/etc/boxcars.json"
class Hash
def self.deep_cast(other_hash)
other_hash.each_with_object({} of K => V) {|entry, hash|
k, v = entry
hash[k.as(K)] = v.as(V)
}
end
end
def read_config
unless File.exists? CONFIG
STDERR.puts "#{CONFIG} not found!"
exit 1
end
config = JSON.parse(File.read(CONFIG)).as_h
config.each_with_object({} of String => Hash(String, String)|String) {|entry, config|
name, target = entry
if target.is_a?(String)
config[name] = target
elsif target.is_a?(Hash)
config[name] = Hash(String, String).deep_cast(target)
end
}
end
def write_config(config)
File.write CONFIG, config.to_pretty_json
end
def help
puts "Usage: #{$0} name target - Add or change mapping"
puts " or: #{$0} delete name - Remove mapping"
puts " or: #{$0} - List current mappings"
end
def list
config = read_config
return if config.empty?
nested_names = config.values
.flat_map {|target| target.is_a?(Hash(String, String)) ? target.keys : [] of String }
nested_left_column = nested_names.empty? ? 0 : nested_names.max_of(&.size)+3
left_column = [config.keys.max_of(&.size)+1, nested_left_column].max
config.each do |name, target|
print name.ljust(left_column)
if target.is_a? String
puts target
elsif target.is_a? Hash(String, String)
puts
target.each do |sub, target|
print " #{sub.ljust(left_column-2)}"
puts target
end
end
end
end
def parse_name(name)
if name.includes? "/"
name, path = name.split(/(?=\/)/, 2)
else
path = "/"
end
name = "#{name}.dev" unless name.ends_with? ".dev"
{name, path}
end
def find_mappings(config, name)
if !config.has_key? name
{} of String => String
elsif config[name].is_a? String
{"/" => config[name].as(String)}
else
config[name].as(Hash(String, String))
end
end
def normalize_mappings(mappings)
if mappings.size == 1 && mappings.keys.first == "/"
mappings.values.first
else
mappings
end
end
def delete(name)
config = read_config
name, path = parse_name(name)
mappings = find_mappings(config, name)
if mappings.has_key? path
mappings.delete path
config[name] = normalize_mappings(mappings)
config.delete(name) if config[name].empty?
write_config config
puts "#{name}#{path} deleted."
else
puts "#{name}#{path} not found."
end
end
def change(name, target)
target = "localhost:#{target}" if target[/^\d+$/]?
config = read_config
name, path = parse_name(name)
mappings = find_mappings(config, name)
mappings[path] = target
config[name] = normalize_mappings(mappings)
write_config config
puts "#{name}#{path} points to #{target} now."
end
if ARGV.empty?
list
elsif ARGV[0]? == "-h" || ARGV[0]? == "--help"
help
elsif ARGV.size == 2
if ARGV[0] == "delete"
delete ARGV[1]
else
change ARGV[0], ARGV[1]
end
else
help
exit 1
end
The MIT License (MIT)
Copyright (c) 2017 Jonne Haß
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# Maintainer: Jonne Haß <me@jhass.eu>
pkgname=bxctl
pkgver=0.1.0
pkgrel=1
pkgdesc="CLI util to manage boxcars config"
arch=('i686' 'x86_64')
url="https://gist.github.com/jhass/739bc0f6ec53d0d44f56"
license=('MIT')
depends=('boxcars')
makedepends=('crystal')
source=("bxctl.cr")
build() {
cd "$srcdir"
crystal build --release bxctl.cr
}
package() {
cd "$srcdir"
install -Dm4755 "bxctl" "$pkgdir/usr/bin/bxctl"
}
sha256sums=('ad7b44e10f0329e8fe5564ea91696fc99cfe60642113bd3ce878e1062e06d2b9')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment