Skip to content

Commit 56a94bb

Browse files
authored
MacOS compatibility (#345)
* MacOS Compatibility GNU Utils Requires: brew install coreutils * MacOS getopt GNU Requires brew install gnu-getopt * Adds darwin binaries * more getopt
1 parent cc6f976 commit 56a94bb

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

dist/activate

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
KCTF_YQ_URL="https://github.com/mikefarah/yq/releases/download/v4.2.0/yq_linux_amd64"
16-
KCTF_YQ_HASH="5d44bd64e264e9029c5f06bcd960ba162d7ed7ddd1781f02a28d62f50577b632"
15+
if [[ "$OSTYPE" =~ ^darwin.* ]]; then
16+
KCTF_YQ_URL="https://github.com/mikefarah/yq/releases/download/v4.2.0/yq_darwin_amd64"
17+
KCTF_YQ_HASH="83b9dc96e75799e162035b2ee2dffc0c51de869c27a2e294eb0aee8653a19804"
1718

18-
KCTF_KIND_URL="https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64"
19-
KCTF_KIND_HASH="949f81b3c30ca03a3d4effdecda04f100fa3edc07a28b19400f72ede7c5f0491"
19+
KCTF_KIND_URL="https://kind.sigs.k8s.io/dl/v0.11.1/kind-darwin-amd64"
20+
KCTF_KIND_HASH="432bef555a70e9360b44661c759658265b9eaaf7f75f1beec4c4d1e6bbf97ce3"
2021

21-
KCTF_KUBECTL_URL="https://dl.k8s.io/release/v1.20.4/bin/linux/amd64/kubectl"
22-
KCTF_KUBECTL_HASH="98e8aea149b00f653beeb53d4bd27edda9e73b48fed156c4a0aa1dabe4b1794c"
22+
KCTF_KUBECTL_URL="https://dl.k8s.io/release/v1.20.4/bin/darwin/amd64/kubectl"
23+
KCTF_KUBECTL_HASH="37f593731b8c9913bf2a3bfa36dacb3058dc176c7aeae2930c783822ea03a573"
24+
25+
STAT="gstat"
26+
MKTEMP="gmktemp"
27+
else
28+
KCTF_YQ_URL="https://github.com/mikefarah/yq/releases/download/v4.2.0/yq_linux_amd64"
29+
KCTF_YQ_HASH="5d44bd64e264e9029c5f06bcd960ba162d7ed7ddd1781f02a28d62f50577b632"
30+
31+
KCTF_KIND_URL="https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64"
32+
KCTF_KIND_HASH="949f81b3c30ca03a3d4effdecda04f100fa3edc07a28b19400f72ede7c5f0491"
33+
34+
KCTF_KUBECTL_URL="https://dl.k8s.io/release/v1.20.4/bin/linux/amd64/kubectl"
35+
KCTF_KUBECTL_HASH="98e8aea149b00f653beeb53d4bd27edda9e73b48fed156c4a0aa1dabe4b1794c"
36+
37+
STAT="stat"
38+
MKTEMP="mktemp"
39+
fi
2340

2441
export KCTF_CTF_DIR="$(realpath --no-symlinks "$(dirname "${BASH_SOURCE-$0}")/..")"
2542
export KCTF_BIN="${KCTF_CTF_DIR}/kctf/bin"
@@ -30,14 +47,14 @@ function _kctf_check_umask {
3047
_kctf_log_err "umask is too prohibitive. Please set it to 022 when using kctf"
3148
return 1
3249
fi
33-
if [[ "$(stat "${KCTF_BIN}/kctf-cluster" --format '%a')" -ne "755" ]]; then
50+
if [[ "$(${STAT} "${KCTF_BIN}/kctf-cluster" --format '%a')" -ne "755" ]]; then
3451
_kctf_log_err "${KCTF_BIN}/kctf-cluster has unexpected permissions. Maybe a umask problem during checkout?"
3552
return 1
3653
fi
3754
}
3855

3956
function _kctf_setup_environment {
40-
KCTF_CONFIG_DIR="$(mktemp -d --tmpdir kctf.XXXXXXXXXX)"
57+
KCTF_CONFIG_DIR="$(${MKTEMP} -d --tmpdir kctf.XXXXXXXXXX)"
4158
if [[ $? -ne 0 ]]; then
4259
return 1
4360
fi

dist/bin/kctf-challenge

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
source "${KCTF_BIN}/kctf-log"
1717

18+
if [[ "$OSTYPE" =~ ^darwin.* ]]; then
19+
GETOPT="$(brew --prefix gnu-getopt)/bin/getopt"
20+
else
21+
GETOPT="getopt"
22+
fi
23+
1824
function has_cluster_config {
1925
[[ ! -z "${CLUSTER_NAME-}" ]]
2026
}
@@ -45,7 +51,7 @@ function parse_help_arg_only_usage {
4551
function parse_help_arg_only {
4652
OPTS="h"
4753
LONGOPTS="help"
48-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
54+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
4955
if [[ $? -ne 0 ]]; then
5056
parse_help_arg_only_usage
5157
exit 1
@@ -82,7 +88,7 @@ function parse_container_name_usage {
8288
function parse_container_name {
8389
OPTS="h"
8490
LONGOPTS="help,container:"
85-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
91+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
8692
if [[ $? -ne 0 ]]; then
8793
parse_container_name_usage
8894
exit 1
@@ -245,7 +251,7 @@ function kctf_chal_debug_logs {
245251

246252
OPTS="h"
247253
LONGOPTS="help,container:,tail:"
248-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
254+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal ${COMMAND}" -- "$@")
249255
if [[ $? -ne 0 ]]; then
250256
kctf_chal_debug_logs_usage
251257
exit 1
@@ -346,7 +352,7 @@ function kctf_chal_debug_port_forward {
346352

347353
OPTS="h"
348354
LONGOPTS="help,challenge-name:,port:,local-port:"
349-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal debug port-forward" -- "$@")
355+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal debug port-forward" -- "$@")
350356
if [[ $? -ne 0 ]]; then
351357
kctf_chal_debug_port_forward_usage
352358
exit 1
@@ -462,7 +468,7 @@ function kctf_chal_create_usage {
462468
function kctf_chal_create {
463469
OPTS="h"
464470
LONGOPTS="help,template:,challenge-dir:"
465-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal create" -- "$@")
471+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal create" -- "$@")
466472
if [[ $? -ne 0 ]]; then
467473
kctf_chal_create_usage
468474
exit 1

dist/bin/kctf-cluster

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ KCTF_CLOUD_BASE_URL="https://kctf-cloud.appspot.com/v1"
2020
# owned by kctf-cloud
2121
KCTF_CLOUD_API_KEY="AIzaSyC7Jgu4e0IygmImZNPmJHrcfZ3lJA9ZrZs"
2222

23+
if [[ "$OSTYPE" =~ ^darwin.* ]]; then
24+
GETOPT="$(brew --prefix gnu-getopt)/bin/getopt"
25+
else
26+
GETOPT="getopt"
27+
fi
28+
2329
function update_gcloud_config {
2430
_kctf_log "Updating gcloud config."
2531
ACTIVE_ACCOUNT="$(CLOUDSDK_ACTIVE_CONFIG_NAME= gcloud config get-value core/account 2>/dev/null)"
@@ -146,7 +152,7 @@ function kctf_cluster_create {
146152

147153
OPTS="h"
148154
LONGOPTS="help,type:,project:,zone:,registry:,cluster-name:,domain-name:,email-address:,start"
149-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf cluster create" -- "$@")
155+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf cluster create" -- "$@")
150156
if [[ $? -ne 0 ]]; then
151157
kctf_cluster_create_usage
152158
return 1
@@ -344,7 +350,7 @@ function kctf_cluster_ip_ranges {
344350

345351
OPTS="h"
346352
LONGOPTS="help"
347-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf cluster set-src-ip-ranges" -- "$@")
353+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf cluster set-src-ip-ranges" -- "$@")
348354
if [[ $? -ne 0 ]]; then
349355
kctf_cluster_ip_ranges_usage
350356
return 1
@@ -742,7 +748,7 @@ function kctf_cluster_resize {
742748

743749
OPTS="h"
744750
LONGOPTS="help,machine-type:,min-nodes:,max-nodes:,num-nodes:,pool-name:,old-pool"
745-
PARSED=$(getopt --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal create" -- "$@")
751+
PARSED=$(${GETOPT} --options=$OPTS --longoptions=$LONGOPTS --name "kctf chal create" -- "$@")
746752
if [[ $? -ne 0 ]]; then
747753
kctf_cluster_resize_usage
748754
exit 1

0 commit comments

Comments
 (0)