Sha256: a9d6fd7bfe8e9df046b66817b0dfe5b5548a599111f696c700ba85c5602668ce

Contents?: true

Size: 1.65 KB

Versions: 73

Compression:

Stored size: 1.65 KB

Contents

#!/usr/bin/env ruby

require 'thor'
require 'json'

class PortManager < Thor
    PORT_FILE = '/var/harbr/ports'
    PORT_RANGE = 60535..65535
    PORTS = {}

    no_commands {
        def load_ports
            if File.exist?(PORT_FILE)
                PORTS.merge!(JSON.parse(File.read(PORT_FILE)))
            end
        end

        def save_ports
            File.write(PORT_FILE, PORTS.to_json)
        end
    }

    desc "assign KEY", "Assign a random port to a given key"
    def assign(key)
        load_ports
        if PORTS.value?(key.downcase)
            invoke :key, [key]
            return
        end

        available_ports = PORT_RANGE.to_a - PORTS.keys.map(&:to_i)
        if available_ports.empty?
            raise 'No available ports'
        end

        port = available_ports.sample
        PORTS[port.to_s.downcase] = key
        save_ports
        puts port
    end

    

    desc "release PORT", "Release a specified port"
    def release(port)
        load_ports
        if PORTS.delete(port.to_s)
            save_ports
            puts "Released port: #{port}"
        else
            puts "Port #{port} is not assigned"
        end
    end

    desc "pool", "List all assigned ports"
    def pool
        load_ports
        puts 'Assigned Ports:'
        puts ""
        PORTS.each { |port, key| puts "Port: #{port}, Key: #{key}\n" }
    end

    desc "key KEY", "Get the port for a given key"
    def key(key)
        load_ports
        port = PORTS.key(key.downcase)
        if port
            port = port.to_i
            puts port
        else
            puts "No port assigned to key #{key}"
        end
    end
end

PortManager.start(ARGV)

Version data entries

73 entries across 73 versions & 2 rubygems

Version Path
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/portr-0.0.2/exe/port
portr-0.0.2 exe/port