Skip to content

Commit 934f9bd

Browse files
committed
refactor(download): move hash-related functions to seperate lib file, update imports
1 parent 08d22f2 commit 934f9bd

File tree

8 files changed

+36
-43
lines changed

8 files changed

+36
-43
lines changed

bin/checkhashes.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ param(
4242

4343
. "$PSScriptRoot\..\lib\core.ps1"
4444
. "$PSScriptRoot\..\lib\manifest.ps1"
45+
. "$PSScriptRoot\..\lib\hash.ps1" # 'get_hash'
4546
. "$PSScriptRoot\..\lib\buckets.ps1"
4647
. "$PSScriptRoot\..\lib\autoupdate.ps1"
4748
. "$PSScriptRoot\..\lib\json.ps1"

lib/autoupdate.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Must included with 'json.ps1'
22

3+
. "$PSScriptRoot\..\lib\hash.ps1" # 'get_hash'
4+
35
function format_hash([String] $hash) {
46
$hash = $hash.toLower()
57

lib/download.ps1

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Description: Functions for downloading files
22

33
. "$PSScriptRoot\..\lib\core.ps1"
4+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url'
45
. "$PSScriptRoot\..\lib\virustotal.ps1"
56

67
## Meta downloader
@@ -733,21 +734,6 @@ function url_remote_filename($url) {
733734
return $basename
734735
}
735736

736-
### Hash-related functions
737-
738-
function hash_for_url($manifest, $url, $arch) {
739-
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
740-
741-
if ($hashes.length -eq 0) { return $null }
742-
743-
$urls = @(script:url $manifest $arch)
744-
745-
$index = [array]::IndexOf($urls, $url)
746-
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
747-
748-
@($hashes)[$index]
749-
}
750-
751737
function check_hash($file, $hash, $app_name) {
752738
# returns (ok, err)
753739
if (!$hash) {
@@ -783,19 +769,5 @@ function check_hash($file, $hash, $app_name) {
783769
return $true, $null
784770
}
785771

786-
function get_hash([String] $multihash) {
787-
$type, $hash = $multihash -split ':'
788-
if (!$hash) {
789-
# no type specified, assume sha256
790-
$type, $hash = 'sha256', $multihash
791-
}
792-
793-
if (@('md5', 'sha1', 'sha256', 'sha512') -notcontains $type) {
794-
return $null, "Hash type '$type' isn't supported."
795-
}
796-
797-
return $type, $hash.ToLower()
798-
}
799-
800772
# Setup proxy globally
801773
setup_proxy

lib/hash.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### Hash-related functions
2+
3+
function hash_for_url($manifest, $url, $arch) {
4+
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
5+
6+
if ($hashes.length -eq 0) { return $null }
7+
8+
$urls = @(script:url $manifest $arch)
9+
10+
$index = [array]::IndexOf($urls, $url)
11+
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
12+
13+
@($hashes)[$index]
14+
}
15+
16+
function get_hash([String] $multihash) {
17+
$type, $hash = $multihash -split ':'
18+
if (!$hash) {
19+
# no type specified, assume sha256
20+
$type, $hash = 'sha256', $multihash
21+
}
22+
23+
if (@('md5', 'sha1', 'sha256', 'sha512') -notcontains $type) {
24+
return $null, "Hash type '$type' isn't supported."
25+
}
26+
27+
return $type, $hash.ToLower()
28+
}

lib/install.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
function nightly_version($quiet = $false) {
23
if (!$quiet) {
34
warn "This is a nightly version. Downloaded files won't be verified."

lib/virustotal.ps1

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
. "$PSScriptRoot\json.ps1" # 'json_path'
2-
# . "$PSScriptRoot\download.ps1" # 'hash_for_url'
2+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url'
33

44
# Error codes
55
$script:_ERR_UNSAFE = 2
@@ -314,16 +314,3 @@ function virustotal_check_app($app, $manifest, $architecture, $api_key, $scan) {
314314
Check-VirusTotalUrl $app $url $hash $api_key $scan
315315
}
316316
}
317-
318-
function hash_for_url($manifest, $url, $arch) {
319-
$hashes = @(hash $manifest $arch) | Where-Object { $_ -ne $null }
320-
321-
if ($hashes.length -eq 0) { return $null }
322-
323-
$urls = @(script:url $manifest $arch)
324-
325-
$index = [array]::IndexOf($urls, $url)
326-
if ($index -eq -1) { abort "Couldn't find hash in manifest for '$url'." }
327-
328-
@($hashes)[$index]
329-
}

libexec/scoop-download.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
. "$PSScriptRoot\..\lib\getopt.ps1"
2323
. "$PSScriptRoot\..\lib\json.ps1" # 'autoupdate.ps1' (indirectly)
24+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url'
2425
. "$PSScriptRoot\..\lib\autoupdate.ps1" # 'generate_user_manifest' (indirectly)
2526
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
2627
. "$PSScriptRoot\..\lib\manifest.ps1" # 'generate_user_manifest' 'Get-Manifest'

libexec/scoop-update.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
. "$PSScriptRoot\..\lib\psmodules.ps1"
2323
. "$PSScriptRoot\..\lib\decompress.ps1"
2424
. "$PSScriptRoot\..\lib\manifest.ps1"
25+
. "$PSScriptRoot\..\lib\hash.ps1" # 'hash_for_url'
2526
. "$PSScriptRoot\..\lib\versions.ps1"
2627
. "$PSScriptRoot\..\lib\depends.ps1"
2728
. "$PSScriptRoot\..\lib\install.ps1"

0 commit comments

Comments
 (0)