Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active December 26, 2015 17:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/b675a263897680e02fbd to your computer and use it in GitHub Desktop.
Save Integralist/b675a263897680e02fbd to your computer and use it in GitHub Desktop.
Go Guardfile: `bundle exec guard`
source "https://rubygems.org"
gem "guard"
gem "guard-shell"
gem "terminal-notifier" # brew upgrade terminal-notifier
gem "terminal-notifier-guard"
require "terminal-notifier-guard"
guard :shell do
watch(/(.*).go/) do |m|
puts "m: #{m}" # m: ["file-that-was-modified", "folder-file-sits-inside"]
issues = ""
`golint #{m.first}`.tap { |res| issues = res }
puts "\nissues:\n#{issues}" unless issues.empty?
`go run #{m.first}` if issues.empty?
end
end
@Integralist
Copy link
Author

Alternative that works with long running go programs (such as a web server)...

require "terminal-notifier-guard"

$thread_finished = false

def notify(t)
  $thread_finished = true
  t.kill
end

def pattern(str)
  "ps aux | grep '#{str}' | awk '{ print $2 }'"
end

def extract(pid)
  pid.split("\n").last.to_i
end

def cleanup(regex)
  pid   = `#{pattern regex}`
  pid   = extract pid
  `kill #{pid}`
end

guard :shell do
  watch(/(.*).go/) do |m|
    issues = ""

    `golint #{m.first}`.tap { |res| issues = res }

    puts "\nissues:\n#{issues}" unless issues.empty?

    Thread.new { `go run #{m.first}`; notify(Thread.current) } if issues.empty?

    seconds = 10 # threshold to wait for program to complete

    while (seconds > 0)
      break if $thread_finished
      puts "sleeping\n"
      sleep 1
      seconds -= 1
    end

    cleanup("[0-9] go run #{m.first}")
    cleanup("go-build")

    puts "Program finished\n"
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment