Skip to content

Commit ee4ac92

Browse files
committed
feat: add types
1 parent f93d77c commit ee4ac92

File tree

3 files changed

+84
-27
lines changed

3 files changed

+84
-27
lines changed

cmd/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ var createCmd = &cobra.Command{
1818
}
1919

2020
func init() {
21-
createCmd.Flags().String("name", "", "Name of the application")
22-
createCmd.Flags().String("language", "", "Language of the application")
23-
createCmd.Flags().String("template", "", "Template to use for the application")
21+
createCmd.Flags().StringP("name", "n", "", "Name of the application")
22+
createCmd.Flags().StringP("language", "l", "", "Language of the application")
23+
createCmd.Flags().StringP("template", "t", "", "Template to use for the application")
2424
}
2525

2626
func runCreateApp(cmd *cobra.Command, args []string) error {

pkg/create/prompts.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@ package create
33
import (
44
"fmt"
55
"regexp"
6+
"slices"
67

78
"github.com/AlecAivazis/survey/v2"
89
)
910

10-
const defaultAppName = "my-kernel-app"
11-
12-
// projectNameValidator ensures the project name is safe for file systems and package managers.
13-
func projectNameValidator(val any) error {
11+
func validateAppName(val any) error {
1412
str, ok := val.(string)
1513
if !ok {
1614
return fmt.Errorf("invalid input type")
1715
}
1816

19-
// Project name must be non-empty
2017
if len(str) == 0 {
2118
return fmt.Errorf("project name cannot be empty")
2219
}
2320

2421
// Validate project name: only letters, numbers, underscores, and hyphens
25-
// This regex prevents special characters that might break shell commands or filesystem paths.
2622
matched, err := regexp.MatchString(`^[A-Za-z\-_\d]+$`, str)
2723
if err != nil {
2824
return err
@@ -33,40 +29,44 @@ func projectNameValidator(val any) error {
3329
return nil
3430
}
3531

36-
// PromptForAppName prompts the user for the application name if not provided
3732
func PromptForAppName(providedAppName string) (string, error) {
3833
if providedAppName != "" {
3934
return providedAppName, nil
4035
}
4136

4237
var appName string
4338
prompt := &survey.Input{
44-
Message: "What is the name of your project?",
45-
Default: defaultAppName,
39+
Message: AppNamePrompt,
40+
Default: DefaultAppName,
4641
}
4742

48-
if err := survey.AskOne(prompt, &appName, survey.WithValidator(projectNameValidator)); err != nil {
43+
if err := survey.AskOne(prompt, &appName, survey.WithValidator(validateAppName)); err != nil {
4944
return "", err
5045
}
5146

5247
return appName, nil
5348
}
5449

55-
func PromptForLanguage(providedLanguage string) (string, error) {
56-
if providedLanguage != "" {
57-
return providedLanguage, nil
58-
}
59-
var language string
50+
func handleLangugePrompt() (string, error) {
51+
var l string
6052
languagePrompt := &survey.Select{
61-
Message: "Choose a programming language:",
62-
// TODO: create constants so that more languages can be added later
63-
Options: []string{"typescript", "python"},
64-
Default: "typescript",
53+
Message: LanguagePrompt,
54+
Options: SupportedLanguages,
6555
}
66-
if err := survey.AskOne(languagePrompt, &language); err != nil {
56+
if err := survey.AskOne(languagePrompt, &l); err != nil {
6757
return "", err
6858
}
69-
return language, nil
59+
return l, nil
60+
}
61+
62+
func PromptForLanguage(providedLanguage string) (string, error) {
63+
l := NormalizeLanguage(providedLanguage)
64+
if l != "" && !slices.Contains(SupportedLanguages, l) {
65+
return handleLangugePrompt()
66+
} else if providedLanguage != "" {
67+
return l, nil
68+
}
69+
return handleLangugePrompt()
7070
}
7171

7272
func PromptForTemplate(providedTemplate string) (string, error) {
@@ -76,9 +76,8 @@ func PromptForTemplate(providedTemplate string) (string, error) {
7676

7777
var template string
7878
templatePrompt := &survey.Select{
79-
Message: "Choose a template:",
80-
Options: []string{"sample-app"},
81-
Default: "sample-app",
79+
Message: TemplatePrompt,
80+
Options: GetSupportedTemplates(),
8281
}
8382
if err := survey.AskOne(templatePrompt, &template); err != nil {
8483
return "", err

pkg/create/types.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package create
2+
3+
const (
4+
DefaultAppName = "my-kernel-app"
5+
AppNamePrompt = "What is the name of your project?"
6+
LanguagePrompt = "Choose a programming language:"
7+
TemplatePrompt = "Select a template:"
8+
)
9+
10+
type Language string
11+
12+
const (
13+
LanguageTypeScript = "typescript"
14+
LanguagePython = "python"
15+
LanguageShorthandTypeScript = "ts"
16+
LanguageShorthandPython = "py"
17+
)
18+
19+
type TemplateInfo struct {
20+
Name string
21+
Description string
22+
Languages []Language
23+
}
24+
25+
var Templates = map[string]TemplateInfo{
26+
"sample-app": {
27+
Name: "Sample App",
28+
Description: "Implements basic Kernel apps",
29+
Languages: []Language{LanguageTypeScript, LanguagePython},
30+
},
31+
}
32+
33+
// SupportedLanguages returns a list of all supported languages
34+
var SupportedLanguages = []string{
35+
LanguageTypeScript,
36+
LanguagePython,
37+
}
38+
39+
// GetSupportedTemplates returns a list of all supported template names
40+
func GetSupportedTemplates() []string {
41+
templates := make([]string, 0, len(Templates))
42+
for tn := range Templates {
43+
templates = append(templates, tn)
44+
}
45+
return templates
46+
}
47+
48+
// Helper to normalize language input (handle shorthand)
49+
func NormalizeLanguage(language string) string {
50+
switch language {
51+
case LanguageShorthandTypeScript:
52+
return LanguageTypeScript
53+
case LanguageShorthandPython:
54+
return LanguagePython
55+
default:
56+
return language
57+
}
58+
}

0 commit comments

Comments
 (0)