Class: Zwischen::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/zwischen/installer.rb

Constant Summary collapse

ZWISCHEN_BIN_DIR =
File.expand_path("~/.zwischen/bin")
GITLEAKS_REPO =
"gitleaks/gitleaks"
PLATFORMS =
{
  darwin: "macos",
  linux: "linux",
  mingw: "windows",
  mswin: "windows"
}.freeze
GITLEAKS_PLATFORMS =

Map Ruby platform to gitleaks release naming

{
  "linux" => "linux",
  "macos" => "darwin"
}.freeze
GITLEAKS_ARCHS =
{
  "x86_64" => "x64",
  "amd64" => "x64",
  "aarch64" => "arm64",
  "arm64" => "arm64"
}.freeze
INSTALL_COMMANDS =
{
  gitleaks: {
    macos: {
      brew: "brew install gitleaks",
      manual: "Visit https://github.com/gitleaks/gitleaks/releases"
    },
    linux: {
      brew: "brew install gitleaks",
      manual: "Visit https://github.com/gitleaks/gitleaks/releases"
    },
    windows: {
      manual: "Visit https://github.com/gitleaks/gitleaks/releases"
    }
  },
  semgrep: {
    macos: {
      brew: "brew install semgrep",
      pip: "pip install semgrep",
      manual: "Visit https://semgrep.dev/docs/getting-started/"
    },
    linux: {
      pip: "pip install semgrep",
      pipx: "pipx install semgrep",
      manual: "Visit https://semgrep.dev/docs/getting-started/"
    },
    windows: {
      pip: "pip install semgrep",
      manual: "Visit https://semgrep.dev/docs/getting-started/"
    }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.install_commands(tool, platform = nil) ⇒ Object



70
71
72
# File 'lib/zwischen/installer.rb', line 70

def self.install_commands(tool, platform = nil)
  new.install_commands(tool, platform)
end

.platformObject



66
67
68
# File 'lib/zwischen/installer.rb', line 66

def self.platform
  new.platform
end

Instance Method Details

#auto_install_gitleaksObject

Auto-install gitleaks binary if not present



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zwischen/installer.rb', line 125

def auto_install_gitleaks
  return true if gitleaks_available?

  FileUtils.mkdir_p(ZWISCHEN_BIN_DIR)

  release = fetch_latest_gitleaks_release
  return false unless release

  asset = find_gitleaks_asset(release)
  return false unless asset

  download_and_extract_gitleaks(asset)
end

#check_tool(tool_name) ⇒ Object



109
110
111
# File 'lib/zwischen/installer.rb', line 109

def check_tool(tool_name)
  system("which", tool_name, out: File::NULL, err: File::NULL)
end

#get_version(tool_name) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/zwischen/installer.rb', line 113

def get_version(tool_name)
  return nil unless check_tool(tool_name)

  stdout, _stderr, status = Open3.capture3(tool_name, "--version")
  return nil unless status.success?

  stdout.strip.split("\n").first
rescue StandardError
  nil
end

#gitleaks_available?Boolean

Check if gitleaks is available (local or system)

Returns:

  • (Boolean)


140
141
142
# File 'lib/zwischen/installer.rb', line 140

def gitleaks_available?
  !gitleaks_path.nil?
end

#gitleaks_pathObject

Get path to gitleaks executable (local install or system)



145
146
147
148
149
150
# File 'lib/zwischen/installer.rb', line 145

def gitleaks_path
  local = File.join(ZWISCHEN_BIN_DIR, "gitleaks")
  return local if File.executable?(local)

  check_tool("gitleaks") ? "gitleaks" : nil
end

#install_commands(tool, platform = nil) ⇒ Object



88
89
90
91
# File 'lib/zwischen/installer.rb', line 88

def install_commands(tool, platform = nil)
  platform ||= self.platform
  INSTALL_COMMANDS.dig(tool.to_sym, platform.to_sym) || {}
end

#platformObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zwischen/installer.rb', line 74

def platform
  host_os = RbConfig::CONFIG["host_os"].downcase
  case host_os
  when /darwin/
    "macos"
  when /linux/
    "linux"
  when /mingw|mswin/
    "windows"
  else
    "unknown"
  end
end

#preferred_command(tool, platform = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zwischen/installer.rb', line 93

def preferred_command(tool, platform = nil)
  platform ||= self.platform
  commands = install_commands(tool, platform)

  # Prefer brew on macOS, pip on Linux
  if platform == "macos" && commands[:brew]
    commands[:brew]
  elsif commands[:pip]
    commands[:pip]
  elsif commands[:brew]
    commands[:brew]
  else
    commands[:manual]
  end
end