Open VS Code Remote Containers directly from the CLI on Mac

How it works

After opening VS Code Remote Containers once, go to cat ~ / Library / Application \ Support / Code / storage.json If you look at windowsState> lastActiveWindow> folder, you can see that it looks like the following.

    "windowsState": {
        "lastActiveWindow": {
            "folder": "vscode-remote://dev-container%2B2f557365722f6e616d652f646576656c6f702f70726f6a656374/usr/src",
            "backupPath": "...",
            "remoteAuthority": "dev-container+2f557365722f6e616d652f646576656c6f702f70726f6a656374",
            "uiState": {
                "mode": 1,
                "x": 0,
                "y": 23,
                "width": 3440,
                "height": 1417
            }
        },
        "openedWindows": []
    },

If you know the URL of this folder

$ code --folder-uri vscode-remote://dev-container%2B2f557365722f6e616d652f646576656c6f702f70726f6a656374/usr/src

It is possible to launch directly from the CLI in this way.

The part of 2f557365722f6e616d652f646576656c6f702f70726f6a656374 is hexadecimal string. Decoding this will result in / User / name / develop / project. You can also see that it is URL-encoded because it contains % 2B. The final / usr / src will be the path specified by workspaceFolder in devcontainer.json. Based on these, as the whole URL component

"vscode-remote://" + URI.encode_www_form_component("dev-container+" + "/User/name/develop/project".unpack('H*')) + "/usr/src"

It seems that it is.

Script creation

I will write a script that will generate a URL to start VS Code Remote Containers after specifying the path. It is generated by reading workspaceFolder of .devcontainer / devcontainer.json under the specified path.

main.rb


# frozen_string_literal: true
# !/usr/bin/env ruby
require 'json'

module VSCodeRemoteContainer
  class Utility
    def initialize
    end

    def generate_url(root_path)
      folder = find_workspace_folder(root_path)
      path = "dev-container+#{root_path.unpack('H*')[0]}"
      puts "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
  end
end

VSCodeRemoteContainer::Utility.new.generate_url(*ARGV)

Save the above as main.rb and execute as below to generate the URL

$ ruby main.rb '/User/name/xxxx'
# => vscode-remote://dev-container%2B2f557365722f6e616d652f646576656c6f702f70726f6a656374/usr/src

Creating Alfred Workflows

This time, I created Workflows that work in combination with ghq. VS Code is started directly in the container by specifying the repository name as ↓ as an operation image.

I added it to the above script when creating Workflows.

module VSCodeRemoteContainer
  class Utility
    attr_accessor :bin_path
    def initialize
      @bin_path = ENV['GHQ_PATH'] || '/usr/local/bin'
    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

What I'm doing is using ghq to get a list of the target repositories and I try to return only the repositories that have the .devcontainer directory.

The finished product is https://gist.github.com/Slowhand0309/253bb296cd7acb089601d2b32da4723b I put it here. I just made it for the time being, so if you find any problems We would appreciate it if you could contact us.

Recommended Posts

Open VS Code Remote Containers directly from the CLI on Mac
Hello World on Mac VS Code Java
[Ruby on Rails] Automatically enter the address from the zip code
Run VS Code on Docker
How to open a script file from Ubuntu with VS code
Run the AWS CLI on Docker
Java build with mac vs code
Switch from Eclipse to VS Code
Java development environment (Mac, VS Code)
Check when the container cannot be accessed from Host in the Laravel on docker environment using VS Code's Remote container.