module Diffend::LocalContext::Host

Module responsible for building host information from local context

Constants

WINDOWS_REGEXP

Regexp that checks if we're running under Windows

Public Class Methods

call() click to toggle source

Build host information

@return [Hash]

# File lib/diffend/local_context/host.rb, line 19
def call
  uname = Etc.uname

  {
    'command' => command,
    'name' => uname[:nodename],
    'system' => {
      'machine' => uname[:machine],
      'name' => uname[:sysname],
      'release' => uname[:release],
      'version' => uname[:version]
    },
    'tags' => tags,
    'user' => Etc.getpwuid(Process.uid)&.name || ENV['USERNAME'],
    'pid' => Process.pid
  }.freeze
end

Private Class Methods

clean(str) click to toggle source

@param str [String] that we want to clean and truncate

# File lib/diffend/local_context/host.rb, line 91
def clean(str)
  str
    .dup
    .gsub(/[[:space:]]+/, ' ')
    .strip[0...255]
end
command() click to toggle source

Build host command information

@return [Hash]

# File lib/diffend/local_context/host.rb, line 42
def command
  if File.exist?($PROGRAM_NAME)
    if defined?(JRUBY_VERSION) || WINDOWS_REGEXP =~ RUBY_PLATFORM
      name = $PROGRAM_NAME.split('/').last.strip
      command = "#{name} #{ARGV.join(' ')}"
    else
      array = `ps -p #{Process.pid} -o command=`.strip.split(' ')
      array.shift if array.first.end_with?('bin/ruby')
      name = array.shift.split('/').last.strip
      command = "#{name} #{array.join(' ')}"
    end

    { 'name' => clean(command), 'title' => '' }
  else
    { 'name' => clean(ARGV.join(' ')), 'title' => clean($PROGRAM_NAME) }
  end
end
prepare_user_tags() click to toggle source

Prepare user tags

@return [Array]

# File lib/diffend/local_context/host.rb, line 82
def prepare_user_tags
  if ENV.key?('DIFFEND_TAGS')
    ENV['DIFFEND_TAGS'].split(',')
  else
    []
  end
end
tags() click to toggle source

Build host tags

@return [Array]

# File lib/diffend/local_context/host.rb, line 63
def tags
  tags = prepare_user_tags

  if ENV.key?('GITHUB_ACTIONS')
    tags << 'ci'
    tags << 'ci-github'
  end

  if ENV.key?('CIRCLECI')
    tags << 'ci'
    tags << 'ci-circle'
  end

  tags
end