-
Notifications
You must be signed in to change notification settings - Fork 45
/
main.go
105 lines (94 loc) · 3.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"context"
"crypto/rand"
"errors"
"fmt"
"strings"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/logrusorgru/aurora"
mbase "github.com/multiformats/go-multibase"
"github.com/spf13/cobra"
"github.com/textileio/go-threads/core/thread"
"github.com/textileio/textile/v2/buckets/local"
"github.com/textileio/textile/v2/cmd"
buck "github.com/textileio/textile/v2/cmd/buck/cli"
hub "github.com/textileio/textile/v2/cmd/hub/cli"
)
var clients *cmd.Clients
func init() {
cobra.OnInitialize(cmd.InitConfig(hub.Config()))
hub.Init(rootCmd)
}
func main() {
cmd.ErrCheck(rootCmd.Execute())
}
var rootCmd = &cobra.Command{
Use: hub.Name,
Short: "Hub Client",
Long: `The Hub Client.`,
PersistentPreRun: func(c *cobra.Command, args []string) {
cmd.ExpandConfigVars(hub.Config().Viper, hub.Config().Flags)
hubTarget := hub.Config().Viper.GetString("api")
minerIndexTarget := hub.Config().Viper.GetString("apiMinerIndex")
clients = cmd.NewClients(hubTarget, true, minerIndexTarget)
hub.SetClients(clients)
config := local.DefaultConfConfig()
config.EnvPrefix = fmt.Sprintf("%s_%s", strings.ToUpper(hub.Name), strings.ToUpper(buck.Name))
buck.SetBucks(local.NewBucketsWithAuth(clients, config, hub.Auth))
newIdentity, err := c.Flags().GetBool("newIdentity")
cmd.ErrCheck(err)
if newIdentity {
sk, pk, err := crypto.GenerateEd25519Key(rand.Reader)
cmd.ErrCheck(err)
skb, err := crypto.MarshalPrivateKey(sk)
cmd.ErrCheck(err)
pkb, err := crypto.MarshalPublicKey(pk)
cmd.ErrCheck(err)
sks, err := mbase.Encode(mbase.Base32, skb)
cmd.ErrCheck(err)
pks, err := mbase.Encode(mbase.Base32, pkb)
cmd.ErrCheck(err)
hub.Config().Viper.Set("identity", sks)
hub.Config().Viper.Set("token", "")
cmd.WriteConfigToHome(hub.Config())
cmd.Message("Generated new identity with public key %s", aurora.White(pks).Bold())
}
if hub.Config().Viper.GetString("identity") != "" &&
hub.Config().Viper.GetString("token") == "" {
_, id, err := mbase.Decode(hub.Config().Viper.GetString("identity"))
if err != nil {
cmd.Fatal(fmt.Errorf("invalid identity: %w", err))
}
sk, err := crypto.UnmarshalPrivateKey(id)
cmd.ErrCheck(err)
pkb, err := crypto.MarshalPublicKey(sk.GetPublic())
cmd.ErrCheck(err)
pks, err := mbase.Encode(mbase.Base32, pkb)
cmd.ErrCheck(err)
ctx, cancel := context.WithTimeout(context.Background(), cmd.Timeout)
defer cancel()
tok, err := clients.Threads.GetToken(hub.Auth(ctx), thread.NewLibp2pIdentity(sk))
if err != nil {
cmd.Fatal(fmt.Errorf("getting identity token: %w", err))
}
hub.Config().Viper.Set("token", string(tok))
cmd.WriteConfigToHome(hub.Config())
cmd.Message("Fetched new identity token for public key %s", aurora.White(pks).Bold())
}
if hub.Config().Viper.GetString("session") == "" &&
hub.Config().Viper.GetString("apiKey") == "" &&
c.Use != "init" &&
c.Use != "login" &&
c.Use != "version" &&
c.Use != "update" &&
c.Parent().Use != "index" {
msg := "unauthorized! run `%s` or use `%s` to authorize"
cmd.Fatal(errors.New(msg), aurora.Cyan(hub.Name+" init|login"), aurora.Cyan("--session"))
}
},
PersistentPostRun: func(c *cobra.Command, args []string) {
clients.Close()
},
Args: cobra.ExactArgs(0),
}