Skip to content

Commit

Permalink
refactor Options
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Nov 8, 2022
1 parent 17ced3e commit c1cb7cf
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 254 deletions.
6 changes: 3 additions & 3 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (sd *Session) getChannels(ctx context.Context, chanTypes []string, cb func(
ctx, task := trace.NewTask(ctx, "getChannels")
defer task.End()

limiter := network.NewLimiter(network.Tier2, sd.options.Tier2Burst, int(sd.options.Tier2Boost))
limiter := network.NewLimiter(network.Tier2, sd.options.Limits.Tier2.Burst, int(sd.options.Limits.Tier2.Boost))

if chanTypes == nil {
chanTypes = AllChanTypes
}

params := &slack.GetConversationsParameters{Types: chanTypes, Limit: sd.options.ChannelsPerReq}
params := &slack.GetConversationsParameters{Types: chanTypes, Limit: sd.options.Limits.Request.Channels}
fetchStart := time.Now()
var total int
for i := 1; ; i++ {
Expand All @@ -63,7 +63,7 @@ func (sd *Session) getChannels(ctx context.Context, chanTypes []string, cb func(
nextcur string
)
reqStart := time.Now()
if err := withRetry(ctx, limiter, sd.options.Tier3Retries, func() error {
if err := withRetry(ctx, limiter, sd.options.Limits.Tier3.Retries, func() error {
var err error
trace.WithRegion(ctx, "GetConversationsContext", func() {
chans, nextcur, err = sd.client.GetConversationsContext(ctx, params)
Expand Down
4 changes: 2 additions & 2 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestSession_getChannels(t *testing.T) {
},
func(mc *mockClienter) {
mc.EXPECT().GetConversationsContext(gomock.Any(), &slack.GetConversationsParameters{
Limit: DefOptions.ChannelsPerReq,
Limit: DefOptions.Limits.Request.Channels,
Types: AllChanTypes,
}).Return(types.Channels{
slack.Channel{GroupConversation: slack.GroupConversation{
Expand All @@ -64,7 +64,7 @@ func TestSession_getChannels(t *testing.T) {
},
func(mc *mockClienter) {
mc.EXPECT().GetConversationsContext(gomock.Any(), &slack.GetConversationsParameters{
Limit: DefOptions.ChannelsPerReq,
Limit: DefOptions.Limits.Request.Channels,
Types: AllChanTypes,
}).Return(
nil,
Expand Down
10 changes: 5 additions & 5 deletions cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ configuration file.
var ErrConfigInvalid = errors.New("config validation failed")

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

var opts slackdump.Options
var limits slackdump.Limits
dec := yaml.NewDecoder(f, yaml.DisallowUnknownField(), yaml.DisallowDuplicateKey())
if err := dec.Decode(&opts); err != nil {
if err := dec.Decode(&limits); err != nil {
return nil, err
}

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

func printErrors(w io.Writer, err error) error {
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/diag/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func generateThread(ctx context.Context, client *slack.Client, channelID string,
return fmt.Errorf("failed to send initial message: %w", err)
}

l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Tier3Burst, int(slackdump.DefOptions.Tier3Boost))
l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Limits.Tier3.Burst, int(slackdump.DefOptions.Limits.Tier3.Boost))
pb := progressbar.Default(int64(numMsg))
pb.Describe("posting messages")
defer pb.Finish()
Expand Down Expand Up @@ -151,7 +151,7 @@ func delMessages(ctx context.Context, client *slack.Client, channelID string, ms

defer pb.Finish()

l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Tier3Burst, int(slackdump.DefOptions.Tier3Boost))
l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Limits.Tier3.Burst, int(slackdump.DefOptions.Limits.Tier3.Boost))
for _, m := range msgs {
err := network.WithRetry(ctx, l, 3, func() error {
_, _, err := client.DeleteMessageContext(ctx, channelID, m.Timestamp)
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/dump/assets/dump.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ slackdump dump -files C051D4052 \

_Windows users_: please note that "\" is used on UNIX systems to split the
single command across multiple lines. The same command can be entered on a
single line without "\" and will have the same effect as the one above.
single line with "\" removed, and will have the same effect as the one above.
26 changes: 13 additions & 13 deletions cmd/slackdump/internal/v1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,24 +338,24 @@ func parseCmdLine(args []string) (params, error) {
// - file download options
fs.BoolVar(&p.appCfg.Options.DumpFiles, "f", slackdump.DefOptions.DumpFiles, "same as -download")
fs.BoolVar(&p.appCfg.Options.DumpFiles, "download", slackdump.DefOptions.DumpFiles, "enable files download.")
fs.IntVar(&p.appCfg.Options.Workers, "download-workers", slackdump.DefOptions.Workers, "number of file download worker threads.")
fs.IntVar(&p.appCfg.Options.DownloadRetries, "dl-retries", slackdump.DefOptions.DownloadRetries, "rate limit retries for file downloads.")
fs.IntVar(&p.appCfg.Options.Limits.Workers, "download-workers", slackdump.DefOptions.Limits.Workers, "number of file download worker threads.")
fs.IntVar(&p.appCfg.Options.Limits.DownloadRetries, "dl-retries", slackdump.DefOptions.Limits.DownloadRetries, "rate limit retries for file downloads.")

// - API request speed
fs.IntVar(&p.appCfg.Options.Tier3Retries, "t3-retries", slackdump.DefOptions.Tier3Retries, "rate limit retries for conversation.")
fs.UintVar(&p.appCfg.Options.Tier3Boost, "t3-boost", slackdump.DefOptions.Tier3Boost, "Tier-3 rate limiter boost in `events` per minute, will be added to the\nbase slack tier event per minute value.")
fs.UintVar(&p.appCfg.Options.Tier3Burst, "t3-burst", slackdump.DefOptions.Tier3Burst, "Tier-3 rate limiter burst, allow up to `N` burst events per second.\nDefault value is safe.")
fs.IntVar(&p.appCfg.Options.Tier2Retries, "t2-retries", slackdump.DefOptions.Tier2Retries, "rate limit retries for channel listing.")
fs.UintVar(&p.appCfg.Options.Tier2Boost, "t2-boost", slackdump.DefOptions.Tier2Boost, "Tier-2 rate limiter boost in `events` per minute\n(affects users and channels).")
fs.UintVar(&p.appCfg.Options.Tier2Burst, "t2-burst", slackdump.DefOptions.Tier2Burst, "Tier-2 rate limiter burst, allow up to `N` burst events per second.\n(affects users and channels).")
fs.IntVar(&p.appCfg.Options.Limits.Tier3.Retries, "t3-retries", slackdump.DefOptions.Limits.Tier3.Retries, "rate limit retries for conversation.")
fs.UintVar(&p.appCfg.Options.Limits.Tier3.Boost, "t3-boost", slackdump.DefOptions.Limits.Tier3.Boost, "Tier-3 rate limiter boost in `events` per minute, will be added to the\nbase slack tier event per minute value.")
fs.UintVar(&p.appCfg.Options.Limits.Tier3.Burst, "t3-burst", slackdump.DefOptions.Limits.Tier3.Burst, "Tier-3 rate limiter burst, allow up to `N` burst events per second.\nDefault value is safe.")
fs.IntVar(&p.appCfg.Options.Limits.Tier2.Retries, "t2-retries", slackdump.DefOptions.Limits.Tier2.Retries, "rate limit retries for channel listing.")
fs.UintVar(&p.appCfg.Options.Limits.Tier2.Boost, "t2-boost", slackdump.DefOptions.Limits.Tier2.Boost, "Tier-2 rate limiter boost in `events` per minute\n(affects users and channels).")
fs.UintVar(&p.appCfg.Options.Limits.Tier2.Burst, "t2-burst", slackdump.DefOptions.Limits.Tier2.Burst, "Tier-2 rate limiter burst, allow up to `N` burst events per second.\n(affects users and channels).")

fs.UintVar(&p.appCfg.Options.Tier3Boost, "limiter-boost", slackdump.DefOptions.Tier3Boost, "same as -t3-boost.")
fs.UintVar(&p.appCfg.Options.Tier3Burst, "limiter-burst", slackdump.DefOptions.Tier3Burst, "same as -t3-burst.")
fs.UintVar(&p.appCfg.Options.Limits.Tier3.Boost, "limiter-boost", slackdump.DefOptions.Limits.Tier3.Boost, "same as -t3-boost.")
fs.UintVar(&p.appCfg.Options.Limits.Tier3.Burst, "limiter-burst", slackdump.DefOptions.Limits.Tier3.Burst, "same as -t3-burst.")

// - API request size
fs.IntVar(&p.appCfg.Options.ConversationsPerReq, "cpr", slackdump.DefOptions.ConversationsPerReq, "number of conversation `items` per request.")
fs.IntVar(&p.appCfg.Options.ChannelsPerReq, "npr", slackdump.DefOptions.ChannelsPerReq, "number of `channels` per request.")
fs.IntVar(&p.appCfg.Options.RepliesPerReq, "rpr", slackdump.DefOptions.RepliesPerReq, "number of `replies` per request.")
fs.IntVar(&p.appCfg.Options.Limits.Request.Conversations, "cpr", slackdump.DefOptions.Limits.Request.Conversations, "number of conversation `items` per request.")
fs.IntVar(&p.appCfg.Options.Limits.Request.Channels, "npr", slackdump.DefOptions.Limits.Request.Channels, "number of `channels` per request.")
fs.IntVar(&p.appCfg.Options.Limits.Request.Replies, "rpr", slackdump.DefOptions.Limits.Request.Replies, "number of `replies` per request.")

// - cache controls
fs.StringVar(&p.appCfg.Options.CacheDir, "cache-dir", cfg.CacheDir(), "slackdump cache directory")
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ func parseFlags(cmd *base.Command, args []string) ([]string, error) {
}

// load the API limit configuration file.
opts, err := apiconfig.Load(cfg.ConfigFile)
limits, err := apiconfig.Load(cfg.ConfigFile)
if err != nil {
return nil, err
}
if err := cfg.SlackOptions.Apply(*opts); err != nil {
if err := cfg.SlackOptions.Limits.Apply(*limits); err != nil {
return nil, err
}
return cmd.Flag.Args(), nil
Expand Down
2 changes: 1 addition & 1 deletion internal/app/appauth/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (m *Manager) name(filename string) (string, error) {
func (m *Manager) readWsp(r io.Reader) string {
var current string
if _, err := fmt.Fscanln(r, &current); err != nil {
return filepath.Join(m.dir, defCredsFile)
return defCredsFile
}
return strings.TrimSpace(current)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/app/appauth/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func testFiles(dir string) []string {
return files
}

func TestManager_List(t *testing.T) {
func TestManager_listFiles(t *testing.T) {
tests := []struct {
name string
prepFn func(t *testing.T, dir string)
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestManager_List(t *testing.T) {
if tt.prepFn != nil {
tt.prepFn(t, tempdir)
}
got, err := m.List()
got, err := m.listFiles()
if !tt.wantErr(t, err, fmt.Sprintf("List()")) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ func (sd *Session) dumpChannel(ctx context.Context, channelID string, oldest, la
resp *slack.GetConversationHistoryResponse
)
reqStart := time.Now()
if err := withRetry(ctx, convLimiter, sd.options.Tier3Retries, func() error {
if err := withRetry(ctx, convLimiter, sd.options.Limits.Tier3.Retries, func() error {
var err error
trace.WithRegion(ctx, "GetConversationHistoryContext", func() {
resp, err = sd.client.GetConversationHistoryContext(ctx, &slack.GetConversationHistoryParameters{
ChannelID: channelID,
Cursor: cursor,
Limit: sd.options.ConversationsPerReq,
Limit: sd.options.Limits.Request.Conversations,
Oldest: structures.FormatSlackTS(oldest),
Latest: structures.FormatSlackTS(latest),
Inclusive: true,
Expand Down Expand Up @@ -168,7 +168,7 @@ func (sd *Session) dumpChannel(ctx context.Context, channelID string, oldest, la
func (sd *Session) getChannelName(ctx context.Context, l *rate.Limiter, channelID string) (string, error) {
// get channel name
var ci *slack.Channel
if err := withRetry(ctx, l, sd.options.Tier3Retries, func() error {
if err := withRetry(ctx, l, sd.options.Limits.Tier3.Retries, func() error {
var err error
ci, err = sd.client.GetConversationInfoContext(ctx, channelID, false)
return err
Expand Down
6 changes: 3 additions & 3 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestSession_DumpMessages(t *testing.T) {
gomock.Any(),
&slack.GetConversationHistoryParameters{
ChannelID: "CHANNEL",
Limit: DefOptions.ConversationsPerReq,
Limit: DefOptions.Limits.Request.Conversations,
Inclusive: true,
}).Return(
&slack.GetConversationHistoryResponse{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestSession_DumpMessages(t *testing.T) {
gomock.Any(),
&slack.GetConversationHistoryParameters{
ChannelID: "CHANNEL",
Limit: DefOptions.ConversationsPerReq,
Limit: DefOptions.Limits.Request.Conversations,
Inclusive: true,
}).
Return(
Expand All @@ -154,7 +154,7 @@ func TestSession_DumpMessages(t *testing.T) {
&slack.GetConversationHistoryParameters{
ChannelID: "CHANNEL",
Cursor: "cur",
Limit: DefOptions.ConversationsPerReq,
Limit: DefOptions.Limits.Request.Conversations,
Inclusive: true,
}).
Return(
Expand Down
Loading

0 comments on commit c1cb7cf

Please sign in to comment.