Skip to content

Commit

Permalink
refactor(config): RPC > HTTP & API for config
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Mar 5, 2021
1 parent f171fd8 commit a114a3d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 40 deletions.
6 changes: 4 additions & 2 deletions cmd/completion.go
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"context"
"fmt"
"io"

Expand Down Expand Up @@ -96,9 +97,10 @@ func (o *ConfigOptions) GetConfig(args []string) (err error) {
params.Field = args[0]
}

var data []byte
ctx := context.TODO()

if err = o.ConfigMethods.GetConfigKeys(params, &data); err != nil {
data, err := o.ConfigMethods.GetConfigKeys(ctx, params)
if err != nil {
return err
}

Expand Down
17 changes: 13 additions & 4 deletions cmd/config.go
@@ -1,6 +1,8 @@
package cmd

import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -159,9 +161,13 @@ func (o *ConfigOptions) Get(args []string) (err error) {
params.Field = args[0]
}

var data []byte
ctx := context.TODO()

if err = o.ConfigMethods.GetConfig(params, &data); err != nil {
data, err := o.ConfigMethods.GetConfig(ctx, params)
if err != nil {
if errors.Is(err, lib.ErrUnsupportedRPC) {
return fmt.Errorf("%w - this could mean you're running qri connect in another terminal or application", err)
}
return err
}

Expand Down Expand Up @@ -214,8 +220,11 @@ func (o *ConfigOptions) Set(args []string) (err error) {
}
}
}
var ok bool
if err = o.ConfigMethods.SetConfig(o.inst.Config(), &ok); err != nil {
ctx := context.TODO()
if _, err := o.ConfigMethods.SetConfig(ctx, o.inst.Config()); err != nil {
if errors.Is(err, lib.ErrUnsupportedRPC) {
return fmt.Errorf("%w - this could mean you're running qri connect in another terminal or application", err)
}
return err
}
if profileChanged {
Expand Down
51 changes: 30 additions & 21 deletions lib/config.go
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -34,14 +35,15 @@ type GetConfigParams struct {

// GetConfig returns the Config, or one of the specified fields of the Config,
// as a slice of bytes the bytes can be formatted as json, concise json, or yaml
func (m *ConfigMethods) GetConfig(p *GetConfigParams, res *[]byte) (err error) {
if m.inst.rpc != nil {
return checkRPCError(m.inst.rpc.Call("ConfigMethods.GetConfig", p, res))
func (m *ConfigMethods) GetConfig(ctx context.Context, p *GetConfigParams) ([]byte, error) {
if m.inst.http != nil {
return nil, ErrUnsupportedRPC
}

var (
cfg = m.inst.cfg
encode interface{}
err error
)

if !p.WithPrivateKey {
Expand All @@ -55,37 +57,40 @@ func (m *ConfigMethods) GetConfig(p *GetConfigParams, res *[]byte) (err error) {
if p.Field != "" {
encode, err = cfg.Get(p.Field)
if err != nil {
return fmt.Errorf("error getting %s from config: %s", p.Field, err)
return nil, fmt.Errorf("error getting %s from config: %w", p.Field, err)
}
}

var res []byte

switch p.Format {
case "json":
if p.Concise {
*res, err = json.Marshal(encode)
res, err = json.Marshal(encode)
} else {
*res, err = json.MarshalIndent(encode, "", " ")
res, err = json.MarshalIndent(encode, "", " ")
}
case "yaml":
*res, err = yaml.Marshal(encode)
res, err = yaml.Marshal(encode)
}
if err != nil {
return fmt.Errorf("error getting config: %s", err)
return nil, fmt.Errorf("error getting config: %w", err)
}

return nil
return res, nil
}

// GetConfigKeys returns the Config key fields, or sub keys of the specified
// fields of the Config, as a slice of bytes to be used for auto completion
func (m *ConfigMethods) GetConfigKeys(p *GetConfigParams, res *[]byte) (err error) {
if m.inst.rpc != nil {
return checkRPCError(m.inst.rpc.Call("ConfigMethods.GetConfigKeys", p, res))
func (m *ConfigMethods) GetConfigKeys(ctx context.Context, p *GetConfigParams) ([]byte, error) {
if m.inst.http != nil {
return nil, ErrUnsupportedRPC
}

var (
cfg = m.inst.cfg
encode interface{}
err error
)

cfg = cfg.WithoutPrivateValues()
Expand All @@ -110,15 +115,14 @@ func (m *ConfigMethods) GetConfigKeys(p *GetConfigParams, res *[]byte) (err erro
parentKey = strings.Join(fieldArgs[:len(fieldArgs)-1], ".")
newEncode, fieldErr := cfg.Get(parentKey)
if fieldErr != nil {
return fmt.Errorf("error getting %s from config: %s", p.Field, err)
return nil, fmt.Errorf("error getting %s from config: %w", p.Field, err)
}
encode = newEncode
}
}
}

*res, err = parseKeys(encode, keyPrefix, parentKey)
return err
return parseKeys(encode, keyPrefix, parentKey)
}

func parseKeys(cfg interface{}, prefix, parentKey string) ([]byte, error) {
Expand Down Expand Up @@ -152,14 +156,19 @@ func parseKeys(cfg interface{}, prefix, parentKey string) ([]byte, error) {
}

// SetConfig validates, updates and saves the config
func (m *ConfigMethods) SetConfig(update *config.Config, set *bool) (err error) {
if m.inst.rpc != nil {
return checkRPCError(m.inst.rpc.Call("ConfigMethods.SetConfig", update, set))
func (m *ConfigMethods) SetConfig(ctx context.Context, update *config.Config) (*bool, error) {
res := false
if m.inst.http != nil {
return &res, ErrUnsupportedRPC
}

if err = update.Validate(); err != nil {
return fmt.Errorf("validating config: %s", err)
if err := update.Validate(); err != nil {
return &res, fmt.Errorf("validating config: %w", err)
}

return m.inst.ChangeConfig(update)
if err := m.inst.ChangeConfig(update); err != nil {
return &res, err
}
res = true
return &res, nil
}
17 changes: 7 additions & 10 deletions lib/config_test.go
Expand Up @@ -32,8 +32,8 @@ func TestGetConfig(t *testing.T) {
m := NewConfigMethods(inst)

p := &GetConfigParams{Field: "profile.id", Format: "json"}
res := []byte{}
if err := m.GetConfig(p, &res); err != nil {
res, err := m.GetConfig(ctx, p)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(res, []byte(`"QmeL2mdVka1eahKENjehK6tBxkkpk5dNQ1qMcgWi7Hrb4B"`)) {
Expand Down Expand Up @@ -66,8 +66,7 @@ func TestSaveConfigToFile(t *testing.T) {
inst := NewInstanceFromConfigAndNode(ctx, cfg, node)
m := NewConfigMethods(inst)

var ok bool
if err := m.SetConfig(cfg, &ok); err != nil {
if _, err := m.SetConfig(ctx, cfg); err != nil {
t.Error(err.Error())
}
}
Expand All @@ -90,19 +89,17 @@ func TestSetConfig(t *testing.T) {
inst := NewInstanceFromConfigAndNode(ctx, cfg, node)
m := NewConfigMethods(inst)

var set bool

if err := m.SetConfig(&config.Config{}, &set); err == nil {
if _, err := m.SetConfig(ctx, &config.Config{}); err == nil {
t.Errorf("expected saving empty config to be invalid")
}

cfg.Profile.Twitter = "@qri_io"
if err := m.SetConfig(cfg, &set); err != nil {
if _, err := m.SetConfig(ctx, cfg); err != nil {
t.Error(err.Error())
}
p := &GetConfigParams{Field: "profile.twitter", Format: "json"}
res := []byte{}
if err := m.GetConfig(p, &res); err != nil {
res, err := m.GetConfig(ctx, p)
if err != nil {
t.Error(err.Error())
}
if !bytes.Equal(res, []byte(`"@qri_io"`)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/datasets_test.go
Expand Up @@ -1155,7 +1155,7 @@ func TestDatasetRequestsValidateFSI(t *testing.T) {
m := NewDatasetMethods(tr.Instance)

vp := &ValidateParams{Ref: refstr}
if _, err = m.Validate(ctx, vp); err != nil {
if _, err := m.Validate(ctx, vp); err != nil {
t.Fatal(err)
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/http.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -14,6 +15,9 @@ import (
"github.com/qri-io/qri/auth/token"
)

// ErrUnsupportedRPC is an error for when running a method that is not supported via HTTP RPC
var ErrUnsupportedRPC = errors.New("Warning: method is not suported over RPC")

const jsonMimeType = "application/json"

// HTTPClient implements the qri http client
Expand Down
2 changes: 1 addition & 1 deletion lib/lib_test.go
Expand Up @@ -187,7 +187,7 @@ func TestReceivers(t *testing.T) {
inst := &Instance{node: node, cfg: cfg}

reqs := Receivers(inst)
expect := 9
expect := 8
if len(reqs) != expect {
t.Errorf("unexpected number of receivers returned. expected: %d. got: %d\nhave you added/removed a receiver?", expect, len(reqs))
return
Expand Down
1 change: 0 additions & 1 deletion lib/rpc.go
Expand Up @@ -36,7 +36,6 @@ func Receivers(inst *Instance) []Methods {
NewLogMethods(inst),
NewPeerMethods(inst),
NewProfileMethods(inst),
NewConfigMethods(inst),
NewSearchMethods(inst),
NewSQLMethods(inst),
NewRenderMethods(inst),
Expand Down

0 comments on commit a114a3d

Please sign in to comment.