Skip to content

Commit b216c1d

Browse files
Add support for placing a managed IPFS credential
1 parent f69bf76 commit b216c1d

File tree

7 files changed

+117
-24
lines changed

7 files changed

+117
-24
lines changed

.github/workflows/builder.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ on:
2525

2626
env:
2727
PRODUCT: wayback
28+
WAYBACK_IPFS_APIKEY: ${{ secrets.WAYBACK_IPFS_APIKEY }}
2829

2930
permissions: write-all
3031

.github/workflows/docker.yml

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ jobs:
8989
9090
- name: Build artifacts
9191
uses: docker/bake-action@fb56172975f3a7f62d2defb5d32d735a2eaa41eb # v2.0.0
92+
env:
93+
WAYBACK_IPFS_APIKEY: ${{ secrets.WAYBACK_IPFS_APIKEY }}
9294
with:
9395
files: ./docker-bake.hcl
9496
targets: artifact-all
@@ -184,6 +186,8 @@ jobs:
184186
185187
- name: Build artifacts
186188
uses: docker/bake-action@fb56172975f3a7f62d2defb5d32d735a2eaa41eb # v2.0.0
189+
env:
190+
WAYBACK_IPFS_APIKEY: ${{ secrets.WAYBACK_IPFS_APIKEY }}
187191
with:
188192
files: ./docker-bake.hcl
189193
targets: artifact-all

.github/workflows/release.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111

1212
env:
1313
PRODUCT: wayback
14+
WAYBACK_IPFS_APIKEY: ${{ secrets.WAYBACK_IPFS_APIKEY }}
1415

