repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

nushell/modules/package/extractor.nu

Raw
# extracts various types of archives

export def unpack [
	archive: path
	target: path
] {
	# FIXME: maybe it is unnecessary to give type?
	# let extension = $archive | path parse | get extension
	# let type = match $extension {
	# 	'gz' => 'gzip'
	# 	'bz' => 'bzip2'
	# 	'xz' => 'xz'
	# 	_ => { error make { msg: $"Unsupported extension type ($extension)!" } }
	# }
	# run-external tar '--extract' $"--($type)" $"--file=($archive)" $"--directory=($target)" '--strip-components=1'

	# TODO: test on win32
	let is_single_dir = (run-external tar '--list' $'--file=($archive)' | lines | path split | first | uniq | length) == 1
	let strip = if $is_single_dir { 1 } else { 0 }
	run-external tar '--extract' $"--file=($archive)" $"--directory=($target)" $'--strip-components=($strip)'
}