Skip to content

Commit

Permalink
correct return codes in record tool
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 13, 2024
1 parent ab465ef commit 8ebb779
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
5 changes: 2 additions & 3 deletions auth/rod.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func NewRODAuth(ctx context.Context, opts ...Option) (RodAuth, error) {
if err != nil {
return r, err
}
fmt.Fprintln(os.Stderr, "authenticated.")
case auth_ui.LCancel:
return r, ErrCancelled
}

fmt.Fprintln(os.Stderr, "authenticated.")

return RodAuth{
simpleProvider: sp,
}, nil
Expand Down Expand Up @@ -128,7 +129,5 @@ func headlessFlow(ctx context.Context, workspace string, ui browserAuthUIExt) (s
if loginErr != nil {
return sp, loginErr
}

fmt.Fprintln(os.Stderr, "authenticated.")
return
}
23 changes: 21 additions & 2 deletions cmd/slackdump/internal/diag/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ var output = CmdRecordStream.Flag.String("output", "", "output file")

func runRecord(ctx context.Context, _ *base.Command, args []string) error {
if len(args) == 0 {
base.SetExitStatus(base.SInvalidParameters)
return errors.New("missing channel argument")
}

prov, err := auth.FromContext(ctx)
if err != nil {
base.SetExitStatus(base.SAuthError)
return err
}
sess, err := slackdump.New(ctx, prov)
if err != nil {
base.SetExitStatus(base.SWorkspaceError)
return err
}

Expand All @@ -70,6 +73,7 @@ func runRecord(ctx context.Context, _ *base.Command, args []string) error {
w = os.Stdout
} else {
if f, err := os.Create(*output); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
} else {
defer f.Close()
Expand All @@ -82,19 +86,26 @@ func runRecord(ctx context.Context, _ *base.Command, args []string) error {
cfg.Log.Printf("streaming channel %q", ch)
if err := sess.Stream().SyncConversations(ctx, rec, ch); err != nil {
if err2 := rec.Close(); err2 != nil {
base.SetExitStatus(base.SApplicationError)
return fmt.Errorf("error streaming channel %q: %w; error closing recorder: %v", ch, err, err2)
}
return err
}
}
if err := rec.Close(); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
st, err := rec.State()
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
return st.Save(*output + ".state")
if err := st.Save(*output + ".state"); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
return nil
}

func init() {
Expand All @@ -104,23 +115,31 @@ func init() {

func runRecordState(ctx context.Context, _ *base.Command, args []string) error {
if len(args) == 0 {
base.SetExitStatus(base.SInvalidParameters)
return errors.New("missing record file argument")
}
f, err := os.Open(args[0])
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
defer f.Close()

cf, err := chunk.FromReader(f)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
state, err := cf.State()
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(state)
if err := enc.Encode(state); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/golang/base/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type StatusCode uint8
const (
SNoError StatusCode = iota
SGenericError
SInvalidParameters
SHelpRequested
SInvalidParameters
SAuthError
SApplicationError
SWorkspaceError
Expand Down
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/golang/base/statuscode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/rusq/encio v0.1.0
github.com/rusq/fsadapter v1.0.1
github.com/rusq/osenv/v2 v2.0.1
github.com/rusq/slackauth v0.0.6
github.com/rusq/slackauth v0.0.7
github.com/rusq/tracer v1.0.1
github.com/schollz/progressbar/v3 v3.13.0
github.com/slack-go/slack v0.12.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ github.com/rusq/osenv/v2 v2.0.1 h1:1LtNt8VNV/W86wb38Hyu5W3Rwqt/F1JNRGE+8GRu09o=
github.com/rusq/osenv/v2 v2.0.1/go.mod h1:+wJBSisjNZpfoD961JzqjaM+PtaqSusO3b4oVJi7TFY=
github.com/rusq/secure v0.0.4 h1:svpiZHfHnx89eEDCCFI9OXG1Y8hL9kUWUG6fJbrWUOI=
github.com/rusq/secure v0.0.4/go.mod h1:F1QilMKreuFRjov0UY7DZSIXn77/8RqMVGu2zV0RtqY=
github.com/rusq/slackauth v0.0.6 h1:vV4kg3lRKV+oiHVAWxyKXa9aoRU4XwT5pSQ0mlo9OSM=
github.com/rusq/slackauth v0.0.6/go.mod h1:zb1PJY2+8uEqn0RiWuRjnd+ZFwwfnvA5xrGoooVUgNY=
github.com/rusq/slackauth v0.0.7 h1:wAzwR3rXf63lPM+f2IvgV5M2W3G93k2Lz44aclS3KAE=
github.com/rusq/slackauth v0.0.7/go.mod h1:zb1PJY2+8uEqn0RiWuRjnd+ZFwwfnvA5xrGoooVUgNY=
github.com/rusq/tracer v1.0.1 h1:5u4PCV8NGO97VuAINQA4gOVRkPoqHimLE2jpezRVNMU=
github.com/rusq/tracer v1.0.1/go.mod h1:Rqu48C3/K8bA5NPmF20Hft73v431MQIdM+Co+113pME=
github.com/schollz/progressbar/v3 v3.13.0 h1:9TeeWRcjW2qd05I8Kf9knPkW4vLM/hYoa6z9ABvxje8=
Expand Down
8 changes: 4 additions & 4 deletions internal/format/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func (txt *Text) txtConversations(w io.Writer, m []types.Message, prefix string,
}

func (txt *Text) Users(ctx context.Context, w io.Writer, u []slack.User) error {
const strFormat = "%s\t%s\t%s\t%s\t%s\n"
const strFormat = "%s\t%s\t%s\t%s\t%s\t%s\n"
writer := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
defer writer.Flush()

// header
if _, err := fmt.Fprintf(writer, strFormat, "Name", "ID", "Bot?", "Deleted?", "Restricted?"); err != nil {
if _, err := fmt.Fprintf(writer, strFormat, "Name", "ID", "Bot?", "Email", "Deleted?", "Restricted?"); err != nil {
return fmt.Errorf("writer error: %w", err)
}
if _, err := fmt.Fprintf(writer, strFormat, "", "", "", "", ""); err != nil {
if _, err := fmt.Fprintf(writer, strFormat, "", "", "", "", "", ""); err != nil {
return fmt.Errorf("writer error: %w", err)
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func (txt *Text) Users(ctx context.Context, w io.Writer, u []slack.User) error {
}

_, err := fmt.Fprintf(writer, strFormat,
name, usermap[name].ID, bot, deleted, restricted,
name, usermap[name].ID, bot, usermap[name].Profile.Email, deleted, restricted,
)
if err != nil {
return fmt.Errorf("writer error: %w", err)
Expand Down

0 comments on commit 8ebb779

Please sign in to comment.