/
init.go
67 lines (55 loc) 路 1.36 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
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"github.com/ipni/index-provider/cmd/provider/internal/config"
"github.com/urfave/cli/v2"
)
var InitCmd = &cli.Command{
Name: "init",
Usage: "Initialize reference provider config file and identity",
Flags: initFlags,
Action: initCommand,
}
var initFlags = []cli.Flag{
&cli.StringFlag{
Name: "pubkind",
Usage: "Set publisher kind in config. Must be one of 'http', 'libp2p', 'libp2phttp'",
Value: "libp2p",
},
}
func initCommand(cctx *cli.Context) error {
log.Info("Initializing provider config file")
// Check that the config root exists and it writable.
configRoot, err := config.PathRoot()
if err != nil {
return err
}
if err = dirWritable(configRoot); err != nil {
return err
}
configFile, err := config.Filename(configRoot)
if err != nil {
return err
}
_, err = os.Stat(configFile)
if !errors.Is(err, fs.ErrNotExist) {
return config.ErrInitialized
}
cfg, err := config.Init(cctx.App.Writer)
if err != nil {
return err
}
pubkind := config.PublisherKind(cctx.String("pubkind"))
switch pubkind {
case "":
pubkind = config.Libp2pPublisherKind
case config.Libp2pPublisherKind, config.HttpPublisherKind, config.Libp2pHttpPublisherKind:
default:
return fmt.Errorf("unknown publisher kind: %s", pubkind)
}
cfg.Ingest.PublisherKind = pubkind
return cfg.Save(configFile)
}