1516
permissions: write-all
1617

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PACKDIR ?= ./build/package
99
LDFLAGS := $(shell echo "-X '${REPO}/version.Version=`git describe --tags --abbrev=0`'")
1010
LDFLAGS := $(shell echo "${LDFLAGS} -X '${REPO}/version.Commit=`git rev-parse --short HEAD`'")
1111
LDFLAGS := $(shell echo "${LDFLAGS} -X '${REPO}/version.BuildDate=`date +%FT%T%z`'")
12+
LDFLAGS := $(shell echo "${LDFLAGS} -X '${REPO}/config.IPFSToken=$(shell echo ${WAYBACK_IPFS_APIKEY})'")
1213
GOBUILD ?= go build -trimpath --ldflags "-s -w ${LDFLAGS} -buildid=" -v
1314
VERSION ?= $(shell git describe --tags `git rev-list --tags --max-count=1` | sed -e 's/v//g')
1415
GOFILES ?= $(wildcard ./cmd/wayback/*.go)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ You can also specify configuration options either via command flags or via envir
233233
| `--ipfs-host` | `WAYBACK_IPFS_HOST` | `127.0.0.1` | IPFS daemon service host |
234234
| `-p`, `--ipfs-port` | `WAYBACK_IPFS_PORT` | `5001` | IPFS daemon service port |
235235
| `-m`, `--ipfs-mode` | `WAYBACK_IPFS_MODE` | `pinner` | IPFS mode for preserve webpage, e.g. `daemon`, `pinner` |
236-
| - | `WAYBACK_IPFS_TARGET` | `infura` | The IPFS pinning service is used to store files, supported pinners: infura, pinata, nftstorage, web3storage. |
236+
| - | `WAYBACK_IPFS_TARGET` | `web3storage` | The IPFS pinning service is used to store files, supported pinners: infura, pinata, nftstorage, web3storage. |
237237
| - | `WAYBACK_IPFS_APIKEY` | - | Apikey of the IPFS pinning service |
238238
| - | `WAYBACK_IPFS_SECRET` | - | Secret of the IPFS pinning service |
239239
| - | `WAYBACK_GITHUB_TOKEN` | - | GitHub Personal Access Token, required the `repo` scope |

config/config_test.go

+98-22
Original file line numberDiff line numberDiff line change
@@ -243,38 +243,114 @@ func TestIPFSMode(t *testing.T) {
243243
}
244244

245245
func TestIPFSTarget(t *testing.T) {
246-
os.Clearenv()
247-
os.Setenv("WAYBACK_IPFS_TARGET", "target")
248-
249-
parser := NewParser()
250-
opts, err := parser.ParseEnvironmentVariables()
251-
if err != nil {
252-
t.Fatalf(`Parsing environment variables failed: %v`, err)
246+
var tests = []struct {
247+
token string // managed ipfs token
248+
userApikey string
249+
userTarget string
250+
expected string
251+
}{
252+
{
253+
token: "",
254+
userApikey: "",
255+
userTarget: "",
256+
expected: "",
257+
},
258+
{
259+
token: "foo",
260+
userApikey: "",
261+
userTarget: "",
262+
expected: IPFSTarget,
263+
},
264+
{
265+
token: "",
266+
userApikey: "bar",
267+
userTarget: "",
268+
expected: "",
269+
},
270+
{
271+
token: "",
272+
userApikey: "",
273+
userTarget: "foo-ipfs-pinning",
274+
expected: "foo-ipfs-pinning",
275+
},
253276
}
254277

255-
expected := "target"
256-
got := opts.IPFSTarget()
278+
for i, test := range tests {
279+
t.Run(strconv.Itoa(i), func(t *testing.T) {
280+
os.Clearenv()
281+
os.Setenv("WAYBACK_IPFS_TARGET", test.userTarget)
282+
os.Setenv("WAYBACK_IPFS_APIKEY", test.userApikey)
283+
IPFSToken = test.token
257284

258-
if got != expected {
259-
t.Fatalf(`Unexpected IPFS target, got %v instead of %s`, got, expected)
285+
parser := NewParser()
286+
opts, err := parser.ParseEnvironmentVariables()
287+
if err != nil {
288+
t.Fatalf(`Parsing environment variables failed: %v`, err)
289+
}
290+
291+
expected := test.expected
292+
got := opts.IPFSTarget()
293+
294+
if got != expected {
295+
t.Errorf(`Unexpected IPFS target, got %v instead of %s`, got, expected)
296+
}
297+
})
260298
}
261299
}
262300

263301
func TestIPFSApikey(t *testing.T) {
264-
os.Clearenv()
265-
os.Setenv("WAYBACK_IPFS_APIKEY", "apikey")
266-
267-
parser := NewParser()
268-
opts, err := parser.ParseEnvironmentVariables()
269-
if err != nil {
270-
t.Fatalf(`Parsing environment variables failed: %v`, err)
302+
var tests = []struct {
303+
token string // managed ipfs token
304+
userApikey string
305+
userTarget string
306+
expected string
307+
}{
308+
{
309+
token: "",
310+
userApikey: "",
311+
userTarget: "",
312+
expected: "",
313+
},
314+
{
315+
token: "foo",
316+
userApikey: "",
317+
userTarget: "",
318+
expected: "foo",
319+
},
320+
{
321+
token: "bar",
322+
userApikey: "zoo",
323+
userTarget: "",
324+
expected: "zoo",
325+
},
326+
{
327+
token: "",
328+
userApikey: "",
329+
userTarget: "foo-ipfs-pinning",
330+
expected: "",
331+
},
271332
}
272333

273-
expected := "apikey"
274-
got := opts.IPFSApikey()
334+
for i, test := range tests {
335+
t.Run(strconv.Itoa(i), func(t *testing.T) {
336+
os.Clearenv()
337+
os.Setenv("WAYBACK_IPFS_TARGET", test.userTarget)
338+
os.Setenv("WAYBACK_IPFS_APIKEY", test.userApikey)
339+
IPFSToken = test.token
275340

276-
if got != expected {
277-
t.Fatalf(`Unexpected IPFS apikey, got %v instead of %s`, got, expected)
341+
parser := NewParser()
342+
opts, err := parser.ParseEnvironmentVariables()
343+
if err != nil {
344+
t.Fatalf(`Parsing environment variables failed: %v`, err)
345+
}
346+
347+
expected := test.expected
348+
got := opts.IPFSApikey()
349+
350+
if got != expected {
351+
t.Errorf(`Unexpected IPFS apikey, got %v instead of %s`, got, expected)
352+
}
353+
})
278354
}
279355
}
280356

config/options.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
defIPFSHost = "127.0.0.1"
2424
defIPFSPort = 4001
2525
defIPFSMode = "pinner"
26-
defIPFSTarget = "infura"
26+
defIPFSTarget = ""
2727
defIPFSApikey = ""
2828
defIPFSSecret = ""
2929

@@ -93,6 +93,9 @@ const (
9393
)
9494

9595
var (
96+
IPFSToken = ""
97+
IPFSTarget = "web3storage"
98+
9699
defTorRemotePorts = []int{80}
97100
)
98101

@@ -358,11 +361,18 @@ func (o *Options) IPFSMode() string {
358361

359362
// IPFSTarget returns which IPFS pinning service to use.
360363
func (o *Options) IPFSTarget() string {
364+
if IPFSToken != "" {
365+
return IPFSTarget
366+
}
361367
return o.ipfs.target
362368
}
363369

364370
// IPFSApiKey returns the apikey of the IPFS pinning service.
371+
// It returns a managed IPFS credential if env `WAYBACK_IPFS_APIKEY` empty.
365372
func (o *Options) IPFSApikey() string {
373+
if o.ipfs.apikey == "" {
374+
return IPFSToken
375+
}
366376
return o.ipfs.apikey
367377
}
368378

0 commit comments

Comments
 (0)