# frozen_string_literal: true # !/usr/bin/env ruby require 'json' module VSCodeRemoteContainer class Utility attr_accessor :bin_path def initialize @bin_path = ENV['GHQ_PATH'] || '/usr/local/bin' end def generate_url(root_path) folder = find_workspace_folder(root_path) path = "dev-container+#{root_path.unpack('H*')[0]}" print "vscode-remote://#{URI.encode_www_form_component(path)}#{folder}" end def find_workspace_folder(root_path) unless File.exist?("#{root_path}/.devcontainer/devcontainer.json") puts 'Not found devcontainer.json file.' return end config = JSON.parse(File.read("#{root_path}/.devcontainer/devcontainer.json")) config['workspaceFolder'] end def ghq_exists? !`which #{@bin_path}/ghq`.empty? end def search return unless ghq_exists? result = [] `#{@bin_path}/ghq list --full-path`.split(/\R/).each do |d| Dir.foreach(d) do |path| next if ['.', '..'].include?(path) file = File.join(d, path) result << d if file.include?('.devcontainer') end end result end end end