-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
145 lines (124 loc) · 4.73 KB
/
Copy pathroot.go
File metadata and controls
145 lines (124 loc) · 4.73 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cli
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
logging "github.com/ipfs/go-log/v2"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/fil-forge/piri/cmd/cli/client"
"github.com/fil-forge/piri/cmd/cli/identity"
"github.com/fil-forge/piri/cmd/cli/serve"
"github.com/fil-forge/piri/cmd/cli/setup"
"github.com/fil-forge/piri/cmd/cli/status"
"github.com/fil-forge/piri/cmd/cli/wallet"
"github.com/fil-forge/piri/pkg/build"
)
func ExecuteContext(ctx context.Context) {
if err := rootCmd.ExecuteContext(ctx); err != nil {
log.Fatal(err)
}
}
var log = logging.Logger("cmd")
var configFilePath = path.Join("piri", "config.toml")
const piriShortDescription = `
Piri is the software run by all storage providers on the Storacha network
`
var (
cfgFile string
logLevel string
rootCmd = &cobra.Command{
Use: "piri",
Short: piriShortDescription,
Long: fmt.Sprintf(`Piri - Provable Information Retention Interface (Version: %s)
Piri can run entirely on its own with no software other than Filecoin Lotus, or it can integrate into Filecoin storage provider operation running Curio.`, build.Version),
Version: build.Version,
}
)
func init() {
cobra.OnInitialize(initLogging, initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Config file path. Attempts to load from user config directory if not set e.g. ~/.config/"+configFilePath)
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "logging level")
rootCmd.PersistentFlags().String("data-dir", filepath.Join(lo.Must(os.UserHomeDir()), ".storacha"), "Storage service data directory")
cobra.CheckErr(viper.BindPFlag("repo.data_dir", rootCmd.PersistentFlags().Lookup("data-dir")))
// backwards compatibility
cobra.CheckErr(viper.BindEnv("repo.data_dir", "PIRI_DATA_DIR"))
rootCmd.PersistentFlags().String("temp-dir", filepath.Join(os.TempDir(), "storage"), "Storage service temp directory")
cobra.CheckErr(viper.BindPFlag("repo.temp_dir", rootCmd.PersistentFlags().Lookup("temp-dir")))
// backwards compatibility
cobra.CheckErr(viper.BindEnv("repo.temp_dir", "PIRI_TEMP_DIR"))
rootCmd.PersistentFlags().String("key-file", "", "Path to a PEM file containing ed25519 private key")
cobra.CheckErr(rootCmd.MarkPersistentFlagFilename("key-file", "pem"))
cobra.CheckErr(viper.BindPFlag("identity.key_file", rootCmd.PersistentFlags().Lookup("key-file")))
// backwards compatibility
cobra.CheckErr(viper.BindEnv("identity.key_file", "PIRI_KEY_FILE"))
// register all commands and their subcommands
rootCmd.AddCommand(serve.Cmd)
rootCmd.AddCommand(wallet.Cmd)
rootCmd.AddCommand(identity.Cmd)
rootCmd.AddCommand(client.Cmd)
rootCmd.AddCommand(status.Cmd)
rootCmd.AddCommand(setup.InitCmd)
rootCmd.AddCommand(setup.InstallCmd)
rootCmd.AddCommand(setup.UninstallCmd)
rootCmd.AddCommand(setup.UpdateCmd)
rootCmd.AddCommand(setup.InternalUpdateCmd)
}
func initConfig() {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("PIRI")
if cfgFile == "" {
if configDir, err := os.UserConfigDir(); err == nil {
defaultCfgFile := path.Join(configDir, configFilePath)
if inf, err := os.Stat(defaultCfgFile); err == nil && !inf.IsDir() {
log.Infof("loading config automatically from: %s", defaultCfgFile)
cfgFile = defaultCfgFile
}
}
}
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
cobra.CheckErr(viper.ReadInConfig())
} else {
// otherwise look for piri-config.toml in current directory
viper.SetConfigName("piri-config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
// Don't error if config file is not found - it's optional
_ = viper.ReadInConfig()
}
}
func initLogging() {
if logLevel != "" {
ll, err := logging.LevelFromString(logLevel)
cobra.CheckErr(err)
logging.SetAllLoggers(ll)
} else {
// else set all loggers to warn level, then the ones we care most about to info.
logging.SetAllLoggers(logging.LevelWarn)
logging.SetLogLevel("pdp/service", "info")
logging.SetLogLevel("pdp/client", "info")
logging.SetLogLevel("telemetry", "info")
logging.SetLogLevel("publisher", "warn")
logging.SetLogLevel("cli/wallet", "info")
logging.SetLogLevel("announce", "warn")
logging.SetLogLevel("proof", "warn")
logging.SetLogLevel("pdp/aggregator", "info")
logging.SetLogLevel("pdp/scheduler", "info")
logging.SetLogLevel("metrics", "warn")
logging.SetLogLevel("pdp/tasks", "info")
logging.SetLogLevel("pdp/api", "info")
logging.SetLogLevel("replicator", "info")
logging.SetLogLevel("storage/ucan", "info")
logging.SetLogLevel("pdp/server", "info")
logging.SetLogLevel("cmd/serve", "info")
logging.SetLogLevel("database", "warn")
logging.SetLogLevel("server", "info")
logging.SetLogLevel("storage", "info")
}
}