Skip to content

Commit

Permalink
workspace refactor, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Aug 17, 2024
1 parent c02dbca commit f41fb53
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 123 deletions.
8 changes: 4 additions & 4 deletions cmd/slackdump/internal/workspace/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ func runWspDel(ctx context.Context, cmd *base.Command, args []string) error {
return err
}
if *delAll {
return delAllWsp(m)
return delAllWsp(m, *delConfirm)
} else {
return delOneWsp(m, args)
}
}

func delAllWsp(m manager) error {
func delAllWsp(m manager, confirm bool) error {
workspaces, err := m.List()
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}

if !*delConfirm && !yesno("This will delete ALL workspaces") {
base.SetExitStatus(base.SNoError)
if !confirm && !yesno("This will delete ALL workspaces") {
base.SetExitStatus(base.SCancelled)
return ErrOpCancelled
}
for _, name := range workspaces {
Expand Down
144 changes: 27 additions & 117 deletions cmd/slackdump/internal/workspace/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ speed and the number of workspaces.
PrintFlags: true,
}

const timeLayout = "2006-01-02 15:04:05"

var (
bare = CmdWspList.Flag.Bool("b", false, "bare output format (just names)")
all = CmdWspList.Flag.Bool("a", false, "all information, including user")
Expand All @@ -61,13 +59,19 @@ func runList(ctx context.Context, cmd *base.Command, args []string) error {
}

// default formatter is print full.
formatter := printFull
formatter := printDefault
if *bare {
formatter = printBare
} else if *all {
formatter = printAll
}

return list(m, formatter)
}

type formatFunc func(io.Writer, manager, string, []string) error

func list(m manager, formatter formatFunc) error {
entries, err := m.List()
if err != nil {
if errors.Is(err, cache.ErrNoWorkspaces) {
Expand All @@ -94,16 +98,14 @@ func runList(ctx context.Context, cmd *base.Command, args []string) error {
return formatter(os.Stdout, m, current, entries)
}

const defMark = "=>"

var hdrItems = []hdrItem{
{"C", 1},
{"name", 7},
{"filename", 12},
{"modified", 19},
{"team", 9},
{"user", 8},
{"error", 5},
func printDefault(w io.Writer, m manager, current string, wsps []string) error {
ew := &errWriter{w: w}
fmt.Fprintf(ew, "Workspaces in %q:\n\n", cfg.CacheDir())
for _, row := range simpleList(m, current, wsps) {
fmt.Fprintf(ew, "%s (file: %s, last modified: %s)\n", row[0], row[1], row[2])
}
fmt.Fprintf(ew, "\nCurrent workspace is marked with ' %s '.\n", defMark)
return ew.Err()
}

func printAll(w io.Writer, m manager, current string, wsps []string) error {
Expand All @@ -125,6 +127,17 @@ func printAll(w io.Writer, m manager, current string, wsps []string) error {
return ew.Err()
}

func printBare(w io.Writer, _ manager, current string, workspaces []string) error {
ew := &errWriter{w: w}
for _, name := range workspaces {
if current == name {
fmt.Fprint(ew, "*")
}
fmt.Fprintln(ew, name)
}
return ew.Err()
}

func wspInfo(ctx context.Context, m manager, current string, wsps []string) [][]string {
var rows = [][]string{}

Expand Down Expand Up @@ -173,114 +186,11 @@ func wspRow(ctx context.Context, m manager, current, name string) []string {
return []string{curr, name, fi.Name(), fi.ModTime().Format(timeLayout), info.Team, info.User, "OK"}
}

type hdrItem struct {
name string
size int
}

func (h *hdrItem) String() string {
return h.name
}

func (h *hdrItem) Size() int {
if h.size == 0 {
h.size = len(h.String())
}
return h.size
}

func (h *hdrItem) Underline(char ...string) string {
if len(char) == 0 {
char = []string{"-"}
}
return strings.Repeat(char[0], h.Size())
}

// makeHeader creates header, separating columns with tabs and underlining
// them with dashes.
// Example:
//
// C name filename modified team user error
// - ---- -------- -------- ---- ---- -----
func makeHeader(hi ...hdrItem) string {
var sb strings.Builder
for i, h := range hi {
if i > 0 {
sb.WriteByte('\t')
}
sb.WriteString(h.String())
}
sb.WriteByte('\n')
for i, h := range hi {
if i > 0 {
sb.WriteByte('\t')
}
sb.WriteString(h.Underline())
}
return sb.String()
}

// userInfo returns the team and user information for the given workspace.
func userInfo(ctx context.Context, m manager, name string) (*slack.AuthTestResponse, error) {
prov, err := m.LoadProvider(name)
if err != nil {
return nil, err
}
return prov.Test(ctx)
}

type errWriter struct {
w io.Writer
err error
}

func (ew *errWriter) Write(p []byte) (n int, err error) {
if ew.err != nil {
return 0, nil
}
n, ew.err = ew.w.Write(p)
return n, ew.err
}

func (ew *errWriter) Err() error {
return ew.err
}

func printFull(w io.Writer, m manager, current string, wsps []string) error {
ew := &errWriter{w: w}
fmt.Fprintf(ew, "Workspaces in %q:\n\n", cfg.CacheDir())
for _, row := range simpleList(m, current, wsps) {
fmt.Fprintf(ew, "%s (file: %s, last modified: %s)\n", row[0], row[1], row[2])
}
fmt.Fprintf(ew, "\nCurrent workspace is marked with ' %s '.\n", defMark)
return ew.Err()
}

func simpleList(m manager, current string, wsps []string) [][]string {
var rows = make([][]string, 0, len(wsps))
for _, name := range wsps {
timestamp := "unknown"
filename := "-"
if fi, err := m.FileInfo(name); err == nil {
timestamp = fi.ModTime().Format(timeLayout)
filename = fi.Name()
}
if name == current {
name = defMark + " " + name
} else {
name = " " + name
}
rows = append(rows, []string{name, filename, timestamp})
}
return rows
}

func printBare(w io.Writer, _ manager, current string, workspaces []string) error {
ew := &errWriter{w: w}
for _, name := range workspaces {
if current == name {
fmt.Fprint(ew, "*")
}
fmt.Fprintln(ew, name)
}
return ew.Err()
}
101 changes: 101 additions & 0 deletions cmd/slackdump/internal/workspace/list_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package workspace

import (
"io"
"strings"
)

const (
defMark = "=>"
timeLayout = "2006-01-02 15:04:05"
)

var hdrItems = []hdrItem{
{"C", 1},
{"name", 7},
{"filename", 12},
{"modified", 19},
{"team", 9},
{"user", 8},
{"error", 5},
}

type errWriter struct {
w io.Writer
err error
}

func (ew *errWriter) Write(p []byte) (n int, err error) {
if ew.err != nil {
return 0, nil
}
n, ew.err = ew.w.Write(p)
return n, ew.err
}

func (ew *errWriter) Err() error {
return ew.err
}

func simpleList(m manager, current string, wsps []string) [][]string {
var rows = make([][]string, 0, len(wsps))
for _, name := range wsps {
timestamp := "unknown"
filename := "-"
if fi, err := m.FileInfo(name); err == nil {
timestamp = fi.ModTime().Format(timeLayout)
filename = fi.Name()
}
if name == current {
name = defMark + " " + name
} else {
name = " " + name
}
rows = append(rows, []string{name, filename, timestamp})
}
return rows
}

type hdrItem struct {
name string
size int
}

func (h *hdrItem) String() string {
return h.name
}

func (h *hdrItem) Size() int {
return len(h.String())
}

func (h *hdrItem) Underline(char ...string) string {
if len(char) == 0 {
char = []string{"-"}
}
return strings.Repeat(char[0], h.Size())
}

// makeHeader creates header, separating columns with tabs and underlining
// them with dashes.
// Example:
//
// C name filename modified team user error
// - ---- -------- -------- ---- ---- -----
func makeHeader(hi ...hdrItem) string {
var sb strings.Builder
for i, h := range hi {
if i > 0 {
sb.WriteByte('\t')
}
sb.WriteString(h.String())
}
sb.WriteByte('\n')
for i, h := range hi {
if i > 0 {
sb.WriteByte('\t')
}
sb.WriteString(h.Underline())
}
return sb.String()
}
Loading

0 comments on commit f41fb53

Please sign in to comment.