-
Notifications
You must be signed in to change notification settings - Fork 402
/
main.go
112 lines (89 loc) · 2.91 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
106
107
108
109
110
111
112
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/fpath"
"storj.io/private/cfgstruct"
"storj.io/private/process"
"storj.io/storj/crashcollect"
)
// Config defines storj crash collect service configuration.
type Config struct {
crashcollect.Config
}
func main() {
rootCmd := &cobra.Command{
Use: "crashcollect",
Short: "Crash collect service",
}
var runCfg Config
var setupCfg Config
var confDir string
var identityDir string
defaultConfDir := fpath.ApplicationDir("storj", "crashcollect")
defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "crashcollect")
cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for storj crash collect service configuration")
cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for storj crash collect service identity credentials")
defaults := cfgstruct.DefaultsFlag(rootCmd)
runCmd := RunCommand(&runCfg)
setupCmd := SetupCommand(confDir)
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
process.Bind(setupCmd, &setupCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir), cfgstruct.SetupMode())
process.Bind(runCmd, &runCfg, defaults, cfgstruct.ConfDir(confDir), cfgstruct.IdentityDir(identityDir))
process.ExecCustomDebug(rootCmd)
}
// RunCommand creates command for running crash collect.
func RunCommand(runCfg *Config) *cobra.Command {
runCmd := &cobra.Command{
Use: "run",
Short: "Run the storj crash collect service",
}
runCmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx, _ := process.Ctx(cmd)
log := zap.L()
identity, err := runCfg.Identity.Load()
if err != nil {
log.Error("failed to load identity.", zap.Error(err))
return errs.New("failed to load identity: %+v", err)
}
peer, err := crashcollect.New(log, identity, runCfg.Config)
if err != nil {
return err
}
runError := peer.Run(ctx)
closeError := peer.Close()
return errs.Combine(runError, closeError)
}
return runCmd
}
// SetupCommand creates command for creating config file for crash collect service.
func SetupCommand(confDir string) *cobra.Command {
setupCmd := &cobra.Command{
Use: "setup",
Short: "Create config files",
Annotations: map[string]string{"type": "setup"},
}
setupCmd.RunE = func(cmd *cobra.Command, args []string) error {
setupDir, err := filepath.Abs(confDir)
if err != nil {
return err
}
valid, _ := fpath.IsValidSetupDir(setupDir)
if !valid {
return fmt.Errorf("storj crash collect service configuration already exists (%v)", setupDir)
}
err = os.MkdirAll(setupDir, 0700)
if err != nil {
return err
}
return process.SaveConfig(cmd, filepath.Join(setupDir, "config.yaml"))
}
return setupCmd
}