-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
multiaddr "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// MultiaddrHook returns a mapstructure decode hook for multiaddrs. | ||
func MultiaddrHook() mapstructure.DecodeHookFuncType { | ||
return func( | ||
f reflect.Type, | ||
t reflect.Type, | ||
data interface{}, | ||
) (interface{}, error) { | ||
if f.Kind() != reflect.String { | ||
return data, nil | ||
} | ||
|
||
if t != reflect.TypeOf((*multiaddr.Multiaddr)(nil)).Elem() { | ||
return data, nil | ||
} | ||
|
||
return multiaddr.NewMultiaddr(data.(string)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
multiaddr "github.com/multiformats/go-multiaddr" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var decryptorCmd = &cobra.Command{ | ||
Use: "decryptor", | ||
Short: "Run a decryptor node", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return decryptorMain() | ||
}, | ||
} | ||
|
||
type DecryptorConfig struct { | ||
PeerMultiaddrs []multiaddr.Multiaddr | ||
} | ||
|
||
func init() { | ||
decryptorCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") | ||
} | ||
|
||
func decryptorMain() error { | ||
config, err := readDecryptorConfig() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%+v", config) | ||
return nil | ||
} | ||
|
||
func readDecryptorConfig() (DecryptorConfig, error) { | ||
config := DecryptorConfig{} | ||
|
||
viper.AddConfigPath("$HOME/.config/shutter") | ||
viper.SetConfigName("decryptor") | ||
viper.SetConfigType("toml") | ||
viper.SetConfigFile(cfgFile) | ||
|
||
err := viper.ReadInConfig() | ||
if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
// Config file not found | ||
if cfgFile != "" { | ||
return config, err | ||
} | ||
} else if err != nil { | ||
return config, err // Config file was found but another error was produced | ||
} | ||
|
||
err = viper.Unmarshal(&config, viper.DecodeHook(MultiaddrHook())) | ||
if err != nil { | ||
return config, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters