2018-07-26 15:21:35 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-08 23:22:59 +01:00
|
|
|
"fmt"
|
2018-07-26 15:21:35 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/peertls"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
|
|
|
"storj.io/storj/pkg/process"
|
|
|
|
"storj.io/storj/pkg/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "hc",
|
|
|
|
Short: "Heavy client",
|
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
runCmd = &cobra.Command{
|
|
|
|
Use: "run",
|
|
|
|
Short: "Run the heavy client",
|
|
|
|
RunE: cmdRun,
|
|
|
|
}
|
|
|
|
setupCmd = &cobra.Command{
|
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
|
|
|
}
|
2018-07-26 15:21:35 +01:00
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
runCfg struct {
|
2018-07-26 15:21:35 +01:00
|
|
|
Identity provider.IdentityConfig
|
|
|
|
Kademlia kademlia.Config
|
|
|
|
PointerDB pointerdb.Config
|
|
|
|
Overlay overlay.Config
|
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
setupCfg struct {
|
2018-08-08 23:22:59 +01:00
|
|
|
BasePath string `default:"$CONFDIR" help:"base path for setup"`
|
|
|
|
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultConfDir = "$HOME/.storj/hc"
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-07-30 08:38:31 +01:00
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
|
|
|
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
|
|
|
|
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2018-07-30 08:38:31 +01:00
|
|
|
return runCfg.Identity.Run(process.Ctx(cmd),
|
|
|
|
runCfg.Kademlia, runCfg.PointerDB, runCfg.Overlay)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
2018-08-08 23:22:59 +01:00
|
|
|
_, err = os.Stat(setupCfg.BasePath)
|
|
|
|
if !setupCfg.Overwrite && err == nil {
|
|
|
|
fmt.Println("An hc configuration already exists. Rerun with --overwrite")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
err = os.MkdirAll(setupCfg.BasePath, 0700)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
identityPath := filepath.Join(setupCfg.BasePath, "identity")
|
2018-08-08 23:22:59 +01:00
|
|
|
_, err = peertls.NewTLSFileOptions(identityPath, identityPath, true, true)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
return process.SaveConfig(runCmd.Flags(),
|
|
|
|
filepath.Join(setupCfg.BasePath, "config.yaml"), nil)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-07-30 08:38:31 +01:00
|
|
|
runCmd.Flags().String("config",
|
|
|
|
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
|
|
|
|
process.Exec(rootCmd)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|