From e93d6fbfb13c545c80bb4ccf93845532cbb02493 Mon Sep 17 00:00:00 2001 From: Feiran Yang Date: Wed, 16 Feb 2022 06:19:53 +0800 Subject: [PATCH] feat: warn user when 'pin remote add' while offline (#8621) * feat: add warning message when the local node is offline for remote pinning * feat: check is node is online * feat: add test in t0700-remotepin.sh * feat: use grep and fix the test * fix: print to stdout only when --enc=text This ensures we don't break JSON produced by --enc=json Co-authored-by: Marcin Rataj --- core/commands/pin/remotepin.go | 3 +++ test/sharness/t0700-remotepin.sh | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/core/commands/pin/remotepin.go b/core/commands/pin/remotepin.go index 3ca0d3532..0e4bf373e 100644 --- a/core/commands/pin/remotepin.go +++ b/core/commands/pin/remotepin.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "os" "sort" "strings" "text/tabwriter" @@ -185,6 +186,8 @@ NOTE: a comma-separated notation is supported in CLI for convenience: return err } opts = append(opts, pinclient.PinOpts.WithOrigins(addrs...)) + } else if isInBlockstore && !node.IsOnline && cmds.GetEncoding(req, cmds.Text) == cmds.Text { + fmt.Fprintf(os.Stdout, "WARNING: the local node is offline and remote pinning may fail if there is no other provider for this CID\n") } // Execute remote pin request diff --git a/test/sharness/t0700-remotepin.sh b/test/sharness/t0700-remotepin.sh index 538375841..2566c06d8 100755 --- a/test/sharness/t0700-remotepin.sh +++ b/test/sharness/t0700-remotepin.sh @@ -319,6 +319,14 @@ test_remote_pins() { test_remote_pins "" test_kill_ipfs_daemon + +WARNINGMESSAGE="WARNING: the local node is offline and remote pinning may fail if there is no other provider for this CID" + +test_expect_success "'ipfs pin remote add' shows the warning message while offline" ' + test_expect_code 0 ipfs pin remote add --service=test_pin_svc --background $BASE_ARGS --name=name_a $HASH_A > actual && + test_expect_code 0 grep -q "$WARNINGMESSAGE" actual +' + test_done # vim: ts=2 sw=2 sts=2 et: