Skip to content

Commit

Permalink
Merge pull request #302 from mailchain/issue-299-show-current-config
Browse files Browse the repository at this point in the history
Show current config
  • Loading branch information
robdefeo authored Oct 8, 2019
2 parents 67ec3a6 + fae2a61 commit d25a45c
Show file tree
Hide file tree
Showing 53 changed files with 2,131 additions and 109 deletions.
4 changes: 2 additions & 2 deletions cmd/mailchain/commands/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func Test_accountCmd(t *testing.T) {
args{
func() *settings.Base {
v := viper.New()
config := settings.New(v)
config := settings.FromStore(v)
return config
}(),
},
Expand All @@ -337,7 +337,7 @@ func Test_accountCmd(t *testing.T) {
func() *settings.Base {
v := viper.New()
v.Set("keystore.kind", "invalid")
config := settings.New(v)
config := settings.FromStore(v)
return config
}(),
},
Expand Down
46 changes: 0 additions & 46 deletions cmd/mailchain/commands/config.go

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/mailchain/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ Complete documentation is available at https://github.com/mailchain/mailchain`,
cmd.PersistentFlags().String("log-level", "warn", "log level [Panic,Fatal,Error,Warn,Info,Debug]")
cmd.PersistentFlags().Bool("prevent-init-config", false, "stop automatically creating config if non is found")

config := settings.New(v)
config := settings.FromStore(v)
account, err := accountCmd(config)
if err != nil {
return nil, err
}
cmd.AddCommand(account)
cmd.AddCommand(settingsCmd(config))

serve, err := serveCmd()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func serveCmd() (*cobra.Command, error) {
Short: "Serve the mailchain application",
// PersistentPreRunE: preRun,
RunE: func(cmd *cobra.Command, args []string) error {
config := settings.New(viper.GetViper())
config := settings.FromStore(viper.GetViper())
router, err := http.CreateRouter(config, cmd)
if err != nil {
return err
Expand Down
45 changes: 45 additions & 0 deletions cmd/mailchain/commands/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings"
"github.com/spf13/cobra"
)

func settingsCmd(config *settings.Base) *cobra.Command {
cmd := &cobra.Command{
Use: "settings",
Short: "Settings of the mailchain application",
}
cmd.AddCommand(settingsViewAll(config))
return cmd
}

func settingsViewAll(config *settings.Base) *cobra.Command {
cmd := &cobra.Command{
Use: "view",
Short: "View the current config",
RunE: func(cmd *cobra.Command, args []string) error {
commentDefaults, _ := cmd.Flags().GetBool("comment-defaults")
excludeDefaults, _ := cmd.Flags().GetBool("exclude-defaults")
config.ToYaml(cmd.OutOrStderr(), 2, commentDefaults, excludeDefaults)
return nil
},
}
cmd.Flags().Bool("comment-defaults", true, "comment out values if the value is the default")
cmd.Flags().Bool("exclude-defaults", false, "exclude values if the value is the default")
return cmd
}
96 changes: 96 additions & 0 deletions cmd/mailchain/commands/settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2019 Finobo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"strings"
"testing"

"github.com/mailchain/mailchain/cmd/mailchain/internal/settings"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func Test_settingsCmd(t *testing.T) {
assert := assert.New(t)
type args struct {
config *settings.Base
}
tests := []struct {
name string
args args
wantCommandNames []string
}{
{
"success",
args{
func() *settings.Base {
v := viper.New()
config := settings.FromStore(v)
return config
}(),
},
[]string{
"view",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := settingsCmd(tt.args.config)
subCommandNames := []string{}
for _, x := range got.Commands() {
subCommandNames = append(subCommandNames, x.Name())
}
if !assert.Equal(tt.wantCommandNames, subCommandNames) {
t.Errorf("settingsCmd().Commands = %v, wantCommandNames %v", subCommandNames, strings.Join(tt.wantCommandNames, ","))
}
})
}
}

func Test_settingsViewAll(t *testing.T) {
type args struct {
config *settings.Base
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"success",
args{
func() *settings.Base {
v := viper.New()
v.Set("server.port", 99999)
config := settings.FromStore(v)
return config
}(),
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := settingsViewAll(tt.args.config)
err := got.Execute()
if (err != nil) != tt.wantErr {
t.Errorf("settingsViewAll().Execute error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
4 changes: 2 additions & 2 deletions cmd/mailchain/internal/http/handlers/protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func GetProtocols(base *settings.Base) func(w http.ResponseWriter, r *http.Reque
}
networks := []string{}
for _, network := range protocol.Networks {
if !network.Disabled.Get() {
networks = append(networks, network.Kind)
if !network.Disabled() {
networks = append(networks, network.Kind())
}
}
sort.Strings(networks)
Expand Down
6 changes: 3 additions & 3 deletions cmd/mailchain/internal/http/handlers/protocols_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGetProtocols(t *testing.T) {
m.EXPECT().IsSet("protocols.ethereum.disabled").Return(true)
m.EXPECT().GetBool("protocols.ethereum.disabled").Return(true)
m.EXPECT().IsSet(gomock.Any()).Return(false).AnyTimes()
return settings.New(m)
return settings.FromStore(m)
}(),
},
"{\"protocols\":[]}\n",
Expand All @@ -60,7 +60,7 @@ func TestGetProtocols(t *testing.T) {
m.EXPECT().IsSet("protocols.ethereum.networks.goerli.disabled").Return(true)
m.EXPECT().GetBool("protocols.ethereum.networks.goerli.disabled").Return(true)
m.EXPECT().IsSet(gomock.Any()).Return(false).AnyTimes()
return settings.New(m)
return settings.FromStore(m)
}(),
},
"{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[\"kovan\",\"mainnet\",\"rinkeby\",\"ropsten\"]}]}\n",
Expand All @@ -72,7 +72,7 @@ func TestGetProtocols(t *testing.T) {
func() *settings.Base {
m := valuestest.NewMockStore(mockCtrl)
m.EXPECT().IsSet(gomock.Any()).Return(false).AnyTimes()
return settings.New(m)
return settings.FromStore(m)
}(),
},
"{\"protocols\":[{\"name\":\"ethereum\",\"networks\":[\"goerli\",\"kovan\",\"mainnet\",\"rinkeby\",\"ropsten\"]}]}\n",
Expand Down
5 changes: 3 additions & 2 deletions cmd/mailchain/internal/http/handlers/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import (
func GetPublicKey(finders map[string]mailbox.PubKeyFinder) func(w http.ResponseWriter, r *http.Request) {
// Get swagger:route GET /public-key PublicKey GetPublicKey
//
// Get public key from an address.
// Public key from address.
//
// Get the public key.
// This method will get the public key to use when encrypting messages and envelopes.
// Protocols and networks have different methods for retrieving or calculating a public key from an address.
//
// Responses:
// 200: GetPublicKeyResponse
Expand Down
4 changes: 3 additions & 1 deletion cmd/mailchain/internal/http/handlers/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func GetRead(store stores.State) func(w http.ResponseWriter, r *http.Request) {
}
// Get swagger:route GET /messages/{message_id}/read Messages GetRead
//
// Get message read status.
// Message read status.
//
// Messages can be either read or unread.
//
// Responses:
// 200: GetReadResponse
Expand Down
31 changes: 29 additions & 2 deletions cmd/mailchain/internal/settings/base.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package settings

import (
"io"

"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/output"
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/values"
"github.com/mailchain/mailchain/internal/chains"
)

// TODO: maybe this is not a new?
func New(s values.Store) *Base {
func FromStore(s values.Store) *Base {
return &Base{
DomainNameServices: domainNameServices(s),
AddressNameServices: addressNameServices(s),
Senders: senders(s),
Receivers: receivers(s),
PublicKeyFinders: publicKeyFinders(s),
// Protocols these contain the networks
Protocols: map[string]*Protocol{
chains.Ethereum: protocol(s, chains.Ethereum),
},
Expand All @@ -39,3 +42,27 @@ type Base struct {
SentStore *SentStore
Server *Server
}

func (o *Base) ToYaml(out io.Writer, tabsize int, commentDefaults, excludeDefaults bool) {
protocols := []output.Element{}
for _, v := range o.Protocols {
protocols = append(protocols, v.Output())
}

output.ToYaml(output.Root{
Elements: append(
protocols,
o.AddressNameServices.Output(),
o.DomainNameServices.Output(),

o.PublicKeyFinders.Output(),
o.Receivers.Output(),
o.Senders.Output(),

o.Keystore.Output(),
o.MailboxState.Output(),
o.SentStore.Output(),
o.Server.Output(),
),
}, out, 2, commentDefaults, excludeDefaults)
}
Loading

0 comments on commit d25a45c

Please sign in to comment.