Skip to content

Commit

Permalink
move dump to list convo
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jan 22, 2023
1 parent 8d82e44 commit c42846c
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 190 deletions.
160 changes: 16 additions & 144 deletions cmd/slackdump/internal/dump/dump.go
Original file line number Diff line number Diff line change
@@ -1,159 +1,31 @@
package dump

import (
"context"
_ "embed"
"encoding/json"
"errors"
"flag"
"fmt"
"runtime/trace"
"strings"
"text/template"
"time"

"github.com/rusq/dlog"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/fsadapter"
"github.com/rusq/slackdump/v2/internal/app/config"
"github.com/rusq/slackdump/v2/internal/app/nametmpl"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/types"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/list"
)

//go:embed assets/dump.md
var dumpMd string
const codeBlock = "```"

// CmdDump is the dump command.
var CmdDump = &base.Command{
UsageLine: "slackdump dump [flags] <IDs or URLs>",
Short: "dump individual conversations or threads",
Long: base.Render(dumpMd),
RequireAuth: true,
PrintFlags: true,
}
UsageLine: "slackdump dump [flags] <IDs or URLs>",
Short: "dump individual conversations or threads",
Long: base.Render(`
# Dump Command
// ErrNothingToDo is returned if there's no links to dump.
var ErrNothingToDo = errors.New("no conversations to dump, run \"slackdump help dump\"")
This command is an alias for:
` + codeBlock + `
slackdump list conversation
` + codeBlock + `
type options struct {
Oldest time.Time // Oldest is the timestamp of the oldest message to fetch.
Latest time.Time // Latest is the timestamp of the newest message to fetch.
NameTemplate string // NameTemplate is the template for the output file name.
To get extended usage help, run ` + "`slackdump help list conversation`"),
RequireAuth: true,
PrintFlags: true,
}

var opts options

// ptr returns a pointer to the given value.
func ptr[T any](a T) *T { return &a }

func init() {
CmdDump.Run = RunDump
InitDumpFlagset(&CmdDump.Flag)
}

func InitDumpFlagset(fs *flag.FlagSet) {
fs.Var(ptr(config.TimeValue(opts.Oldest)), "from", "timestamp of the oldest message to fetch")
fs.Var(ptr(config.TimeValue(opts.Latest)), "to", "timestamp of the newest message to fetch")
fs.StringVar(&opts.NameTemplate, "ft", nametmpl.Default, "output file naming template.\n")
}

// RunDump is the main entry point for the dump command.
func RunDump(ctx context.Context, cmd *base.Command, args []string) error {
if len(args) == 0 {
base.SetExitStatus(base.SInvalidParameters)
return ErrNothingToDo
}
// Retrieve the Authentication provider.
prov, err := auth.FromContext(ctx)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}

// initialize the list of entities to dump.
list, err := structures.NewEntityList(args)
if err != nil {
base.SetExitStatus(base.SInvalidParameters)
return err
} else if list.IsEmpty() {
base.SetExitStatus(base.SInvalidParameters)
return ErrNothingToDo
}

lg := dlog.FromContext(ctx)

// initialize the file naming template.
if opts.NameTemplate == "" {
lg.Print("File name template is empty, using the default.")
opts.NameTemplate = nametmpl.Default
}
nameTemplate, err := nametmpl.Compile(opts.NameTemplate)
if err != nil {
base.SetExitStatus(base.SUserError)
return fmt.Errorf("file template error: %w", err)
}

// Initialize the filesystem.
if fs, err := fsadapter.New(cfg.BaseLoc); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
} else {
cfg.SlackOptions.Filesystem = fs
defer fs.Close()
}

// Initialize the session.
sess, err := slackdump.NewWithOptions(ctx, prov, cfg.SlackOptions)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}

// Dump conversations.
for _, link := range list.Include {
if err := dump(ctx, sess, nameTemplate, opts, link); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
}
return nil
}

