|
| 1 | +package shell |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + |
| 7 | + files "github.com/ipfs/go-ipfs-files" |
| 8 | +) |
| 9 | + |
| 10 | +type FilesOpt func(*RequestBuilder) error |
| 11 | + |
| 12 | +type MfsLsEntry struct { |
| 13 | + Name string |
| 14 | + Type uint8 |
| 15 | + Size uint64 |
| 16 | + Hash string |
| 17 | +} |
| 18 | + |
| 19 | +type FilesStatObject struct { |
| 20 | + Blocks int |
| 21 | + CumulativeSize uint64 |
| 22 | + Hash string |
| 23 | + Local bool |
| 24 | + Size uint64 |
| 25 | + SizeLocal uint64 |
| 26 | + Type string |
| 27 | + WithLocality bool |
| 28 | +} |
| 29 | + |
| 30 | +type filesLsOutput struct { |
| 31 | + Entries []*MfsLsEntry |
| 32 | +} |
| 33 | + |
| 34 | +type filesFlushOutput struct { |
| 35 | + Cid string |
| 36 | +} |
| 37 | + |
| 38 | +type filesLs struct{} |
| 39 | +type filesChcid struct{} |
| 40 | +type filesMkdir struct{} |
| 41 | +type filesRead struct{} |
| 42 | +type filesWrite struct{} |
| 43 | +type filesStat struct{} |
| 44 | + |
| 45 | +var ( |
| 46 | + FilesLs filesLs |
| 47 | + FilesChcid filesChcid |
| 48 | + FilesMkdir filesMkdir |
| 49 | + FilesRead filesRead |
| 50 | + FilesWrite filesWrite |
| 51 | + FilesStat filesStat |
| 52 | +) |
| 53 | + |
| 54 | +// Stat use long listing format |
| 55 | +func (filesLs) Stat(long bool) FilesOpt { |
| 56 | + return func(rb *RequestBuilder) error { |
| 57 | + rb.Option("long", long) |
| 58 | + return nil |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// CidVersion cid version to use. (experimental) |
| 63 | +func (filesChcid) CidVersion(version int) FilesOpt { |
| 64 | + return func(rb *RequestBuilder) error { |
| 65 | + rb.Option("cid-version", version) |
| 66 | + return nil |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Hash hash function to use. Will set Cid version to 1 if used. (experimental) |
| 71 | +func (filesChcid) Hash(hash string) FilesOpt { |
| 72 | + return func(rb *RequestBuilder) error { |
| 73 | + rb.Option("hash", hash) |
| 74 | + return nil |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Parents no error if existing, make parent directories as needed |
| 79 | +func (filesMkdir) Parents(parents bool) FilesOpt { |
| 80 | + return func(rb *RequestBuilder) error { |
| 81 | + rb.Option("parents", parents) |
| 82 | + return nil |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// CidVersion cid version to use. (experimental) |
| 87 | +func (filesMkdir) CidVersion(version int) FilesOpt { |
| 88 | + return func(rb *RequestBuilder) error { |
| 89 | + rb.Option("cid-version", version) |
| 90 | + return nil |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Hash hash function to use. Will set Cid version to 1 if used. (experimental) |
| 95 | +func (filesMkdir) Hash(hash string) FilesOpt { |
| 96 | + return func(rb *RequestBuilder) error { |
| 97 | + rb.Option("hash", hash) |
| 98 | + return nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// Offset byte offset to begin reading from |
| 103 | +func (filesRead) Offset(offset int64) FilesOpt { |
| 104 | + return func(rb *RequestBuilder) error { |
| 105 | + rb.Option("offset", offset) |
| 106 | + return nil |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Count maximum number of bytes to read |
| 111 | +func (filesRead) Count(count int64) FilesOpt { |
| 112 | + return func(rb *RequestBuilder) error { |
| 113 | + rb.Option("count", count) |
| 114 | + return nil |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Hash print only hash. Implies '--format=<hash>'. Conflicts with other format options. |
| 119 | +func (filesStat) Hash(hash bool) FilesOpt { |
| 120 | + return func(rb *RequestBuilder) error { |
| 121 | + rb.Option("hash", hash) |
| 122 | + return nil |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Size print only size. Implies '--format=<cumulsize>'. Conflicts with other format options. |
| 127 | +func (filesStat) Size(size bool) FilesOpt { |
| 128 | + return func(rb *RequestBuilder) error { |
| 129 | + rb.Option("size", size) |
| 130 | + return nil |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// WithLocal compute the amount of the dag that is local, and if possible the total size. |
| 135 | +func (filesStat) WithLocal(withLocal bool) FilesOpt { |
| 136 | + return func(rb *RequestBuilder) error { |
| 137 | + rb.Option("with-local", withLocal) |
| 138 | + return nil |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// Offset byte offset to begin writing at |
| 143 | +func (filesWrite) Offset(offset int64) FilesOpt { |
| 144 | + return func(rb *RequestBuilder) error { |
| 145 | + rb.Option("offset", offset) |
| 146 | + return nil |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// Create create the file if it does not exist |
| 151 | +func (filesWrite) Create(create bool) FilesOpt { |
| 152 | + return func(rb *RequestBuilder) error { |
| 153 | + rb.Option("create", create) |
| 154 | + return nil |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Parents make parent directories as needed |
| 159 | +func (filesWrite) Parents(parents bool) FilesOpt { |
| 160 | + return func(rb *RequestBuilder) error { |
| 161 | + rb.Option("parents", parents) |
| 162 | + return nil |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// Truncate truncate the file to size zero before writing |
| 167 | +func (filesWrite) Truncate(truncate bool) FilesOpt { |
| 168 | + return func(rb *RequestBuilder) error { |
| 169 | + rb.Option("truncate", truncate) |
| 170 | + return nil |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// Count maximum number of bytes to write |
| 175 | +func (filesWrite) Count(count int64) FilesOpt { |
| 176 | + return func(rb *RequestBuilder) error { |
| 177 | + rb.Option("count", count) |
| 178 | + return nil |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// RawLeaves use raw blocks for newly created leaf nodes. (experimental) |
| 183 | +func (filesWrite) RawLeaves(rawLeaves bool) FilesOpt { |
| 184 | + return func(rb *RequestBuilder) error { |
| 185 | + rb.Option("raw-leaves", rawLeaves) |
| 186 | + return nil |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +// CidVersion cid version to use. (experimental) |
| 191 | +func (filesWrite) CidVersion(version int) FilesOpt { |
| 192 | + return func(rb *RequestBuilder) error { |
| 193 | + rb.Option("cid-version", version) |
| 194 | + return nil |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +// Hash hash function to use. Will set Cid version to 1 if used. (experimental) |
| 199 | +func (filesWrite) Hash(hash string) FilesOpt { |
| 200 | + return func(rb *RequestBuilder) error { |
| 201 | + rb.Option("hash", hash) |
| 202 | + return nil |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +// FilesChcid change the cid version or hash function of the root node of a given path |
| 207 | +func (s *Shell) FilesChcid(ctx context.Context, path string, options ...FilesOpt) error { |
| 208 | + if len(path) == 0 { |
| 209 | + path = "/" |
| 210 | + } |
| 211 | + |
| 212 | + rb := s.Request("files/chcid", path) |
| 213 | + for _, opt := range options { |
| 214 | + if err := opt(rb); err != nil { |
| 215 | + return err |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return rb.Exec(ctx, nil) |
| 220 | +} |
| 221 | + |
| 222 | +// FilesCp copy any IPFS files and directories into MFS (or copy within MFS) |
| 223 | +func (s *Shell) FilesCp(ctx context.Context, src string, dest string) error { |
| 224 | + return s.Request("files/cp", src, dest).Exec(ctx, nil) |
| 225 | +} |
| 226 | + |
| 227 | +// FilesFlush flush a given path's data to disk |
| 228 | +func (s *Shell) FilesFlush(ctx context.Context, path string) (string, error) { |
| 229 | + if len(path) == 0 { |
| 230 | + path = "/" |
| 231 | + } |
| 232 | + out := &filesFlushOutput{} |
| 233 | + if err := s.Request("files/flush", path). |
| 234 | + Exec(ctx, out); err != nil { |
| 235 | + return "", err |
| 236 | + } |
| 237 | + |
| 238 | + return out.Cid, nil |
| 239 | +} |
| 240 | + |
| 241 | +// FilesLs list directories in the local mutable namespace |
| 242 | +func (s *Shell) FilesLs(ctx context.Context, path string, options ...FilesOpt) ([]*MfsLsEntry, error) { |
| 243 | + if len(path) == 0 { |
| 244 | + path = "/" |
| 245 | + } |
| 246 | + |
| 247 | + var out filesLsOutput |
| 248 | + rb := s.Request("files/ls", path) |
| 249 | + for _, opt := range options { |
| 250 | + if err := opt(rb); err != nil { |
| 251 | + return nil, err |
| 252 | + } |
| 253 | + } |
| 254 | + if err := rb.Exec(ctx, &out); err != nil { |
| 255 | + return nil, err |
| 256 | + } |
| 257 | + return out.Entries, nil |
| 258 | +} |
| 259 | + |
| 260 | +// FilesMkdir make directories |
| 261 | +func (s *Shell) FilesMkdir(ctx context.Context, path string, options ...FilesOpt) error { |
| 262 | + rb := s.Request("files/mkdir", path) |
| 263 | + for _, opt := range options { |
| 264 | + if err := opt(rb); err != nil { |
| 265 | + return err |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + return rb.Exec(ctx, nil) |
| 270 | +} |
| 271 | + |
| 272 | +// FilesMv move files |
| 273 | +func (s *Shell) FilesMv(ctx context.Context, src string, dest string) error { |
| 274 | + return s.Request("files/mv", src, dest).Exec(ctx, nil) |
| 275 | +} |
| 276 | + |
| 277 | +// FilesRead read a file in a given MFS |
| 278 | +func (s *Shell) FilesRead(ctx context.Context, path string, options ...FilesOpt) (io.ReadCloser, error) { |
| 279 | + rb := s.Request("files/read", path) |
| 280 | + for _, opt := range options { |
| 281 | + if err := opt(rb); err != nil { |
| 282 | + return nil, err |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + resp, err := rb.Send(ctx) |
| 287 | + if err != nil { |
| 288 | + return nil, err |
| 289 | + } |
| 290 | + if resp.Error != nil { |
| 291 | + return nil, resp.Error |
| 292 | + } |
| 293 | + |
| 294 | + return resp.Output, nil |
| 295 | +} |
| 296 | + |
| 297 | +// FilesRm remove a file |
| 298 | +func (s *Shell) FilesRm(ctx context.Context, path string, force bool) error { |
| 299 | + return s.Request("files/rm", path). |
| 300 | + Option("force", force). |
| 301 | + Exec(ctx, nil) |
| 302 | +} |
| 303 | + |
| 304 | +// FilesStat display file status |
| 305 | +func (s *Shell) FilesStat(ctx context.Context, path string, options ...FilesOpt) (*FilesStatObject, error) { |
| 306 | + out := &FilesStatObject{} |
| 307 | + |
| 308 | + rb := s.Request("files/stat", path) |
| 309 | + for _, opt := range options { |
| 310 | + if err := opt(rb); err != nil { |
| 311 | + return nil, err |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + if err := rb.Exec(ctx, out); err != nil { |
| 316 | + return nil, err |
| 317 | + } |
| 318 | + return out, nil |
| 319 | +} |
| 320 | + |
| 321 | +// FilesWrite write to a mutable file in a given filesystem |
| 322 | +func (s *Shell) FilesWrite(ctx context.Context, path string, data io.Reader, options ...FilesOpt) error { |
| 323 | + fr := files.NewReaderFile(data) |
| 324 | + slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)}) |
| 325 | + fileReader := files.NewMultiFileReader(slf, true) |
| 326 | + |
| 327 | + rb := s.Request("files/write", path) |
| 328 | + for _, opt := range options { |
| 329 | + if err := opt(rb); err != nil { |
| 330 | + return err |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + return rb.Body(fileReader).Exec(ctx, nil) |
| 335 | +} |
0 commit comments