Skip to content

Commit 1b21389

Browse files
authored
Merge pull request #423 from rusq/source-detect
Source detection fix
2 parents 54374f9 + 6ddb0ee commit 1b21389

File tree

22 files changed

+1415
-24
lines changed

22 files changed

+1415
-24
lines changed

cmd/slackdump/internal/bootstrap/progress.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package bootstrap
22

33
import (
44
"context"
5+
"fmt"
6+
"io"
57
"log/slog"
8+
"time"
69

710
"github.com/schollz/progressbar/v3"
11+
12+
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
813
)
914

1015
func ProgressBar(ctx context.Context, lg *slog.Logger, opts ...progressbar.Option) *progressbar.ProgressBar {
@@ -28,3 +33,60 @@ func newProgressBar(pb *progressbar.ProgressBar, debug bool) *progressbar.Progre
2833
}
2934
return pb
3035
}
36+
37+
// TimedSpinner displays a spinner for the duration of the operation. It runs
38+
// in a separate goroutine and stops once the stop function is called.
39+
func TimedSpinner(ctx context.Context, w io.Writer, title string, max int, interval time.Duration) (stop func()) {
40+
ctx, cancel := context.WithCancel(ctx)
41+
42+
var wait func()
43+
go func() {
44+
wait = fakeProgress(ctx, w, title, max, interval)
45+
}()
46+
return func() {
47+
cancel()
48+
wait()
49+
}
50+
}
51+
52+
const defaultInterval = 500 * time.Millisecond
53+
54+
// fakeProgress starts a fake spinner and returns a channel that must be closed
55+
// once the operation completes. interval is interval between iterations. If not
56+
// set, will default to 100ms.
57+
func fakeProgress(ctx context.Context, w io.Writer, title string, max int, interval time.Duration) (wait func()) {
58+
if cfg.Log.Enabled(ctx, slog.LevelDebug) {
59+
return func() {}
60+
}
61+
if interval == 0 {
62+
interval = defaultInterval
63+
}
64+
finished := make(chan struct{})
65+
go func() {
66+
bar := progressbar.NewOptions(
67+
max,
68+
progressbar.OptionSetDescription(title),
69+
progressbar.OptionSetPredictTime(false),
70+
progressbar.OptionSpinnerType(61),
71+
progressbar.OptionSetWriter(w),
72+
)
73+
t := time.NewTicker(interval)
74+
defer t.Stop()
75+
76+
for {
77+
select {
78+
case <-ctx.Done():
79+
bar.Clear()
80+
bar.Finish()
81+
fmt.Fprintln(w)
82+
close(finished)
83+
return
84+
case <-t.C:
85+
bar.Add(1)
86+
}
87+
}
88+
}()
89+
return func() {
90+
<-finished
91+
}
92+
}

cmd/slackdump/internal/diag/obfuscate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func runObfuscate(ctx context.Context, cmd *base.Command, args []string) error {
9898
if err := fn(ctx); err != nil {
9999
return err
100100
}
101-
fmt.Println("OK")
101+
fmt.Fprintln(os.Stderr, "OK")
102102
return nil
103103
}
104104

cmd/slackdump/internal/view/view.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9+
"os"
910

1011
"github.com/rusq/slackdump/v3/internal/source"
1112

1213
br "github.com/pkg/browser"
1314

15+
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/bootstrap"
1416
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
1517
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/golang/base"
1618
"github.com/rusq/slackdump/v3/internal/viewer"
@@ -39,26 +41,36 @@ func runView(ctx context.Context, cmd *base.Command, args []string) error {
3941
base.SetExitStatus(base.SInvalidParameters)
4042
return fmt.Errorf("viewing slackdump files requires at least one argument")
4143
}
44+
flags, err := source.Type(args[0])
45+
if err != nil {
46+
base.SetExitStatus(base.SUserError)
47+
return err
48+
}
49+
50+
lg := cfg.Log
51+
lg.InfoContext(ctx, "opening archive", "source", args[0], "flags", flags)
52+
4253
src, err := source.Load(ctx, args[0])
4354
if err != nil {
4455
base.SetExitStatus(base.SUserError)
4556
return err
4657
}
4758
defer src.Close()
4859

60+
stoppb := bootstrap.TimedSpinner(ctx, os.Stdout, "Slackdump Viewer is loading files", -1, 0)
4961
v, err := viewer.New(ctx, listenAddr, src)
5062
if err != nil {
5163
base.SetExitStatus(base.SApplicationError)
5264
return err
5365
}
66+
stoppb()
5467
// sentinel
5568
go func() {
5669
<-ctx.Done()
5770
v.Close()
71+
lg.InfoContext(ctx, "cleanup complete")
5872
}()
5973

60-
lg := cfg.Log
61-
6274
lg.InfoContext(ctx, "listening on", "addr", listenAddr)
6375
go func() {
6476
if err := br.OpenURL(fmt.Sprintf("http://%s", listenAddr)); err != nil {
@@ -67,7 +79,7 @@ func runView(ctx context.Context, cmd *base.Command, args []string) error {
6779
}()
6880
if err := v.ListenAndServe(); err != nil {
6981
if errors.Is(err, http.ErrServerClosed) {
70-
lg.InfoContext(ctx, "bye")
82+
cfg.Log.InfoContext(ctx, "bye")
7183
return nil
7284
}
7385
base.SetExitStatus(base.SApplicationError)

internal/chunk/obfuscate/obfuscate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ import (
1414
"io"
1515
"log"
1616
"math/rand"
17+
"os"
18+
"os/signal"
1719
"runtime/trace"
1820
"strings"
1921
"time"
2022

2123
"github.com/rusq/slack"
24+
2225
"github.com/rusq/slackdump/v3/internal/chunk"
2326
)
2427

@@ -50,7 +53,7 @@ func Do(ctx context.Context, w io.Writer, r io.Reader, options ...Option) error
5053
_, task := trace.NewTask(ctx, "obfuscate.Do")
5154
defer task.End()
5255

53-
var opts = doOpts{
56+
opts := doOpts{
5457
seed: time.Now().UnixNano(),
5558
}
5659
for _, optFn := range options {
@@ -62,6 +65,7 @@ func Do(ctx context.Context, w io.Writer, r io.Reader, options ...Option) error
6265
}
6366

6467
func obfuscate(ctx context.Context, obf obfuscator, w io.Writer, r io.Reader) error {
68+
signal.Reset(os.Interrupt)
6569
var (
6670
dec = json.NewDecoder(r)
6771
enc = json.NewEncoder(w)
@@ -116,7 +120,6 @@ func (o obfuscator) Chunk(c *chunk.Chunk) {
116120
default:
117121
log.Panicf("unknown chunk type: %s", c.Type)
118122
}
119-
120123
}
121124

122125
// obfuscateManyMessages obfuscates a slice of messages.

internal/fixtures/assets/CHY5HUESG.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
5.12 KB
Binary file not shown.
7.51 KB
Binary file not shown.
196 Bytes
Binary file not shown.
3.55 KB
Binary file not shown.

internal/fixtures/assets/source_dump_dir/CHY5HUESG.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)