|
| 1 | +package export |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/ipfs/boxo/files" |
| 10 | + iface "github.com/ipfs/kubo/core/coreiface" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | + "github.com/wetware/go/cmd/internal/flags" |
| 13 | + "github.com/wetware/go/util" |
| 14 | +) |
| 15 | + |
| 16 | +var env Env |
| 17 | + |
| 18 | +func Command() *cli.Command { |
| 19 | + return &cli.Command{ |
| 20 | + Name: "export", |
| 21 | + ArgsUsage: "<path>", |
| 22 | + Usage: "Add a file or directory to IPFS recursively", |
| 23 | + Description: `Add a file or directory to IPFS recursively, equivalent to 'ipfs add -r <path>'. |
| 24 | + |
| 25 | +The command will: |
| 26 | +1. Read the specified path from the local filesystem |
| 27 | +2. Add it to IPFS recursively (including all subdirectories and files) |
| 28 | +3. Print the resulting IPFS path to stdout followed by a newline |
| 29 | +
|
| 30 | +Examples: |
| 31 | + ww export /path/to/file.txt |
| 32 | + ww export /path/to/directory |
| 33 | + ww export . # Export current directory`, |
| 34 | + Flags: append([]cli.Flag{ |
| 35 | + &cli.StringFlag{ |
| 36 | + Name: "ipfs", |
| 37 | + EnvVars: []string{"WW_IPFS"}, |
| 38 | + Value: "/dns4/localhost/tcp/5001/http", |
| 39 | + Usage: "IPFS API endpoint", |
| 40 | + }, |
| 41 | + }, flags.CapabilityFlags()...), |
| 42 | + |
| 43 | + Before: func(c *cli.Context) error { |
| 44 | + return env.Boot(c.String("ipfs")) |
| 45 | + }, |
| 46 | + After: func(c *cli.Context) error { |
| 47 | + return env.Close() |
| 48 | + }, |
| 49 | + |
| 50 | + Action: Main, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func Main(c *cli.Context) error { |
| 55 | + ctx := c.Context |
| 56 | + |
| 57 | + if c.NArg() != 1 { |
| 58 | + return cli.Exit("export requires exactly one argument: <path>", 1) |
| 59 | + } |
| 60 | + |
| 61 | + argPath := c.Args().First() |
| 62 | + if argPath == "" { |
| 63 | + return cli.Exit("path cannot be empty", 1) |
| 64 | + } |
| 65 | + |
| 66 | + // Resolve the path to absolute |
| 67 | + absPath, err := filepath.Abs(argPath) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("failed to resolve path %s: %w", argPath, err) |
| 70 | + } |
| 71 | + |
| 72 | + // Check if path exists |
| 73 | + if _, err := os.Stat(absPath); os.IsNotExist(err) { |
| 74 | + return cli.Exit(fmt.Sprintf("path does not exist: %s", absPath), 1) |
| 75 | + } |
| 76 | + |
| 77 | + // Add the file/directory to IPFS |
| 78 | + ipfsPath, err := env.AddToIPFS(ctx, absPath) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to add to IPFS: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Print the IPFS path to stdout followed by a newline |
| 84 | + fmt.Println(ipfsPath) |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +type Env struct { |
| 89 | + IPFS iface.CoreAPI |
| 90 | +} |
| 91 | + |
| 92 | +func (env *Env) Boot(addr string) error { |
| 93 | + var err error |
| 94 | + env.IPFS, err = util.LoadIPFSFromName(addr) |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +func (env *Env) Close() error { |
| 99 | + // No cleanup needed for IPFS client |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// AddToIPFS adds a file or directory to IPFS recursively |
| 104 | +func (env *Env) AddToIPFS(ctx context.Context, localPath string) (string, error) { |
| 105 | + // Get file info to determine if it's a directory |
| 106 | + fileInfo, err := os.Stat(localPath) |
| 107 | + if err != nil { |
| 108 | + return "", fmt.Errorf("failed to stat %s: %w", localPath, err) |
| 109 | + } |
| 110 | + |
| 111 | + var node files.Node |
| 112 | + if fileInfo.IsDir() { |
| 113 | + // Handle directory |
| 114 | + node, err = env.CreateDirectoryNode(ctx, localPath) |
| 115 | + if err != nil { |
| 116 | + return "", fmt.Errorf("failed to create directory node: %w", err) |
| 117 | + } |
| 118 | + } else { |
| 119 | + // Handle single file |
| 120 | + node, err = env.CreateFileNode(ctx, localPath) |
| 121 | + if err != nil { |
| 122 | + return "", fmt.Errorf("failed to create file node: %w", err) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Check if IPFS client is available |
| 127 | + if env.IPFS == nil { |
| 128 | + return "", fmt.Errorf("IPFS client not initialized") |
| 129 | + } |
| 130 | + |
| 131 | + // Add the node to IPFS using Unixfs API |
| 132 | + path, err := env.IPFS.Unixfs().Add(ctx, node) |
| 133 | + if err != nil { |
| 134 | + return "", fmt.Errorf("failed to add to IPFS: %w", err) |
| 135 | + } |
| 136 | + |
| 137 | + return path.String(), nil |
| 138 | +} |
| 139 | + |
| 140 | +// CreateFileNode creates a files.Node for a single file |
| 141 | +func (env *Env) CreateFileNode(ctx context.Context, filePath string) (files.Node, error) { |
| 142 | + // Read the file content into memory |
| 143 | + content, err := os.ReadFile(filePath) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + // Create a file node from the content |
| 149 | + return files.NewBytesFile(content), nil |
| 150 | +} |
| 151 | + |
| 152 | +// CreateDirectoryNode creates a files.Node for a directory recursively |
| 153 | +func (env *Env) CreateDirectoryNode(ctx context.Context, dirPath string) (files.Node, error) { |
| 154 | + entries, err := os.ReadDir(dirPath) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + // Create a map to hold directory contents |
| 160 | + dirMap := make(map[string]files.Node) |
| 161 | + |
| 162 | + for _, entry := range entries { |
| 163 | + entryPath := filepath.Join(dirPath, entry.Name()) |
| 164 | + |
| 165 | + if entry.IsDir() { |
| 166 | + // Recursively handle subdirectories |
| 167 | + childNode, err := env.CreateDirectoryNode(ctx, entryPath) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + |
| 172 | + // Add subdirectory to the map |
| 173 | + dirMap[entry.Name()] = childNode |
| 174 | + } else { |
| 175 | + // Handle files |
| 176 | + childNode, err := env.CreateFileNode(ctx, entryPath) |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + |
| 181 | + // Add file to the map |
| 182 | + dirMap[entry.Name()] = childNode |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // Create directory from the map |
| 187 | + return files.NewMapDirectory(dirMap), nil |
| 188 | +} |
0 commit comments