Skip to content

Commit

Permalink
extract writeLimits from apiconfig.runNew and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Dec 10, 2022
1 parent 0f04845 commit 0081aa1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 17 deletions.
34 changes: 27 additions & 7 deletions cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,48 @@ configuration file.
var ErrConfigInvalid = errors.New("config validation failed")

// Load reads, parses and validates the config file.
func Load(filename string) (*slackdump.Limits, error) {
func Load(filename string) (slackdump.Limits, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
return slackdump.Limits{}, err
}
defer f.Close()

return readLimits(f)
}

func Save(filename string, limits *slackdump.Limits) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()

return writeLimits(f, limits)
}

func readLimits(r io.Reader) (slackdump.Limits, error) {
var limits slackdump.Limits
dec := yaml.NewDecoder(f, yaml.DisallowUnknownField(), yaml.DisallowDuplicateKey())
dec := yaml.NewDecoder(r, yaml.DisallowUnknownField(), yaml.DisallowDuplicateKey())
if err := dec.Decode(&limits); err != nil {
return nil, err
return slackdump.Limits{}, err
}

if err := cfg.SlackOptions.Limits.Apply(limits); err != nil {
if err := printErrors(os.Stderr, err); err != nil {
return nil, err
return slackdump.Limits{}, err
}
return nil, ErrConfigInvalid
return slackdump.Limits{}, ErrConfigInvalid
}
return &limits, nil
return limits, nil
}

func writeLimits(w io.Writer, cfg *slackdump.Limits) error {
return yaml.NewEncoder(w).Encode(&slackdump.DefOptions.Limits)
}

// printErrors prints configuration errors, if error is not nill and is of
// validator.ValidationErrors type.
func printErrors(w io.Writer, err error) error {
if err == nil {
return nil
Expand Down
71 changes: 71 additions & 0 deletions cmd/slackdump/internal/apiconfig/apiconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package apiconfig

import (
"io"
"reflect"
"strings"
"testing"

"github.com/rusq/slackdump/v2"
)

const sampleLimitsYaml = `workers: 4
download_retries: 3
tier_2:
boost: 20
burst: 1
retries: 20
tier_3:
boost: 120
burst: 1
retries: 3
per_request:
conversations: 100
channels: 100
replies: 200
`

func Test_readConfig(t *testing.T) {
type args struct {
r io.Reader
}
tests := []struct {
name string
args args
want slackdump.Limits
wantErr bool
}{
{
"sample config (ok)",
args{strings.NewReader(sampleLimitsYaml)},
slackdump.DefOptions.Limits,
false,
},
{
"workers invalid",
args{strings.NewReader("workers: -1")},
slackdump.Limits{},
true,
},
{
"one parameter override",
args{strings.NewReader("workers: 55")},
slackdump.Limits{
Workers: 55,
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readLimits(tt.args.r)
if (err != nil) != tt.wantErr {
t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("readConfig() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 1 addition & 9 deletions cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path/filepath"

"github.com/goccy/go-yaml"

"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
Expand Down Expand Up @@ -55,13 +53,7 @@ func runConfigNew(ctx context.Context, cmd *base.Command, args []string) error {
return fmt.Errorf("refusing to overwrite file %q, use -y flag to overwrite", filename)
}

f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("unable to create a new API config file: %w", err)
}
defer f.Close()

if err := yaml.NewEncoder(f).Encode(&slackdump.DefOptions); err != nil {
if err := Save(filename, &slackdump.DefOptions.Limits); err != nil {
base.SetExitStatus(base.SApplicationError)
return fmt.Errorf("error writing the API config %q: %w", filename, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func parseFlags(cmd *base.Command, args []string) ([]string, error) {
if err != nil {
return nil, err
}
if err := cfg.SlackOptions.Limits.Apply(*limits); err != nil {
if err := cfg.SlackOptions.Limits.Apply(limits); err != nil {
return nil, err
}
return cmd.Flag.Args(), nil
Expand Down

0 comments on commit 0081aa1

Please sign in to comment.