Skip to content

Commit 5ed7e88

Browse files
committed
fix: update imports in cmd/
1 parent af97eca commit 5ed7e88

6 files changed

Lines changed: 39 additions & 22 deletions

File tree

cmd/project/create.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ package project
1717
import (
1818
"context"
1919
"fmt"
20+
"math/rand"
2021
"path/filepath"
2122
"strings"
23+
"time"
2224

2325
"github.com/slackapi/slack-cli/internal/iostreams"
2426
"github.com/slackapi/slack-cli/internal/logger"
@@ -130,7 +132,7 @@ func runCreateCommand(clients *shared.ClientFactory, cmd *cobra.Command, args []
130132

131133
// Prompt for app name if not provided via flag or argument
132134
if appNameArg == "" && clients.IO.IsTTY() {
133-
defaultName := create.GenerateRandomAppName()
135+
defaultName := generateRandomAppName()
134136
cmd.Print(style.Secondary(fmt.Sprintf(" Press Enter to use the generated name: %s", defaultName)), "\n")
135137
name, err := clients.IO.InputPrompt(ctx, "Name your app:", iostreams.InputPromptConfig{})
136138
if err != nil {
@@ -286,6 +288,15 @@ func printCreateSuccess(ctx context.Context, clients *shared.ClientFactory, appP
286288
clients.IO.PrintTrace(ctx, slacktrace.CreateSuccess)
287289
}
288290

291+
// generateRandomAppName will create a random app name based on two words and a number
292+
func generateRandomAppName() string {
293+
rand.New(rand.NewSource(time.Now().UnixNano()))
294+
var firstRandomNum = rand.Intn(len(create.Adjectives))
295+
var secondRandomNum = rand.Intn(len(create.Animals))
296+
var randomName = fmt.Sprintf("%s-%s-%d", create.Adjectives[firstRandomNum], create.Animals[secondRandomNum], rand.Intn(1000))
297+
return randomName
298+
}
299+
289300
// printAppCreateError stops the creation spinners and displays the returned error message
290301
func printAppCreateError(clients *shared.ClientFactory, cmd *cobra.Command, err error) {
291302
ctx := cmd.Context()

cmd/project/create_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestCreateCommand(t *testing.T) {
4646
testutil.TableTestCommand(t, testutil.CommandTests{
4747
"creates a bolt application from prompts": {
4848
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
49+
cm.IO.On("IsTTY").Return(true)
4950
cm.IO.On("SelectPrompt", mock.Anything, "Select an app:", mock.Anything, mock.Anything).
5051
Return(
5152
iostreams.SelectPromptResponse{
@@ -82,6 +83,7 @@ func TestCreateCommand(t *testing.T) {
8283
"creates a deno application from flags": {
8384
CmdArgs: []string{"--template", "slack-samples/deno-starter-template"},
8485
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
86+
cm.IO.On("IsTTY").Return(true)
8587
cm.IO.On("SelectPrompt", mock.Anything, "Select an app:", mock.Anything, mock.Anything).
8688
Return(
8789
iostreams.SelectPromptResponse{
@@ -118,6 +120,7 @@ func TestCreateCommand(t *testing.T) {
118120
"creates an agent app using agent argument shortcut": {
119121
CmdArgs: []string{"agent"},
120122
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
123+
cm.IO.On("IsTTY").Return(true)
121124
// Should skip category prompt and go directly to language selection
122125
cm.IO.On("SelectPrompt", mock.Anything, "Select a language:", mock.Anything, mock.Anything).
123126
Return(
@@ -344,6 +347,7 @@ func TestCreateCommand(t *testing.T) {
344347
},
345348
"user accepts default name from prompt": {
346349
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
350+
cm.IO.On("IsTTY").Return(true)
347351
cm.IO.On("SelectPrompt", mock.Anything, "Select an app:", mock.Anything, mock.Anything).
348352
Return(
349353
iostreams.SelectPromptResponse{

internal/iostreams/survey.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ var InputQuestionTemplate = fmt.Sprintf(`
175175

176176
// InputPromptConfig holds additional config for an Input prompt
177177
type InputPromptConfig struct {
178-
Required bool // Whether the input must be non-empty
179-
Default string // Default value pre-filled in the prompt
178+
Required bool // Whether the input must be non-empty
180179
}
181180

182181
// GetFlags returns all flags for the Input prompt
@@ -201,7 +200,6 @@ func (io *IOStreams) InputPrompt(ctx context.Context, message string, cfg InputP
201200
var input string
202201
err := survey.AskOne(&survey.Input{
203202
Message: message,
204-
Default: cfg.Default,
205203
}, &input, SurveyOptions(cfg)...)
206204

207205
if err != nil {

internal/pkg/create/constants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package create
1616

17-
var adjectives = []string{
17+
var Adjectives = []string{
1818
"admiring",
1919
"adoring",
2020
"affectionate",
@@ -113,7 +113,7 @@ var adjectives = []string{
113113
"zen",
114114
}
115115

116-
var animals = []string{
116+
var Animals = []string{
117117
"aardvark",
118118
"alligator",
119119
"alpaca",

internal/pkg/create/create.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ import (
1818
"context"
1919
"fmt"
2020
"io"
21-
"math/rand"
2221
"net/http"
2322
"os"
2423
"os/exec"
2524
"path/filepath"
2625
"strings"
27-
"time"
2826

2927
"github.com/go-git/go-git/v5"
3028
"github.com/go-git/go-git/v5/plumbing"
@@ -150,23 +148,15 @@ func Create(ctx context.Context, clients *shared.ClientFactory, log *logger.Logg
150148
return appDirPath, nil
151149
}
152150

153-
// GenerateRandomAppName will create a random app name based on two words and a number
154-
func GenerateRandomAppName() string {
155-
rand.New(rand.NewSource(time.Now().UnixNano()))
156-
var firstRandomNum = rand.Intn(len(adjectives))
157-
var secondRandomNum = rand.Intn(len(animals))
158-
var randomName = fmt.Sprintf("%s-%s-%d", adjectives[firstRandomNum], animals[secondRandomNum], rand.Intn(1000))
159-
return randomName
160-
}
161-
162151
// getAppDirName will validate and return the app's directory name
163152
func getAppDirName(appName string) (string, error) {
164153
if len(appName) <= 0 {
165-
return GenerateRandomAppName(), nil
154+
return "", fmt.Errorf("app name is required")
166155
}
167156

168157
// trim whitespace
169-
appName = strings.ReplaceAll(appName, " ", "")
158+
appName = strings.TrimSpace(appName)
159+
appName = strings.ReplaceAll(appName, " ", "-")
170160

171161
// name cannot be a reserved word
172162
if goutils.Contains(reserved, appName, false) {

internal/pkg/create/create_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,30 @@ func TestGetProjectDirectoryName(t *testing.T) {
3939
var appName string
4040
var err error
4141

42-
// Test without app name test removed because more than one possible default name
42+
// Test with empty name returns an error
43+
appName, err = getAppDirName("")
44+
assert.Error(t, err, "should return an error for empty name")
45+
assert.Equal(t, "", appName)
46+
4347
// Test with app name
4448
appName, err = getAppDirName("my-app")
4549
assert.NoError(t, err, "should not return an error")
46-
assert.Equal(t, appName, "my-app", "should return 'my-app'")
50+
assert.Equal(t, "my-app", appName, "should return 'my-app'")
4751

4852
// Test with a dot in the app name
4953
appName, err = getAppDirName(".my-app")
5054
assert.NoError(t, err, "should not return an error")
51-
assert.Equal(t, appName, ".my-app", "should return '.my-app'")
55+
assert.Equal(t, ".my-app", appName, "should return '.my-app'")
56+
57+
// Spaces replaced with hyphens
58+
appName, err = getAppDirName("my cool app")
59+
assert.NoError(t, err)
60+
assert.Equal(t, "my-cool-app", appName)
61+
62+
// Leading/trailing spaces trimmed
63+
appName, err = getAppDirName(" my-app ")
64+
assert.NoError(t, err)
65+
assert.Equal(t, "my-app", appName)
5266
}
5367

5468
func TestGetAvailableDirectory(t *testing.T) {

0 commit comments

Comments
 (0)