From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id D2ADC15802E for ; Tue, 25 Jun 2024 04:06:59 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 4EFFEE2A57; Tue, 25 Jun 2024 04:06:58 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 302D5E2A59 for ; Tue, 25 Jun 2024 04:06:58 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3C12A33BEE8 for ; Tue, 25 Jun 2024 04:06:57 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id CAED01D4E for ; Tue, 25 Jun 2024 04:06:55 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1719278228.4cdb3c0869a9e6353fee69a7d61123ba8be01c06.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 4cdb3c0869a9e6353fee69a7d61123ba8be01c06 X-VCS-Branch: master Date: Tue, 25 Jun 2024 04:06:55 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 9055aaee-c946-4369-86d3-2876b3a0298b X-Archives-Hash: df159a31c25c3b30c894c862688ede47 commit: 4cdb3c0869a9e6353fee69a7d61123ba8be01c06 Author: Kerin Millar plushkava net> AuthorDate: Mon Jun 24 21:34:37 2024 +0000 Commit: Sam James gentoo org> CommitDate: Tue Jun 25 01:17:08 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=4cdb3c08 Add the fetch() function This function makes it trivial to issue a network request - such as HTTP GET - in a manner that is quiet and which yields a useful exit status value. It prefers curl if present but supports wget also. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/functions.sh b/functions.sh index 7e09611..9336174 100644 --- a/functions.sh +++ b/functions.sh @@ -47,6 +47,48 @@ chdir() CDPATH= cd -- "$@" } +# +# Considers the first parameter as an URL then attempts to fetch it with either +# curl(1) or wget(1). If the URL does not contain a scheme then the https:// +# scheme shall be presumed. Both utilities shall be invoked in a manner that +# suppresses all output unless an error occurs, and whereby HTTP redirections +# are honoured. Upon success, the body of the response shall be printed to the +# standard output. Otherwise, the return value shall be greater than 0. +# +fetch() +{ + if hash curl 2>/dev/null; then + fetch() + { + if [ "$#" -gt 0 ]; then + # Discard any extraneous parameters. + set -- "$1" + fi + curl -f -sS -L --connect-timeout 10 --proto-default https -- "$@" + } + elif hash wget 2>/dev/null; then + fetch() + { + if [ "$#" -gt 0 ]; then + # Discard any extraneous parameters. + case $1 in + ''|ftp://*|ftps://*|https://*) + set -- "$1" + ;; + *) + set -- "https://$1" + esac + fi + wget -nv -O - --connect-timeout 10 -- "$@" + } + else + warn "fetch: this function requires that curl or wget to be installed" + return 127 + fi + + fetch "$@" +} + # # Determines whether the current shell is a subprocess of portage. #