Skip to content

Instantly share code, notes, and snippets.

@Slowhand0309
Last active Nov 7, 2020
Embed
What would you like to do?
Alfred Workflow: VSCode Remote Containers

Alfred Workflow: Search and Launch VSCode Remote Containers

gif

Install

VSCode Remote Containers CLI.alfredworkflow.zip

Set GHQ_PATH path

  1. Click 「Configure workflow and variables」

  1. Edit path

shell

This workflow uses zsh, but you can modify it to fit your shell.

# 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
require './cli.rb'
query = ARGV[0]
VSCodeRemoteContainer::Utility.new.generate_url(query)
query=$1
source ~/.zshrc
code --folder-uri $query
require './cli.rb'
def item(index, name)
<<ITEM
<item uid="#{index}" arg="#{name}">
<title>"#{File.basename(name)}"</title>
</item>
ITEM
end
targets = VSCodeRemoteContainer::Utility.new.search
items = targets.map.with_index { |t, i| item(i, t)}.join('')
puts <<EOS
<items>
#{items}</items>
EOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment