Class: Pronto::Golang

Inherits:
Runner
  • Object
show all
Includes:
Pronto::GolangSupport::FileFinder
Defined in:
lib/pronto/golang.rb

Constant Summary collapse

CONFIG_FILE =
'.golangtools.yml'
GOLANG_FILE_EXTENSIONS =
['.go'].freeze

Instance Method Summary collapse

Methods included from Pronto::GolangSupport::FileFinder

#find_file_upwards, #find_files_upwards, root_level=, root_level?

Instance Method Details

#available_toolsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pronto/golang.rb', line 81

def available_tools
  if @tools == nil
    config = dotconfig

    @tools = GolangTools.constants.sort.map do |constant|
      next if constant.to_s == 'Base'

      tool_class = Object.const_get("Pronto::GolangTools::#{constant}")

      tool = tool_class.new(config.fetch('tools').fetch(tool_class.base_command))

      if tool.available?
        tool
      else
        nil
      end
    end.compact
  end

  return @tools
end

#dotconfigObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pronto/golang.rb', line 103

def dotconfig
  file = find_file_upwards(CONFIG_FILE, Dir.pwd, use_home: true)

  base = {
    'tools' => {}
  }

  if file
    return base.merge(YAML.load_file(file))
  end

  return base
end

#go_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/pronto/golang.rb', line 117

def go_file?(path)
  GOLANG_FILE_EXTENSIONS.include?(File.extname(path))
end

#inspect(patch) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pronto/golang.rb', line 31

def inspect(patch)
  escaped_path = Shellwords.escape(patch.new_file_full_path.to_s)

  messages = []

  available_tools.each do |tool|
    Open3.popen3("#{tool.command(escaped_path)}") do |stdin, stdout, stderr, wait_thr|
      [stdout, stderr].each do |result_text|
        while output_line = result_text.gets
          next if output_line.strip == 'exit status 1'

          messages << process_line(patch, tool, output_line)
        end
      end



      while output_line = stderr.gets
        process_line(patch, tool, output_line)
      end
    end
  end

  return messages
end

#process_line(patch, tool, output_line) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pronto/golang.rb', line 57

def process_line(patch, tool, output_line)
  return nil if output_line =~ /^#/

  begin
    file_path, line_number, level, message = tool.parse_line(output_line)

    patch.added_lines.each do |line|
      if line_number.to_s == line.new_lineno.to_s &&
         patch.new_file_full_path.to_s == File.expand_path(file_path)

        prefix_message = "#{tool.base_command}: #{message}"

        return Message.new(
          file_path, line, level, prefix_message, line.commit_sha, self.class
        )
      end
    end
  rescue ::Pronto::GolangSupport::UnprocessableLine
    # Do nothing if the line is not processable
  end

  return nil
end

#runObject



17
18
19
20
21
22
23
24
25
# File 'lib/pronto/golang.rb', line 17

def run
  return [] unless @patches

  @patches
    .select { |patch| valid_patch?(patch) }
    .map { |patch| inspect(patch) }
    .flatten
    .compact
end

#valid_patch?(patch) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/pronto/golang.rb', line 27

def valid_patch?(patch)
  patch.additions > 0 && go_file?(patch.new_file_full_path)
end