-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
76 lines (67 loc) · 1.94 KB
/
init.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
package main
import (
"os"
"github.com/UsingCoding/fpgo/pkg/slices"
"github.com/urfave/cli/v2"
"github.com/UsingCoding/gostore/internal/common/maybe"
"github.com/UsingCoding/gostore/internal/gostore/app/encryption"
"github.com/UsingCoding/gostore/internal/gostore/app/storage"
"github.com/UsingCoding/gostore/internal/gostore/app/store"
"github.com/UsingCoding/gostore/internal/gostore/infrastructure/consoleoutput"
)
func initCmd() *cli.Command {
return &cli.Command{
Name: "init",
Usage: "Initialize store",
Action: executeInit,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Usage: "Local store id",
Required: true,
},
&cli.StringFlag{
Name: "store-path",
Usage: "Path to new store",
},
&cli.StringSliceFlag{
Name: "recipients",
Usage: "Pass public key to store",
Aliases: []string{"r"},
},
&cli.StringFlag{
Name: "remote",
Usage: "Remote address for store",
},
},
}
}
func executeInit(ctx *cli.Context) error {
storeID := ctx.String("id")
storePath := optStringFromCtx(ctx, "store-path")
remote := optStringFromCtx(ctx, "remote")
recipients := ctx.StringSlice("recipients")
service, _ := newStoreService(ctx)
res, err := service.Init(ctx.Context, store.InitParams{
CommonParams: store.CommonParams{
StorePath: storePath,
StoreID: maybe.NewJust(storeID),
},
Recipients: slices.Map(recipients, func(r string) encryption.Recipient {
return encryption.Recipient(r)
}),
StorageType: maybe.Maybe[storage.Type]{},
Remote: remote,
})
if err != nil {
return err
}
output := consoleoutput.New(os.Stdout, consoleoutput.WithNewline(true))
output.Printf("Created store: %s", res.StorePath)
if maybe.Valid(res.GeneratedIdentity) {
output.Printf("Generated keys:")
output.Printf("Public key: %s", maybe.Just(res.GeneratedIdentity).Recipient)
output.Printf("Private key: %s", maybe.Just(res.GeneratedIdentity).PrivateKey)
}
return nil
}