Skip to content

Commit

Permalink
Merge fb0dc58 into b941e98
Browse files Browse the repository at this point in the history
  • Loading branch information
robdefeo committed Oct 24, 2019
2 parents b941e98 + fb0dc58 commit 1f04e55
Show file tree
Hide file tree
Showing 55 changed files with 645 additions and 135 deletions.
5 changes: 4 additions & 1 deletion Makefile
Expand Up @@ -39,10 +39,13 @@ openapi:

echo "package handlers" > ./cmd/mailchain/internal/http/handlers/openapi.go
echo "" >> ./cmd/mailchain/internal/http/handlers/openapi.go

echo "// nolint: gofmt" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo "// nolint: lll" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo "// nolint: funlen" >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo 'func spec() string {' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo ' return `' >> ./cmd/mailchain/internal/http/handlers/openapi.go
cat ./docs/openapi/spec.json >> ./cmd/mailchain/internal/http/handlers/openapi.go
cat ./docs/openapi/spec.json | sed 's/`/¬/g' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo '`' >> ./cmd/mailchain/internal/http/handlers/openapi.go
echo '}' >> ./cmd/mailchain/internal/http/handlers/openapi.go
addlicense -l apache -c Finobo ./cmd/mailchain/internal/http/handlers/openapi.go
Expand Down
12 changes: 7 additions & 5 deletions cmd/mailchain/commands/commandstest/test_util.go
Expand Up @@ -9,30 +9,32 @@ import (
)

func ExecuteCommandC(root *cobra.Command, args []string, flags map[string]string) (c *cobra.Command, output string, err error) {
buf := new(bytes.Buffer)
if err := root.ValidateArgs(args); err != nil {
return nil, "", err
}

buf := new(bytes.Buffer)

root.SetOutput(buf)
root.SetArgs(args)

for x := range flags {
root.Flags().Set(x, flags[x])
_ = root.Flags().Set(x, flags[x])
}
c, err = root.ExecuteC()

return c, buf.String(), err
}

