1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-17 15:06:47 +08:00

Cosmetic fixups in examples (#8325)

* test the examples in CI
* combine the two examples into a single one
This commit is contained in:
Petar Maymounkov
2021-08-27 11:55:26 -04:00
committed by GitHub
parent 7b160fbb76
commit c9cc09f6f7
9 changed files with 53 additions and 79 deletions

View File

@ -101,6 +101,9 @@ jobs:
- run:
when: always
command: bash <(curl -s https://codecov.io/bash) -cF unittests -X search -f coverage/unit_tests.coverprofile
- run:
command: go test -v ./...
working_directory: ~/ipfs/go-ipfs/docs/examples/go-ipfs-as-a-library
- run:
when: always

View File

Before

Width:  |  Height:  |  Size: 70 KiB

After

Width:  |  Height:  |  Size: 70 KiB

View File

@ -1,14 +1,13 @@
module github.com/ipfs/go-ipfs/examples/go-ipfs-as-a-library
go 1.14
go 1.15
require (
github.com/ipfs/go-ipfs v0.7.0
github.com/ipfs/go-ipfs v0.9.1
github.com/ipfs/go-ipfs-config v0.14.0
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/interface-go-ipfs-core v0.4.0
github.com/libp2p/go-libp2p-core v0.8.6
github.com/libp2p/go-libp2p-peerstore v0.2.8
github.com/multiformats/go-multiaddr v0.3.3
)

View File

@ -423,8 +423,8 @@ github.com/ipfs/go-ipfs-files v0.0.8 h1:8o0oFJkJ8UkO/ABl8T6ac6tKF3+NIpj67aAB6Zpu
github.com/ipfs/go-ipfs-files v0.0.8/go.mod h1:wiN/jSG8FKyk7N0WyctKSvq3ljIa2NNTiZB55kpTdOs=
github.com/ipfs/go-ipfs-keystore v0.0.2 h1:Fa9xg9IFD1VbiZtrNLzsD0GuELVHUFXCWF64kCPfEXU=
github.com/ipfs/go-ipfs-keystore v0.0.2/go.mod h1:H49tRmibOEs7gLMgbOsjC4dqh1u5e0R/SWuc2ScfgSo=
github.com/ipfs/go-ipfs-pinner v0.1.1 h1:iJd1gwILGQJSZhhI0jn6yFOLg34Ua7fdKcB6mXp6k/M=
github.com/ipfs/go-ipfs-pinner v0.1.1/go.mod h1:EzyyaWCWeZJ/he9cDBH6QrEkSuRqTRWMmCoyNkylTTg=
github.com/ipfs/go-ipfs-pinner v0.1.2 h1:Ve9OBhL6eg5+tVqEnIhPZOCXDtMjB+OhOohVZxPUxms=
github.com/ipfs/go-ipfs-pinner v0.1.2/go.mod h1:/u9kMe+TyQybN21O5OBicdyx3x93lVI77PCtiTnArUk=
github.com/ipfs/go-ipfs-posinfo v0.0.1 h1:Esoxj+1JgSjX0+ylc0hUmJCOv6V2vFoZiETLR6OtpRs=
github.com/ipfs/go-ipfs-posinfo v0.0.1/go.mod h1:SwyeVP+jCwiDu0C313l/8jg6ZxM0qqtlt2a0vILTc1A=
github.com/ipfs/go-ipfs-pq v0.0.1/go.mod h1:LWIqQpqfRG3fNc5XsnIhz/wQ2XXGyugQwls7BgUmUfY=

View File

@ -2,6 +2,7 @@ package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
@ -57,6 +58,24 @@ func createTempRepo() (string, error) {
return "", err
}
// When creating the repository, you can define custom settings on the repository, such as enabling experimental
// features (See experimental-features.md) or customizing the gateway endpoint.
// To do such things, you should modify the variable `cfg`. For example:
if *flagExp {
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-filestore
cfg.Experimental.FilestoreEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-urlstore
cfg.Experimental.UrlstoreEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#directory-sharding--hamt
cfg.Experimental.ShardingEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-p2p
cfg.Experimental.Libp2pStreamMounting = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#p2p-http-proxy
cfg.Experimental.P2pHttpProxy = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#strategic-providing
cfg.Experimental.StrategicProviding = true
}
// Create the repo with the config
err = fsrepo.Init(repoPath, cfg)
if err != nil {
@ -198,7 +217,11 @@ func getUnixfsNode(path string) (files.Node, error) {
/// -------
var flagExp = flag.Bool("experimental", false, "enable experimental features")
func main() {
flag.Parse()
/// --- Part I: Getting a IPFS node running
fmt.Println("-- Getting an IPFS node running -- ")
@ -228,7 +251,7 @@ func main() {
fmt.Println("\n-- Adding and getting back files & directories --")
inputBasePath := "./example-folder/"
inputBasePath := "../example-folder/"
inputPathFile := inputBasePath + "ipfs.paper.draft3.pdf"
inputPathDirectory := inputBasePath + "test-dir"
@ -258,7 +281,11 @@ func main() {
/// --- Part III: Getting the file and directory you added back
outputBasePath := "./example-folder/"
outputBasePath, err := ioutil.TempDir("", "example")
if err != nil {
panic(fmt.Errorf("could not create output dir (%v)", err))
}
fmt.Printf("output folder: %s\n", outputBasePath)
outputPathFile := outputBasePath + strings.Split(cidFile.String(), "/")[2]
outputPathDirectory := outputBasePath + strings.Split(cidDirectory.String(), "/")[2]

View File

@ -0,0 +1,17 @@
package main
import (
"os/exec"
"strings"
"testing"
)
func TestExample(t *testing.T) {
out, err := exec.Command("go", "run", "main.go").Output()
if err != nil {
t.Fatalf("running example (%v)", err)
}
if !strings.Contains(string(out), "All done!") {
t.Errorf("example did not run successfully")
}
}

View File

@ -1,72 +0,0 @@
# Use go-ipfs as a library and enable experimental features
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)
as it gives insights on how to create a repository, the daemon and add a file.
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):
```go
func createTempRepo(ctx context.Context) (string, error) {
repoPath, err := ioutil.TempDir("", "ipfs-shell")
if err != nil {
return "", fmt.Errorf("failed to get temp dir: %s", err)
}
// Create a config with default options and a 2048 bit key
cfg, err := config.Init(ioutil.Discard, 2048)
if err != nil {
return "", err
}
// Create the repo with the config
err = fsrepo.Init(repoPath, cfg)
if err != nil {
return "", fmt.Errorf("failed to init ephemeral node: %s", err)
}
return repoPath, nil
}
```
When creating the repository, you can define custom settings on the repository, such as enabling [experimental
features](../../experimental-features.md) or customizing the gateway endpoint.
To do such things, you should modify the variable `cfg`. For example, to enable the sharding experiment, you would modify the function to:
```go
func createTempRepo(ctx context.Context) (string, error) {
repoPath, err := ioutil.TempDir("", "ipfs-shell")
if err != nil {
return "", fmt.Errorf("failed to get temp dir: %s", err)
}
// Create a config with default options and a 2048 bit key
cfg, err := config.Init(ioutil.Discard, 2048)
if err != nil {
return "", err
}
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-filestore
cfg.Experimental.FilestoreEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-urlstore
cfg.Experimental.UrlstoreEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#directory-sharding--hamt
cfg.Experimental.ShardingEnabled = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#ipfs-p2p
cfg.Experimental.Libp2pStreamMounting = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#p2p-http-proxy
cfg.Experimental.P2pHttpProxy = true
// https://github.com/ipfs/go-ipfs/blob/master/docs/experimental-features.md#strategic-providing
cfg.Experimental.StrategicProviding = true
// Create the repo with the config
err = fsrepo.Init(repoPath, cfg)
if err != nil {
return "", fmt.Errorf("failed to init ephemeral node: %s", err)
}
return repoPath, nil
}
```
There are many other options that you can find through the [documentation](https://godoc.org/github.com/ipfs/go-ipfs-config#Config).