storj/cmd/overlay/main.go
Jeff Wendling e74cac52ab
Command line flags features and cleanup (#2068)
* change BindSetup to be an option to Bind
* add process.Bind to allow composite structures
* hack fix for noprefix flags
* used tagged version of structs

Before this PR, some flags were created by calling `cfgstruct.Bind` and having their fields create a flag. Once the flags were parsed, `viper` was used to acquire all the values from them and config files, and the fields in the struct were set through the flag interface.

This doesn't work for slices of things on config structs very well, since it can only set strings, and for a string slice, it turns out that the implementation in `pflag` appends an entry rather than setting it.

This changes three things:

1. Only have a `Bind` call instead of `Bind` and `BindSetup`, and make `BindSetup` an option instead.
2. Add a `process.Bind` call that takes in a `*cobra.Cmd`, binds the struct to the command's flags, and keeps track of that struct in a global map keyed by the command.
3. Use `viper` to get the values and load them into the bound configuration structs instead of using the flags to propagate the changes.

In this way, we can support whatever rich configuration we want in the config yaml files, while still getting command like flags when important.
2019-05-29 17:56:22 +00:00

130 lines
2.4 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/storj"
)
var (
rootCmd = &cobra.Command{
Use: "overlay",
Short: "Overlay cache management",
}
addCmd = &cobra.Command{
Use: "add",
Short: "Add nodes to the overlay cache",
RunE: cmdAdd,
}
listCmd = &cobra.Command{
Use: "list",
Short: "List nodes in the overlay cache",
RunE: cmdList,
}
cacheCfg struct {
cacheConfig
}
)
func init() {
defaults := cfgstruct.DefaultsFlag(rootCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
process.Bind(addCmd, &cacheCfg, defaults)
process.Bind(listCmd, &cacheCfg, defaults)
}
func cmdList(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
keys, err := cache.Inspect(ctx)
if err != nil {
return err
}
nodeIDs, err := storj.NodeIDsFromBytes(keys.ByteSlices())
if err != nil {
return err
}
const padding = 3
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Node ID\t Address")
for _, id := range nodeIDs {
n, err := cache.Get(process.Ctx(cmd), id)
if err != nil {
fmt.Fprintln(w, id.String(), "\t", "error getting value")
}
if n != nil {
fmt.Fprintln(w, id.String(), "\t", n.Address.Address)
continue
}
fmt.Fprintln(w, id.String(), "\tnil")
}
return w.Flush()
}
func cmdAdd(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
j, err := ioutil.ReadFile(cacheCfg.NodesPath)
if err != nil {
return errs.New("Unable to read file with nodes: %+v", err)
}
var nodes map[string]string
if err := json.Unmarshal(j, &nodes); err != nil {
return errs.Wrap(err)
}
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
for i, a := range nodes {
id, err := storj.NodeIDFromString(i)
if err != nil {
zap.S().Error(err)
}
fmt.Printf("adding node ID: %s; Address: %s", i, a)
err = cache.Put(process.Ctx(cmd), id, pb.Node{
Id: id,
Address: &pb.NodeAddress{
Transport: 0,
Address: a,
},
})
if err != nil {
return err
}
}
return nil
}
func main() {
process.Exec(rootCmd)
}