Skip to content

Instantly share code, notes, and snippets.

@ivan3bx
Created February 27, 2019 14:26
Show Gist options
  • Save ivan3bx/24b41d0e74f21ddaf3fd51137edfa242 to your computer and use it in GitHub Desktop.
Save ivan3bx/24b41d0e74f21ddaf3fd51137edfa242 to your computer and use it in GitHub Desktop.
simple script to unpack a go distribution & link it into /usr/local
#!/usr/bin/env ruby
require 'pathname'
# Upgrade go on linux from binary distribution file
# 1. Will create /usr/local/go-<major>.<minor>.<patch>
# 2. Will create a symbolic link from /usr/local/go -> this version
def run
file = ARGV[0]
if file.nil? || !Pathname.new(file).exist?
puts 'filename required'
exit(1)
end
parts = file.match(/go(?<major>\d+)\.(?<minor>\d+)(\.(?<patch>\d+))?.linux/)
destination = "go-#{parts['major']}.#{parts['minor']}"
destination = "#{destination}.#{parts['patch']}" unless parts['patch'].nil?
if Pathname.new("/usr/local/#{destination}").exist?
puts 'destination already exists'
exit(1)
end
exec_command "sudo mkdir /usr/local/#{destination}"
exec_command "sudo tar -zxvf #{file} -C /usr/local/#{destination} --strip-components 1"
exec_command "cd /usr/local && sudo ln -nsf #{destination} go"
end
def exec_command(cmd)
puts cmd
`#{cmd}`
end
run
puts 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment