Skip to content

Commit e4d947b

Browse files
separate env variables for bacalhau and ipfs (#660)
1 parent 9da63b3 commit e4d947b

File tree

1 file changed

+20
-34
lines changed

1 file changed

+20
-34
lines changed

internal/ipfs/ipfs.go

+20-34
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@ import (
1414
"github.com/labdao/plex/internal/bacalhau"
1515
)
1616

17-
func DeriveIpfsNodeUrl() (string, error) {
18-
bacalApiHost := bacalhau.GetBacalhauApiHost()
17+
func DeriveIpfsNodeUrl() string {
18+
ipfsApiHost, exists := os.LookupEnv("IPFS_API_HOST")
1919

20-
// If bacalApiHost already starts with http:// or https://, don't prepend it.
21-
if strings.HasPrefix(bacalApiHost, "http://") || strings.HasPrefix(bacalApiHost, "https://") {
22-
ipfsUrl := fmt.Sprintf("%s:5001", bacalApiHost)
23-
return ipfsUrl, nil
20+
// If IPFS_API_HOST is not set, use BACALHAU_API_HOST
21+
if !exists {
22+
ipfsApiHost = bacalhau.GetBacalhauApiHost()
2423
}
2524

26-
ipfsUrl := fmt.Sprintf("http://%s:5001", bacalApiHost)
27-
return ipfsUrl, nil
25+
// If ipfsApiHost already starts with http:// or https://, don't prepend it.
26+
if strings.HasPrefix(ipfsApiHost, "http://") || strings.HasPrefix(ipfsApiHost, "https://") {
27+
ipfsUrl := fmt.Sprintf("%s:5001", ipfsApiHost)
28+
return ipfsUrl
29+
}
30+
ipfsUrl := fmt.Sprintf("http://%s:5001", ipfsApiHost)
31+
return ipfsUrl
2832
}
2933

3034
func PinDir(dirPath string) (cid string, err error) {
31-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
32-
if err != nil {
33-
return "", err
34-
}
35+
ipfsNodeUrl := DeriveIpfsNodeUrl()
3536
sh := shell.NewShell(ipfsNodeUrl)
3637
cid, err = sh.AddDir(dirPath)
3738
if err != nil {
@@ -42,10 +43,7 @@ func PinDir(dirPath string) (cid string, err error) {
4243
}
4344

4445
func PinFile(filePath string) (cid string, err error) {
45-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
46-
if err != nil {
47-
return "", err
48-
}
46+
ipfsNodeUrl := DeriveIpfsNodeUrl()
4947
sh := shell.NewShell(ipfsNodeUrl)
5048

5149
// Open the file and ensure it gets closed
@@ -68,10 +66,7 @@ func PinFile(filePath string) (cid string, err error) {
6866

6967
// wraps a file in a directory and adds it to IPFS
7068
func WrapAndPinFile(filePath string) (cid string, err error) {
71-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
72-
if err != nil {
73-
return "", err
74-
}
69+
ipfsNodeUrl := DeriveIpfsNodeUrl()
7570
sh := shell.NewShell(ipfsNodeUrl)
7671

7772
absPath, err := filepath.Abs(filePath)
@@ -115,17 +110,14 @@ func WrapAndPinFile(filePath string) (cid string, err error) {
115110
}
116111

117112
func DownloadToDirectory(cid, directory string) error {
118-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
119-
if err != nil {
120-
return err
121-
}
113+
ipfsNodeUrl := DeriveIpfsNodeUrl()
122114
sh := shell.NewShell(ipfsNodeUrl)
123115

124116
// Construct the full directory path where the CID content will be downloaded
125117
downloadPath := path.Join(directory, cid)
126118

127119
// Use the Get method to download the file or directory with the specified CID
128-
err = sh.Get(cid, downloadPath)
120+
err := sh.Get(cid, downloadPath)
129121
if err != nil {
130122
return err
131123
}
@@ -134,10 +126,7 @@ func DownloadToDirectory(cid, directory string) error {
134126
}
135127

136128
func DownloadToTempDir(cid string) (string, error) {
137-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
138-
if err != nil {
139-
return "", err
140-
}
129+
ipfsNodeUrl := DeriveIpfsNodeUrl()
141130
sh := shell.NewShell(ipfsNodeUrl)
142131

143132
// Get the system's temporary directory
@@ -147,7 +136,7 @@ func DownloadToTempDir(cid string) (string, error) {
147136
downloadPath := path.Join(tempDir, cid)
148137

149138
// Use the Get method to download the file or directory with the specified CID
150-
err = sh.Get(cid, downloadPath)
139+
err := sh.Get(cid, downloadPath)
151140
if err != nil {
152141
return "", err
153142
}
@@ -227,10 +216,7 @@ func onlyOneFile(dirPath string) (bool, string, error) {
227216
}
228217

229218
func DownloadFileContents(cid, filepath string) error {
230-
ipfsNodeUrl, err := DeriveIpfsNodeUrl()
231-
if err != nil {
232-
return err
233-
}
219+
ipfsNodeUrl := DeriveIpfsNodeUrl()
234220
sh := shell.NewShell(ipfsNodeUrl)
235221

236222
// Use the cat method to get the file with the specified CID

0 commit comments

Comments
 (0)