func AssertCommandOutput(t *testing.T, cmd *cobra.Command, err error, out, wantOutput string) bool {
assert := assert.New(t)
if err == nil {
if !assert.Equal(wantOutput, out) {
if !assert.Equal(t, wantOutput, out) {
t.Errorf("cmd().Execute().out = %v, want %v", out, wantOutput)
return false
}
}
if err != nil {
if !assert.Equal(wantOutput+"\n"+cmd.UsageString()+"\n", out) {
if !assert.Equal(t, wantOutput+"\n"+cmd.UsageString()+"\n", out) {
t.Errorf("cmd().Execute().out = %v, want %v", out, wantOutput)
return false
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/http/config.go
Expand Up @@ -22,7 +22,7 @@ type config struct {
}

// nolint: gocyclo
func produceConfig(s *settings.Base) (*config, error) {
func produceConfig(s *settings.Base) (*config, error) { // nolint: funlen
mailboxStore, err := s.MailboxState.Produce()
if err != nil {
return nil, errors.WithMessage(err, "Could not config mailbox store")
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/http/handlers/messages.go
Expand Up @@ -32,6 +32,7 @@ import (
)

// nolint: gocyclo
// nolint: funlen
// GetMessages returns a handler get spec
func GetMessages(inbox stores.State, receivers map[string]mailbox.Receiver, ks keystore.Store,
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -43,7 +44,6 @@ func GetMessages(inbox stores.State, receivers map[string]mailbox.Receiver, ks k
// Responses:
// 200: GetMessagesResponse
// 422: ValidationError

return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
req, err := parseGetMessagesRequest(r)
Expand Down
41 changes: 36 additions & 5 deletions cmd/mailchain/internal/http/handlers/openapi.go
Expand Up @@ -14,7 +14,9 @@

package handlers

// nolint: gofmt
// nolint: lll
// nolint: funlen
func spec() string {
return `
{
Expand Down Expand Up @@ -174,10 +176,11 @@ func spec() string {
},
"/messages/{message_id}/read": {
"get": {
"description": "Messages can be either read or unread.",
"tags": [
"Messages"
],
"summary": "Get message read status.",
"summary": "Message read status.",
"operationId": "GetRead",
"responses": {
"200": {
Expand Down Expand Up @@ -394,11 +397,11 @@ func spec() string {
},
"/public-key": {
"get": {
"description": "Get the public key.",
"description": "This method will get the public key to use when encrypting messages and envelopes.\nProtocols and networks have different methods for retrieving or calculating a public key from an address.",
"tags": [
"PublicKey"
],
"summary": "Get public key from an address.",
"summary": "Public key from address.",
"operationId": "GetPublicKey",
"parameters": [
{
Expand Down Expand Up @@ -476,6 +479,13 @@ func spec() string {
"GetMessagesResponseHeaders": {
"type": "object",
"properties": {
"content-type": {
"description": "The content type and the encoding of the message body",
"type": "string",
"x-go-name": "ContentType",
"readOnly": true,
"example": "text/plain; charset=\\\"UTF-8\\\","
},
"date": {
"description": "When the message was created, this can be different to the transaction data of the message.",
"type": "string",
Expand Down Expand Up @@ -578,14 +588,21 @@ func spec() string {
"description": "GetBody body response",
"type": "object",
"required": [
"public_key"
"public_key",
"public_key_encoding"
],
"properties": {
"public_key": {
"description": "The public key",
"description": "The public key encoded as per ¬public_key_encoding¬",
"type": "string",
"x-go-name": "PublicKey",
"example": "0x79964e63752465973b6b3c610d8ac773fc7ce04f5d1ba599ba8768fb44cef525176f81d3c7603d5a2e466bc96da7b2443bef01b78059a98f45d5c440ca379463"
},
"public_key_encoding": {
"description": "Encoding method used for encoding the ¬public_key¬",
"type": "string",
"x-go-name": "PublicKeyEncoding",
"example": "hex/0x-prefix"
}
},
"x-go-package": "github.com/mailchain/mailchain/cmd/mailchain/internal/http/handlers"
Expand Down Expand Up @@ -618,6 +635,13 @@ func spec() string {
"type": "string",
"x-go-name": "Name",
"example": "mailchain.eth"
},
"status": {
"description": "The rfc1035 error status, if present\nSince 0 status belongs to 'No Error', it's safe to use 'omitempty'",
"type": "integer",
"format": "int64",
"x-go-name": "Status",
"example": 3
}
},
"x-go-package": "github.com/mailchain/mailchain/cmd/mailchain/internal/http/handlers"
Expand All @@ -634,6 +658,13 @@ func spec() string {
"type": "string",
"x-go-name": "Address",
"example": "0x4ad2b251246aafc2f3bdf3b690de3bf906622c51"
},
"status": {
"description": "The rfc1035 error status, if present\nSince 0 status belongs to 'No Error', it's safe to use 'omitempty'",
"type": "integer",
"format": "int64",
"x-go-name": "Status",
"example": 3
}
},
"x-go-package": "github.com/mailchain/mailchain/cmd/mailchain/internal/http/handlers"
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/http/handlers/protocols.go
Expand Up @@ -32,6 +32,7 @@ func GetProtocols(base *settings.Base) func(w http.ResponseWriter, r *http.Reque
if protocol.Disabled.Get() {
continue
}

networks := []string{}
for _, network := range protocol.Networks {
if !network.Disabled() {
Expand Down
3 changes: 1 addition & 2 deletions cmd/mailchain/internal/http/handlers/send_message.go
Expand Up @@ -39,7 +39,7 @@ import (

// SendMessage handler http
func SendMessage(sent stores.Sent, senders map[string]sender.Message, ks keystore.Store,
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) {
deriveKeyOptions multi.OptionsBuilders) func(w http.ResponseWriter, r *http.Request) { // nolint: funlen
encrypter := aes256cbc.NewEncrypter()
// Post swagger:route POST /messages Send SendMessage
//
Expand Down Expand Up @@ -104,7 +104,6 @@ func SendMessage(sent stores.Sent, senders map[string]sender.Message, ks keystor
}

w.WriteHeader(http.StatusOK)
return
}
}

Expand Down
10 changes: 8 additions & 2 deletions cmd/mailchain/internal/http/handlers/spec.go
Expand Up @@ -14,12 +14,18 @@

package handlers

import "net/http"
import (
"net/http"
"strings"
)

// GetSpec returns a handler get spec
func GetSpec() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(spec()))
var mdreplacer = strings.NewReplacer("¬", "`")

doc := mdreplacer.Replace(spec())
_, _ = w.Write([]byte(doc))
}
}
1 change: 0 additions & 1 deletion cmd/mailchain/internal/http/handlers/version.go
Expand Up @@ -17,7 +17,6 @@ func GetVersion() func(w http.ResponseWriter, r *http.Request) {
//
// Responses:
// 200: GetVersionResponseBody

return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(GetVersionResponseBody{
Expand Down
4 changes: 1 addition & 3 deletions cmd/mailchain/internal/http/server.go
Expand Up @@ -92,16 +92,14 @@ func SetupFlags(cmd *cobra.Command) error {
if err := viper.BindPFlag("server.cors.disabled", cmd.Flags().Lookup("cors-disabled")); err != nil {
return err
}
// if err := viper.BindPFlag("server.cors.allowed-origins", cmd.Flags().Lookup("cors-allowed-origins")); err != nil {
// return err
// }

cmd.PersistentFlags().String("passphrase", "", "Passphrase to encrypt/decrypt key with")
return nil
}

func CreateNegroni(config *settings.Server, router http.Handler) *negroni.Negroni {
n := negroni.New()

if !config.CORS.Disabled.Get() {
n.Use(cors.New(cors.Options{
AllowedOrigins: config.CORS.AllowedOrigins.Get(),
Expand Down
6 changes: 5 additions & 1 deletion cmd/mailchain/internal/prompts/secret.go
Expand Up @@ -15,24 +15,28 @@ func Secret(suppliedSecret, prePromptNote, promptLabel string, allowEmpty, confi
if allowEmpty {
return "", nil
}

fmt.Println(prePromptNote)
return secretFromPrompt(promptLabel, confirmPrompt)
}

func secretFromPrompt(promptLabel string, confirmPrompt bool) (string, error) {
prompt := promptui.Prompt{
Label: fmt.Sprintf("%s", promptLabel),
Label: promptLabel,
Mask: '*',
}

secret, err := prompt.Run()
if err != nil {
return "", errors.Errorf("failed read %q", promptLabel)
}

if confirmPrompt {
confirmPromptValue := promptui.Prompt{
Label: fmt.Sprintf("Repeat %s", promptLabel),
Mask: '*',
}

confirm, err := confirmPromptValue.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/prompts/select.go
Expand Up @@ -29,6 +29,7 @@ func SelectItemSkipable(label string, items []string, skipable bool) (selected s
if skipable {
items = append([]string{"skip"}, items...)
}

prompt := promptui.Select{
Label: label,
Items: items,
Expand Down
1 change: 1 addition & 0 deletions cmd/mailchain/internal/settings/base.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/output"
"github.com/mailchain/mailchain/cmd/mailchain/internal/settings/values"
"github.com/mailchain/mailchain/internal/protocols"

"github.com/mailchain/mailchain/internal/protocols/ethereum"
"github.com/mailchain/mailchain/internal/protocols/substrate"
)
Expand Down
8 changes: 4 additions & 4 deletions cmd/mailchain/internal/settings/defaults/networks.go
Expand Up @@ -13,8 +13,8 @@ type NetworkDefaults struct {
Disabled bool
}

func EthereumNetworkAny() NetworkDefaults {
return NetworkDefaults{
func EthereumNetworkAny() *NetworkDefaults {
return &NetworkDefaults{
NameServiceAddress: NameServiceAddressKind,
NameServiceDomainName: NameServiceDomainNameKind,
PublicKeyFinder: mailchain.ClientEtherscanNoAuth,
Expand All @@ -24,8 +24,8 @@ func EthereumNetworkAny() NetworkDefaults {
}
}

func SubstrateNetworkAny() NetworkDefaults {
return NetworkDefaults{
func SubstrateNetworkAny() *NetworkDefaults {
return &NetworkDefaults{
// NameServiceAddress: NameServiceAddressKind,
// NameServiceDomainName: NameServiceDomainNameKind,
PublicKeyFinder: SubstratePublicKeyFinder,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/settings/network.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/mailchain/mailchain/sender"
)

func network(s values.Store, protocol, network string, nd defaults.NetworkDefaults) *Network {
func network(s values.Store, protocol, network string, nd *defaults.NetworkDefaults) *Network {
k := &Network{
kind: network,
protocol: protocol,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mailchain/internal/settings/network_test.go
Expand Up @@ -18,7 +18,7 @@ func Test_network(t *testing.T) {
s values.Store
protocol string
network string
nd defaults.NetworkDefaults
nd *defaults.NetworkDefaults
}
tests := []struct {
name string
Expand Down
3 changes: 3 additions & 0 deletions cmd/mailchain/internal/settings/output/structure.go
Expand Up @@ -19,18 +19,21 @@ func (e Element) SortedElements() []Element {
sort.Slice(e.Elements, func(i, j int) bool {
return e.Elements[i].FullName < e.Elements[j].FullName
})

return e.Elements
}

func (e Element) SortedAttributes() []Attribute {
sort.Slice(e.Attributes, func(i, j int) bool {
return e.Attributes[i].FullName < e.Attributes[j].FullName
})

return e.Attributes
}

func (e Element) ShortName() string {
dots := strings.Split(e.FullName, ".")

return dots[len(dots)-1]
}

Expand Down

0 comments on commit 1f04e55

Please sign in to comment.