// dump dumps the conversation and saves it to the filesystem. Filesystem is
// specified in the global config.
func dump(ctx context.Context, sess *slackdump.Session, t *template.Template, opts options, link string) error {
ctx, task := trace.NewTask(ctx, "dump")
defer task.End()

conv, err := sess.Dump(ctx, link, opts.Oldest, opts.Latest)
if err != nil {
return err
}
var buf strings.Builder
if err := t.Execute(&buf, conv); err != nil {
return err
}
if err := saveConversation(cfg.SlackOptions.Filesystem, buf.String()+".json", conv); err != nil {
return err
}
return nil
}

// saveConversation saves the conversation to the filesystem.
func saveConversation(fs fsadapter.FS, filename string, conv *types.Conversation) error {
f, err := fs.Create(filename)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(conv); err != nil {
return err
}
return nil
CmdDump.Run = list.RunDump
CmdDump.Wizard = list.WizDump
CmdDump.Long = list.HelpDump(CmdDump)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Command Dump
# Command List Conversation

## Description

Dump is the mode that allows to dump the following type of conversations:
List Conversation is the command that allows to dump the following type of
conversations:

- public and private channels with threads
- group messages (MPIM)
- private messages (DMs)
- individual threads

It downloads file attachments as well, if the `-files` flag is set.
If the `-files` flag is set, it downloads file attachments as well,

This is the original low-level mode that applies almost no transformations to
the API output mode of the Slackdump, and its behaviour would be familiar to
Expand All @@ -25,9 +26,9 @@ or URLs or combine file with conversations and individual conversation links.

## Converting JSON Dumps to Other Formats

To convert the JSON file generated by `slackdump dump` to other formats (for
example, text), use `slackdump convert`. Run `slackdump help convert` to
learn mode.
To convert the JSON file generated by `slackdump {{ .LongName }}` to other
formats (for example, text), use `slackdump convert`. Run `slackdump help
convert` to learn mode.

For reference, in the old version this function was controlled by `-r` flag.

Expand All @@ -38,13 +39,13 @@ For reference, in the old version this function was controlled by `-r` flag.
This command will also enable file download.

```shell
slackdump dump -files C051D4052 DHYNUJ00Y
slackdump {{ .LongName }} -files C051D4052 DHYNUJ00Y
```

### Dump channels listed in my_channels.txt

```shell
slackdump dump @my_channels.txt
slackdump {{ .LongName }} @my_channels.txt
```

### Dump a single thread
Expand All @@ -55,14 +56,14 @@ Threads can be specified as a **URL**, or use a Slackdump-specific
URL:

```shell
slackdump dump \
slackdump {{ .LongName }} \
https://ora600.slack.com/archives/C051D4052/p1665917454731419
```

Slackdump colon notation:

```shell
slackdump dump C051D4052:1665917454.731419
slackdump {{ .LongName }} C051D4052:1665917454.731419
```

### Combined all of the above
Expand All @@ -71,7 +72,7 @@ This example shows how you can combine different types of input. URL of the
thread is omitted for brevity:

```shell
slackdump dump -files C051D4052 \
slackdump {{ .LongName }} -files C051D4052 \
DHYNUJ00Y \
@my_channels.txt \
C051D4052:1665917454.731419
Expand Down
30 changes: 0 additions & 30 deletions cmd/slackdump/internal/list/conversation.go
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
package list

import (
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/dump"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
)

const codeBlock = "```"

// CmdListConversation is the command to list conversations.
var CmdListConversation = &base.Command{
UsageLine: "slackdump list convo [flags] <conversation list>",
Short: "synonym for 'slackdump dump'",
PrintFlags: true,
RequireAuth: true,
FlagMask: dump.CmdDump.FlagMask,
Long: base.Render(`
# List Conversation Command
This command does effectvely the same as the following:
` + codeBlock + `
slackdump dump
` + codeBlock + `
To get extended usage help, run ` + "`slackdump help dump`",
)}

func init() {
CmdListConversation.Run = dump.RunDump
dump.InitDumpFlagset(&CmdListConversation.Flag)
}
Loading

0 comments on commit c42846c

Please sign in to comment.