diff --git a/core/commands/pin.go b/core/commands/pin.go index 7bf3beb8cdc..bfe523d072c 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -47,6 +47,7 @@ type AddPinOutput struct { } const ( + pinForceOptionName = "force" pinRecursiveOptionName = "recursive" pinProgressOptionName = "progress" ) @@ -193,6 +194,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) cmdkit.StringArg("ipfs-path", true, true, "Path to object(s) to be unpinned.").EnableStdin(), }, Options: []cmdkit.Option{ + cmdkit.BoolOption(pinForceOptionName, "f", "Ignore nonexistent pins"), cmdkit.BoolOption(pinRecursiveOptionName, "r", "Recursively unpin the object linked to by the specified object(s).").WithDefault(true), }, Type: PinOutput{}, @@ -216,7 +218,18 @@ collected if needed. (By default, recursively. Use -r=false for direct pins.) return } + force, _, err := req.Option(pinForceOptionName).Bool() + if err != nil { + res.SetError(err, cmdkit.ErrNormal) + return + } + removed, err := corerepo.Unpin(n, api, req.Context(), req.Arguments(), recursive) + if force && err == pin.ErrNotPinned { + res.SetOutput(nil) + return + } + if err != nil { res.SetError(err, cmdkit.ErrNormal) return diff --git a/test/sharness/t0085-pins.sh b/test/sharness/t0085-pins.sh index bdc285edb66..d407898770b 100755 --- a/test/sharness/t0085-pins.sh +++ b/test/sharness/t0085-pins.sh @@ -8,6 +8,7 @@ test_description="Test ipfs pinning operations" . lib/test-lib.sh +RANDOM_HASH=Qme8uX5n9hn15pw9p6WcVKoziyyC9LXv4LEgvsmKMULjnV test_pins() { EXTRA_ARGS=$1 @@ -19,7 +20,8 @@ test_pins() { HASH_D=$(echo "D" | ipfs add -q --pin=false) && HASH_E=$(echo "E" | ipfs add -q --pin=false) && HASH_F=$(echo "F" | ipfs add -q --pin=false) && - HASH_G=$(echo "G" | ipfs add -q --pin=false) + HASH_G=$(echo "G" | ipfs add -q --pin=false) && + HASH_TEST_UNPIN=$(echo "unpin" | ipfs add -q) ' test_expect_success "put all those hashes in a file" ' @@ -56,6 +58,10 @@ test_pins() { cat hashes | ipfs pin rm ' + test_expect_success "unpin non-existent hashes with force option" ' + ipfs pin rm --force $RANDOM_HASH $HASH_TEST_UNPIN $RANDOM_HASH + ' + test_expect_success "test pin update" ' ipfs pin add "$HASH_A" && ipfs pin ls > before_update && @@ -69,8 +75,6 @@ test_pins() { ' } -RANDOM_HASH=Qme8uX5n9hn15pw9p6WcVKoziyyC9LXv4LEgvsmKMULjnV - test_pins_error_reporting() { EXTRA_ARGS=$1