Skip to content

Commit

Permalink
Merge pull request #180 from rusq/cli-remake-dump
Browse files Browse the repository at this point in the history
cli-remake/dump
  • Loading branch information
rusq authored Jan 24, 2023
2 parents 0a43e2f + 7be89ab commit 265ef06
Show file tree
Hide file tree
Showing 22 changed files with 205 additions and 205 deletions.
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
var CmdConfig = &base.Command{
UsageLine: "slackdump config",
Short: "API configuration",
Long: base.Render(`
Long: `
# Config Command
Config command allows to perform different operations on the API limits
configuration file.
`),
`,
Commands: []*base.Command{
CmdConfigNew,
CmdConfigCheck,
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/apiconfig/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var CmdConfigCheck = &base.Command{
UsageLine: "slackdump config check",
Short: "validate the existing config for errors",
Long: base.Render(`
Long: `
# Config Check Command
Allows to check the config for errors and invalid values.
Expand All @@ -23,7 +23,7 @@ Example:
It will check for duplicate and unknown keys, and also ensure that values are
within the allowed boundaries.
`),
`,
}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var CmdConfigNew = &base.Command{
UsageLine: "slackdump config new",
Short: "creates a new API config with the default values",
Long: base.Render(`
Long: `
# Config New Command
Creates a new API configuration file containing default values. You will need
Expand All @@ -26,7 +26,7 @@ to specify the filename, for example:
slackdump config new myconfig.yaml
If the extension is omitted, ".yaml" is automatically appended to the filename.
`),
`,
FlagMask: cfg.OmitAll,
PrintFlags: true,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var CmdConvert = &base.Command{
Run: runConvert,
UsageLine: "slackdump convert [flags] <format> <file.json>",
Short: "converts the json files to other formats",
Long: base.Render(`
Long: `
# Convert Command
`),
`, // TODO: add more info
CustomFlags: false,
FlagMask: cfg.OmitAll & ^cfg.OmitWorkspaceFlag,
PrintFlags: true,
Expand Down
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: `
# 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)
}
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/emoji/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var CmdEmoji = &base.Command{
Wizard: wizard,
UsageLine: "slackdump emoji [flags]",
Short: "download workspace emojis",
Long: "",
Long: "", // TODO: add long description
FlagMask: cfg.OmitDownloadFlag | cfg.OmitConfigFlag,
RequireAuth: true,
PrintFlags: true,
Expand Down
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var CmdExport = &base.Command{
Wizard: nil,
UsageLine: "slackdump export",
Short: "exports the Slack Workspace or individual conversations",
Long: ``,
Long: ``, // TODO: add long description
CustomFlags: false,
PrintFlags: true,
RequireAuth: true,
Expand Down
4 changes: 4 additions & 0 deletions cmd/slackdump/internal/golang/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func (c *Command) Usage() {
Exit()
}

func (c *Command) Markdown() string {
return Render(c.Long)
}

// Executable returns the name of the executable for the current OS.
func Executable() string {
exe, err := os.Executable()
Expand Down
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/golang/help/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ package help
var (
helpTemplate = `{{if .Runnable}}usage: {{.UsageLine}}
{{end}}{{.Long | trim}}
{{end}}{{.Markdown | trim}}
`
usageTemplate = `{{.Long | trim}}
usageTemplate = `{{.Markdown | trim}}
Usage:
Expand Down
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
7 changes: 3 additions & 4 deletions cmd/slackdump/internal/list/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var CmdListChannels = &base.Command{
PrintFlags: true,
FlagMask: cfg.OmitDownloadFlag,
Short: "list workspace channels",
Long: base.Render(`
Long: `
# List Channels Command
Lists all visible channels for the currently logged in user. The list
Expand All @@ -23,9 +23,8 @@ including archived ones.
Please note that it may take a while to retrieve all channels, if your
workspace has lots of them.
` +
sectListFormat,
),
` + sectListFormat,

RequireAuth: true,
}

Expand Down
Loading

0 comments on commit 265ef06

Please sign in to comment.