1
1
package project
2
2
3
3
import (
4
+ "bufio"
4
5
"cli/api"
5
6
"errors"
6
7
"fmt"
7
8
"os"
9
+ "path/filepath"
10
+ "strings"
8
11
9
12
"github.com/manifoldco/promptui"
10
13
"github.com/spf13/cobra"
@@ -24,10 +27,18 @@ var (
24
27
const inputPromptPrefix string = " > "
25
28
26
29
func prompt (message string ) string {
27
- var selection string = ""
28
- for selection == "" {
29
- fmt .Print (inputPromptPrefix + message )
30
- fmt .Scanln (& selection )
30
+ reader := bufio .NewReader (os .Stdin )
31
+ fmt .Print (inputPromptPrefix + message )
32
+
33
+ selection , err := reader .ReadString ('\n' )
34
+ if err != nil {
35
+ fmt .Println ("An error occurred while reading input. Please try again." , err )
36
+ return prompt (message )
37
+ }
38
+
39
+ selection = strings .TrimSpace (selection )
40
+ if selection == "" {
41
+ return prompt (message )
31
42
}
32
43
return selection
33
44
}
@@ -124,10 +135,13 @@ func selectStarterTemplate() (template string, err error) {
124
135
}
125
136
options := []StarterTemplateOption {}
126
137
for _ , template := range templates {
127
- options = append (options , StarterTemplateOption {Name : template .Name (), Value : template .Name ()})
138
+ // For the printed name, replace _ with spaces
139
+ var name = template .Name ()
140
+ name = strings .Replace (name , "_" , " " , - 1 )
141
+ options = append (options , StarterTemplateOption {Name : name , Value : template .Name ()})
128
142
}
129
143
getStarterTemplate := promptui.Select {
130
- Label : "Select a Starter Example :" ,
144
+ Label : "Select a Starter Project :" ,
131
145
Items : options ,
132
146
Templates : promptTemplates ,
133
147
}
@@ -157,15 +171,13 @@ var NewProjectCmd = &cobra.Command{
157
171
158
172
// Project Name
159
173
if projectName == "" {
160
- fmt .Print ("1. Project Name:\n " )
161
- fmt .Print (" Please enter the name of your project.\n " )
174
+ fmt .Print ("Provide a name for your project:\n " )
162
175
projectName = prompt ("" )
163
176
}
164
177
fmt .Print ("\n Project name set to '" + projectName + "'.\n \n " )
165
178
166
179
// Project Examples
167
- fmt .Print ("2. Starter Example:\n " )
168
- fmt .Print (" Choose a starter example to begin with.\n " )
180
+ fmt .Print ("Select a starter project to begin with:\n " )
169
181
170
182
if modelType == "" {
171
183
starterExample , err := selectStarterTemplate ()
@@ -178,28 +190,21 @@ var NewProjectCmd = &cobra.Command{
178
190
fmt .Println ("" )
179
191
180
192
// Model Name
181
- if modelType != "Hello World" {
182
- fmt .Print (" Model Name:\n " )
183
- fmt .Print (" Please enter the name of the Hugging Face model you would like to use.\n " )
184
- fmt .Print (" Leave blank to use the default model for the selected example.\n > " )
193
+ if modelType != "Hello_World" {
194
+ fmt .Print (" Enter the name of the Hugging Face model you would like to use:\n " )
195
+ fmt .Print (" Leave blank to use the default model for the selected project.\n > " )
185
196
fmt .Scanln (& modelName )
186
197
fmt .Println ("" )
187
198
}
188
199
189
- // Project Configuration
190
- fmt .Print ("3. Configuration:\n " )
191
- fmt .Print (" Let's configure the project environment.\n \n " )
192
-
193
200
// CUDA Version
194
- fmt .Println (" CUDA Version:" )
195
- cudaVersion := promptChoice (" Choose a CUDA version for your project." ,
201
+ cudaVersion := promptChoice ("Select a CUDA version for your project:" ,
196
202
[]string {"11.8.0" , "12.1.0" , "12.2.0" }, "11.8.0" )
197
203
198
- fmt .Println ("\n Using CUDA version: " + cudaVersion )
204
+ fmt .Println ("\n Using CUDA version: " + cudaVersion + " \n " )
199
205
200
206
// Python Version
201
- fmt .Println ("\n Python Version:" )
202
- pythonVersion := promptChoice (" Choose a Python version for your project." ,
207
+ pythonVersion := promptChoice ("Select a Python version for your project:" ,
203
208
[]string {"3.8" , "3.9" , "3.10" , "3.11" }, "3.10" )
204
209
205
210
fmt .Println ("\n Using Python version: " + pythonVersion )
@@ -208,7 +213,7 @@ var NewProjectCmd = &cobra.Command{
208
213
fmt .Println ("\n Project Summary:" )
209
214
fmt .Println ("----------------" )
210
215
fmt .Printf ("- Project Name : %s\n " , projectName )
211
- fmt .Printf ("- Starter Example : %s\n " , modelType )
216
+ fmt .Printf ("- Starter Project : %s\n " , modelType )
212
217
fmt .Printf ("- CUDA version : %s\n " , cudaVersion )
213
218
fmt .Printf ("- Python version : %s\n " , pythonVersion )
214
219
@@ -219,15 +224,18 @@ var NewProjectCmd = &cobra.Command{
219
224
return
220
225
}
221
226
222
- fmt .Printf ("\n The project will be created in the current directory: \n %s\n \n " , currentDir )
223
- confirm := promptChoice ("Proceed with creation?" , []string {"yes" , "no" }, "yes" )
224
- if confirm != "yes" {
225
- fmt .Println ("Project creation cancelled." )
226
- return
227
+ projectDir := filepath .Join (currentDir , projectName )
228
+ if _ , err := os .Stat (projectDir ); ! os .IsNotExist (err ) {
229
+ fmt .Printf ("\n A directory with the name '%s' already exists in the current path.\n " , projectName )
230
+ confirm := promptChoice ("Continue with overwrite?" , []string {"yes" , "no" }, "no" )
231
+ if confirm != "yes" {
232
+ fmt .Println ("Project creation cancelled." )
233
+ return
234
+ }
235
+ } else {
236
+ fmt .Printf ("\n Creating project '%s' in directory '%s'\n " , projectName , projectDir )
227
237
}
228
238
229
- fmt .Println ("\n Creating project..." )
230
-
231
239
// Create Project
232
240
createNewProject (projectName , cudaVersion , pythonVersion , modelType , modelName , initCurrentDir )
233
241
fmt .Printf ("\n Project %s created successfully! \n Navigate to your project directory with `cd %s`\n \n " , projectName , projectName )
0 commit comments