Skip to content

Commit

Permalink
unify output logic record/search
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 30, 2024
1 parent a49504b commit 0c89921
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/slackdump/internal/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package record
import (
"context"
_ "embed"
"fmt"
"errors"
"strings"
"time"

Expand All @@ -23,24 +23,38 @@ var mdRecord string

var CmdRecord = &base.Command{
Run: RunRecord,
UsageLine: "slackdump record [link1[ link 2[ link N]]]",
UsageLine: "slackdump record [flags] [link1[ link 2[ link N]]]",
Short: "record the dump of the workspace or individual conversations",
Long: mdRecord,
FlagMask: cfg.OmitUserCacheFlag | cfg.OmitCacheDir,
RequireAuth: true,
PrintFlags: true,
}

const zipExt = ".ZIP"

func stripZipExt(s string) string {
if strings.HasSuffix(strings.ToUpper(s), zipExt) {
return s[:len(s)-len(zipExt)]
}
return s
}

var (
errNoOutput = errors.New("output directory is required")
)

func RunRecord(ctx context.Context, cmd *base.Command, args []string) error {
list, err := structures.NewEntityList(args)
if err != nil {
base.SetExitStatus(base.SUserError)
return err
}

if strings.HasSuffix(strings.ToUpper(cfg.Output), ".ZIP") {
cfg.Output = stripZipExt(cfg.Output)
if cfg.Output == "" {
base.SetExitStatus(base.SInvalidParameters)
return fmt.Errorf("record does not support writing to a zip file, please specify a directory")
return errNoOutput
}

cd, err := chunk.CreateDir(cfg.Output)
Expand Down
7 changes: 7 additions & 0 deletions cmd/slackdump/internal/record/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var CmdSearch = &base.Command{
Short: "records search results matching the given query",
Long: `Searches for messages matching criteria.`,
RequireAuth: true,
FlagMask: cfg.OmitUserCacheFlag | cfg.OmitCacheDir,
Run: runSearch,
PrintFlags: true,
}
Expand All @@ -27,6 +28,12 @@ func runSearch(ctx context.Context, cmd *base.Command, args []string) error {
}
query := strings.Join(args, " ")

cfg.Output = stripZipExt(cfg.Output)
if cfg.Output == "" {
base.SetExitStatus(base.SInvalidParameters)
return errNoOutput
}

sess, err := cfg.SlackdumpSession(ctx)
if err != nil {
base.SetExitStatus(base.SInitializationError)
Expand Down

0 comments on commit 0c89921

Please sign in to comment.