Cosmetic fixups in examples (#8325)
* test the examples in CI * combine the two examples into a single one
Petar Maymounkov committed
Aug 27, 2021 at 11:55 UTC
c9cc09f6f7ebe95da69be6fa92c88e4cb245d90b
9 files changed
+53
-79
.circleci/config.yml
+3
@@ -101,6 +101,9 @@ jobs:
101
- run:
102
when: always
103
command: bash <(curl -s https://codecov.io/bash) -cF unittests -X search -f coverage/unit_tests.coverprofile
104
+ - run:
105
+ command: go test -v ./...
106
+ working_directory: ~/ipfs/go-ipfs/docs/examples/go-ipfs-as-a-library
107
108
- run:
109
when: always
docs/examples/example-folder/ipfs.paper.draft3.pdf
renamed
docs/examples/example-folder/test-dir/ipfs-logo.png
renamed
docs/examples/example-folder/test-dir/ipfs.paper.draft3.pdf
renamed
docs/examples/go-ipfs-as-a-library/go.mod
+2
-3
@@ -1,14 +1,13 @@
1
module github.com/ipfs/go-ipfs/examples/go-ipfs-as-a-library
2
3
-go 1.14
3
+go 1.15
4
5
require (
6
- github.com/ipfs/go-ipfs v0.7.0
6
+ github.com/ipfs/go-ipfs v0.9.1
7
github.com/ipfs/go-ipfs-config v0.14.0
8
github.com/ipfs/go-ipfs-files v0.0.8
9
github.com/ipfs/interface-go-ipfs-core v0.4.0
10
github.com/libp2p/go-libp2p-core v0.8.6
11
- github.com/libp2p/go-libp2p-peerstore v0.2.8
11
github.com/multiformats/go-multiaddr v0.3.3
12
)
13
docs/examples/go-ipfs-as-a-library/go.sum
+2
-2
@@ -423,8 +423,8 @@ github.com/ipfs/go-ipfs-files v0.0.8 h1:8o0oFJkJ8UkO/ABl8T6ac6tKF3+NIpj67aAB6Zpu
423
github.com/ipfs/go-ipfs-files v0.0.8/go.mod h1:wiN/jSG8FKyk7N0WyctKSvq3ljIa2NNTiZB55kpTdOs=
424
github.com/ipfs/go-ipfs-keystore v0.0.2 h1:Fa9xg9IFD1VbiZtrNLzsD0GuELVHUFXCWF64kCPfEXU=
425
github.com/ipfs/go-ipfs-keystore v0.0.2/go.mod h1:H49tRmibOEs7gLMgbOsjC4dqh1u5e0R/SWuc2ScfgSo=
426
-github.com/ipfs/go-ipfs-pinner v0.1.1 h1:iJd1gwILGQJSZhhI0jn6yFOLg34Ua7fdKcB6mXp6k/M=
427
-github.com/ipfs/go-ipfs-pinner v0.1.1/go.mod h1:EzyyaWCWeZJ/he9cDBH6QrEkSuRqTRWMmCoyNkylTTg=
426
+github.com/ipfs/go-ipfs-pinner v0.1.2 h1:Ve9OBhL6eg5+tVqEnIhPZOCXDtMjB+OhOohVZxPUxms=
427
+github.com/ipfs/go-ipfs-pinner v0.1.2/go.mod h1:/u9kMe+TyQybN21O5OBicdyx3x93lVI77PCtiTnArUk=
428
github.com/ipfs/go-ipfs-posinfo v0.0.1 h1:Esoxj+1JgSjX0+ylc0hUmJCOv6V2vFoZiETLR6OtpRs=
429
github.com/ipfs/go-ipfs-posinfo v0.0.1/go.mod h1:SwyeVP+jCwiDu0C313l/8jg6ZxM0qqtlt2a0vILTc1A=
430
github.com/ipfs/go-ipfs-pq v0.0.1/go.mod h1:LWIqQpqfRG3fNc5XsnIhz/wQ2XXGyugQwls7BgUmUfY=
docs/examples/go-ipfs-as-a-library/main.go
+29
-2
@@ -2,6 +2,7 @@ package main
2
3
import (
4
"context"
5
+ "flag"
6
"fmt"
7
"io/ioutil"
8
"log"
@@ -57,6 +58,24 @@ func createTempRepo() (string, error) {
58
return "", err
59
}
60
61
+ // When creating the repository, you can define custom settings on the repository, such as enabling experimental
62
+ // features (See experimental-features.md) or customizing the gateway endpoint.
63
+ // To do such things, you should modify the variable `cfg`. For example:
64
+ if *flagExp {
65
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-filestore
66
+ cfg.Experimental.FilestoreEnabled = true
67
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-urlstore
68
+ cfg.Experimental.UrlstoreEnabled = true
69
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#directory-sharding--hamt
70
+ cfg.Experimental.ShardingEnabled = true
71
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-p2p
72
+ cfg.Experimental.Libp2pStreamMounting = true
73
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#p2p-http-proxy
74
+ cfg.Experimental.P2pHttpProxy = true
75
+ // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#strategic-providing
76
+ cfg.Experimental.StrategicProviding = true
77
+ }
78
+
79
// Create the repo with the config
80
err = fsrepo.Init(repoPath, cfg)
81
if err != nil {
@@ -198,7 +217,11 @@ func getUnixfsNode(path string) (files.Node, error) {
217
218
/// -------
219
220
+var flagExp = flag.Bool("experimental", false, "enable experimental features")
221
+
222
func main() {
223
+ flag.Parse()
224
+
225
/// --- Part I: Getting a IPFS node running
226
227
fmt.Println("-- Getting an IPFS node running -- ")
@@ -228,7 +251,7 @@ func main() {
251
252
fmt.Println("\n-- Adding and getting back files & directories --")
253
231
- inputBasePath := "./example-folder/"
254
+ inputBasePath := "../example-folder/"
255
inputPathFile := inputBasePath + "ipfs.paper.draft3.pdf"
256
inputPathDirectory := inputBasePath + "test-dir"
257
@@ -258,7 +281,11 @@ func main() {
281
282
/// --- Part III: Getting the file and directory you added back
283
261
- outputBasePath := "./example-folder/"
284
+ outputBasePath, err := ioutil.TempDir("", "example")
285
+ if err != nil {
286
+ panic(fmt.Errorf("could not create output dir (%v)", err))
287
+ }
288
+ fmt.Printf("output folder: %s\n", outputBasePath)
289
outputPathFile := outputBasePath + strings.Split(cidFile.String(), "/")[2]
290
outputPathDirectory := outputBasePath + strings.Split(cidDirectory.String(), "/")[2]
291
docs/examples/go-ipfs-as-a-library/main_test.go
new
+17
@@ -0,0 +1,17 @@
1
+package main
2
+
3
+import (
4
+ "os/exec"
5
+ "strings"
6
+ "testing"
7
+)
8
+
9
+func TestExample(t *testing.T) {
10
+ out, err := exec.Command("go", "run", "main.go").Output()
11
+ if err != nil {
12
+ t.Fatalf("running example (%v)", err)
13
+ }
14
+ if !strings.Contains(string(out), "All done!") {
15
+ t.Errorf("example did not run successfully")
16
+ }
17
+}
docs/examples/library-experimental-features/README.md
deleted
-72
@@ -1,72 +0,0 @@
1
-# Use go-ipfs as a library and enable experimental features
2
-
3
-Before moving on to this tutorial, you must read first the initial [`go-ipfs` as a library tutorial](../go-ipfs-as-a-library/README.md)
4
-as it gives insights on how to create a repository, the daemon and add a file.
5
-
6
-There is only one thing that differs from this example and the first tutorial, which is the function [`createTempRepo`](../go-ipfs-as-a-library/main.go#L49):
7
-
8
-```go
9
-func createTempRepo(ctx context.Context) (string, error) {
10
- repoPath, err := ioutil.TempDir("", "ipfs-shell")
11
- if err != nil {
12
- return "", fmt.Errorf("failed to get temp dir: %s", err)
13
- }
14
-
15
- // Create a config with default options and a 2048 bit key
16
- cfg, err := config.Init(ioutil.Discard, 2048)
17
- if err != nil {
18
- return "", err
19
- }
20
-
21
- // Create the repo with the config
22
- err = fsrepo.Init(repoPath, cfg)
23
- if err != nil {
24
- return "", fmt.Errorf("failed to init ephemeral node: %s", err)
25
- }
26
-
27
- return repoPath, nil
28
-}
29
-```
30
-
31
-When creating the repository, you can define custom settings on the repository, such as enabling [experimental
32
-features](../../experimental-features.md) or customizing the gateway endpoint.
33
-
34
-To do such things, you should modify the variable `cfg`. For example, to enable the sharding experiment, you would modify the function to:
35
-
36
-```go
37
-func createTempRepo(ctx context.Context) (string, error) {
38
- repoPath, err := ioutil.TempDir("", "ipfs-shell")
39
- if err != nil {
40
- return "", fmt.Errorf("failed to get temp dir: %s", err)
41
- }
42
-
43
- // Create a config with default options and a 2048 bit key
44
- cfg, err := config.Init(ioutil.Discard, 2048)
45
- if err != nil {
46
- return "", err
47
- }
48
-
49
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-filestore
50
- cfg.Experimental.FilestoreEnabled = true
51
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-urlstore
52
- cfg.Experimental.UrlstoreEnabled = true
53
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#directory-sharding--hamt
54
- cfg.Experimental.ShardingEnabled = true
55
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-p2p
56
- cfg.Experimental.Libp2pStreamMounting = true
57
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#p2p-http-proxy
58
- cfg.Experimental.P2pHttpProxy = true
59
- // https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#strategic-providing
60
- cfg.Experimental.StrategicProviding = true
61
-
62
- // Create the repo with the config
63
- err = fsrepo.Init(repoPath, cfg)
64
- if err != nil {
65
- return "", fmt.Errorf("failed to init ephemeral node: %s", err)
66
- }
67
-
68
- return repoPath, nil
69
-}
70
-```
71
-
72
-There are many other options that you can find through the [documentation](https://godoc.org/github.com/ipfs/go-ipfs-config#Config).