storj/cmd/hc/main.go
Maximillian von Briesen 889afd0ae5
add overwrite flag for captplanet, hc, and gw setup (#209)
* add overwrite flag for captplanet setup

* add overwrite flag for heavy client and gateway
2018-08-08 18:22:59 -04:00

91 lines
2.1 KiB
Go

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