fix: `ipfs cid` without repo (#10897)
* fix: `ipfs cid format` without repo these commands should work without daemon or repo but were missing SetDoesNotUseRepo(true) * test: test/cli/commands_without_repo_test.go
Marcin Rataj committed
Aug 8, 2025 at 16:39 UTC
1c9fe273d944630e5fb3ca0e47a522aa0ea30c86
2 files changed
+138
-3
core/commands/cid.go
+8
-3
@@ -121,7 +121,8 @@ The optional format string is a printf style format string:
121
return ""
122
}),
123
},
124
- Type: CidFormatRes{},
124
+ Type: CidFormatRes{},
125
+ Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
126
}
127
128
type CidFormatRes struct {
@@ -151,6 +152,7 @@ Useful when processing third-party CIDs which could come with arbitrary formats.
152
},
153
PostRun: cidFmtCmd.PostRun,
154
Type: cidFmtCmd.Type,
155
+ Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
156
}
157
158
type cidFormatOpts struct {
@@ -309,7 +311,8 @@ var basesCmd = &cmds.Command{
311
return nil
312
}),
313
},
312
- Type: []CodeAndName{},
314
+ Type: []CodeAndName{},
315
+ Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
316
}
317
318
const (
@@ -369,7 +372,8 @@ var codecsCmd = &cmds.Command{
372
return nil
373
}),
374
},
372
- Type: []CodeAndName{},
375
+ Type: []CodeAndName{},
376
+ Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
377
}
378
379
var hashesCmd = &cmds.Command{
@@ -393,6 +397,7 @@ var hashesCmd = &cmds.Command{
397
},
398
Encoders: codecsCmd.Encoders,
399
Type: codecsCmd.Type,
400
+ Extra: CreateCmdExtras(SetDoesNotUseRepo(true)),
401
}
402
403
type multibaseSorter struct {
test/cli/commands_without_repo_test.go
new
+130
@@ -0,0 +1,130 @@
1
+package cli
2
+
3
+import (
4
+ "os"
5
+ "os/exec"
6
+ "strings"
7
+ "testing"
8
+)
9
+
10
+func TestCommandsWithoutRepo(t *testing.T) {
11
+ t.Run("cid", func(t *testing.T) {
12
+ t.Run("base32", func(t *testing.T) {
13
+ cmd := exec.Command("ipfs", "cid", "base32", "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
14
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
15
+ stdout, err := cmd.Output()
16
+ if err != nil {
17
+ t.Fatal(err)
18
+ }
19
+ expected := "bafybeibxm2nsadl3fnxv2sxcxmxaco2jl53wpeorjdzidjwf5aqdg7wa6u\n"
20
+ if string(stdout) != expected {
21
+ t.Fatalf("expected %q, got: %q", expected, stdout)
22
+ }
23
+ })
24
+
25
+ t.Run("format", func(t *testing.T) {
26
+ cmd := exec.Command("ipfs", "cid", "format", "-v", "1", "QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv")
27
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
28
+ stdout, err := cmd.Output()
29
+ if err != nil {
30
+ t.Fatal(err)
31
+ }
32
+ expected := "zdj7WZAAFKPvYPPzyJLso2hhxo8a7ZACFQ4DvvfrNXTHidofr\n"
33
+ if string(stdout) != expected {
34
+ t.Fatalf("expected %q, got: %q", expected, stdout)
35
+ }
36
+ })
37
+
38
+ t.Run("bases", func(t *testing.T) {
39
+ cmd := exec.Command("ipfs", "cid", "bases")
40
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
41
+ stdout, err := cmd.Output()
42
+ if err != nil {
43
+ t.Fatal(err)
44
+ }
45
+ if !strings.Contains(string(stdout), "base32") {
46
+ t.Fatalf("expected base32 in output, got: %s", stdout)
47
+ }
48
+ })
49
+
50
+ t.Run("codecs", func(t *testing.T) {
51
+ cmd := exec.Command("ipfs", "cid", "codecs")
52
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
53
+ stdout, err := cmd.Output()
54
+ if err != nil {
55
+ t.Fatal(err)
56
+ }
57
+ if !strings.Contains(string(stdout), "dag-pb") {
58
+ t.Fatalf("expected dag-pb in output, got: %s", stdout)
59
+ }
60
+ })
61
+
62
+ t.Run("hashes", func(t *testing.T) {
63
+ cmd := exec.Command("ipfs", "cid", "hashes")
64
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
65
+ stdout, err := cmd.Output()
66
+ if err != nil {
67
+ t.Fatal(err)
68
+ }
69
+ if !strings.Contains(string(stdout), "sha2-256") {
70
+ t.Fatalf("expected sha2-256 in output, got: %s", stdout)
71
+ }
72
+ })
73
+ })
74
+
75
+ t.Run("multibase", func(t *testing.T) {
76
+ t.Run("list", func(t *testing.T) {
77
+ cmd := exec.Command("ipfs", "multibase", "list")
78
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
79
+ stdout, err := cmd.Output()
80
+ if err != nil {
81
+ t.Fatal(err)
82
+ }
83
+ if !strings.Contains(string(stdout), "base32") {
84
+ t.Fatalf("expected base32 in output, got: %s", stdout)
85
+ }
86
+ })
87
+
88
+ t.Run("encode", func(t *testing.T) {
89
+ cmd := exec.Command("ipfs", "multibase", "encode", "-b", "base32")
90
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
91
+ cmd.Stdin = strings.NewReader("hello\n")
92
+ stdout, err := cmd.Output()
93
+ if err != nil {
94
+ t.Fatal(err)
95
+ }
96
+ expected := "bnbswy3dpbi"
97
+ if string(stdout) != expected {
98
+ t.Fatalf("expected %q, got: %q", expected, stdout)
99
+ }
100
+ })
101
+
102
+ t.Run("decode", func(t *testing.T) {
103
+ cmd := exec.Command("ipfs", "multibase", "decode")
104
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
105
+ cmd.Stdin = strings.NewReader("bnbswy3dpbi")
106
+ stdout, err := cmd.Output()
107
+ if err != nil {
108
+ t.Fatal(err)
109
+ }
110
+ expected := "hello\n"
111
+ if string(stdout) != expected {
112
+ t.Fatalf("expected %q, got: %q", expected, stdout)
113
+ }
114
+ })
115
+
116
+ t.Run("transcode", func(t *testing.T) {
117
+ cmd := exec.Command("ipfs", "multibase", "transcode", "-b", "base64")
118
+ cmd.Env = append(os.Environ(), "IPFS_PATH="+t.TempDir())
119
+ cmd.Stdin = strings.NewReader("bnbswy3dpbi")
120
+ stdout, err := cmd.Output()
121
+ if err != nil {
122
+ t.Fatal(err)
123
+ }
124
+ expected := "maGVsbG8K"
125
+ if string(stdout) != expected {
126
+ t.Fatalf("expected %q, got: %q", expected, stdout)
127
+ }
128
+ })
129
+ })
130
+}