Skip to content

Commit

Permalink
workspace command code brush up
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Aug 14, 2024
1 parent 6255024 commit 6197f41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 0 additions & 2 deletions cmd/slackdump/internal/workspace/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func runWspDel(ctx context.Context, cmd *base.Command, args []string) error {
}
}

var yesno = base.YesNo

func delAllWsp(m manager) error {
workspaces, err := m.List()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/slackdump/internal/workspace/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
Expand Down Expand Up @@ -52,7 +53,7 @@ func runWspNew(ctx context.Context, cmd *base.Command, args []string) error {

// canOverwrite defined as a variable for testing purposes.
var canOverwrite = func(wsp string) bool {
return base.YesNoWR(os.Stdout, os.Stdin, fmt.Sprintf("Workspace %q already exists. Overwrite", realname(wsp)))
return yesno(fmt.Sprintf("Workspace %q already exists. Overwrite", realname(wsp)))
}

// createWsp creates a new workspace interactively.
Expand Down Expand Up @@ -101,6 +102,7 @@ func createWsp(ctx context.Context, m manager, wsp string, confirmed bool) error
// string with "default". Empty workspace name is possible if the user
// hasn't specified the workspace name on the command line.
func realname(name string) string {
name = strings.TrimSpace(name)
if name == "" {
return "default"
}
Expand Down
42 changes: 41 additions & 1 deletion cmd/slackdump/internal/workspace/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"testing"

auth "github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v3/logger"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -139,3 +139,43 @@ func Test_createWsp(t *testing.T) {
})
}
}

func Test_realname(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "empty",
args: args{
name: "",
},
want: "default",
},
{
name: "spaces",
args: args{
name: " ",
},
want: "default",
},
{
name: "test",
args: args{
name: "test",
},
want: "test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := realname(tt.args.name); got != tt.want {
t.Errorf("realname() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions cmd/slackdump/internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func Current(cacheDir string, override string) (wsp string, err error) {
return wsp, nil
}

var yesno = base.YesNo

// authWsp authenticates in the workspace wsp, and saves, or reuses the
// credentials in the cacheDir. It returns ErrNotExists if the workspace
// doesn't exist in the cacheDir.
Expand Down

0 comments on commit 6197f41

Please sign in to comment.