textileio / textile

Textile hub services and buckets lib
MIT License
226 stars 45 forks source link

Consider adding some/a self-contained demo/example for easy copy-pasting #383

Open carsonfarmer opened 4 years ago

carsonfarmer commented 4 years ago

Here's one we could use:

// mkdir go-demo
// cd go-demo
// go mod init github.com/user/go-demo
// go get github.com/textileio/textile/v2/cmd
// go get github.com/textileio/textile/v2/mail
// go get github.com/libp2p/go-libp2p-core@v0.6.1
// # create your main.go
// # create your dev account and app's user group key via hub cli
// go run main.go
package main
import (
    "context"
    "crypto/rand"
    "fmt"
    "os"
    "github.com/libp2p/go-libp2p-core/crypto"
    "github.com/textileio/go-threads/core/thread"
    "github.com/textileio/textile/v2/cmd"
    "github.com/textileio/textile/v2/mail/local"
)
func main() {
    // Setup the mail lib
    mail := local.NewMail(cmd.NewClients("api.textile.io:443", false), local.DefaultConfConfig())
    // Create a libp2p identity (this can be any thread.Identity)
    privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
    id := thread.NewLibp2pIdentity(privKey)
    if err != nil {
        fmt.Fprintf(os.Stderr, "error: %v\n", err)
        os.Exit(1)
    }
    // Create a new mailbox with config
    mailbox, err := mail.NewMailbox(context.Background(), local.Config{
        Path:      "~/.textile/mail", // Usually a global location like ~/.textile/mail
        Identity:  id,
        APIKey:    "mykey",
        APISecret: "mySecret",
    })
    fmt.Println(mailbox.Identity().GetPublic().String())
}
carsonfarmer commented 4 years ago

This is already 99.99% of the way there: https://docs.textile.io/tutorials/go/getting-started/