-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate.go
More file actions
174 lines (147 loc) · 5.04 KB
/
Copy pathcreate.go
File metadata and controls
174 lines (147 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/kernel/cli/pkg/create"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
// CreateCmd is a cobra-independent command handler for create operations
type CreateCmd struct{}
// Create executes the creating a new Kernel app logic
func (c CreateCmd) Create(ctx context.Context, ci create.CreateInput) error {
appPath, err := filepath.Abs(ci.Name)
if err != nil {
return fmt.Errorf("failed to resolve app path: %w", err)
}
// Check if directory already exists and prompt for overwrite
if _, err := os.Stat(appPath); err == nil {
overwrite, err := create.PromptForOverwrite(ci.Name)
if err != nil {
return fmt.Errorf("failed to prompt for overwrite: %w", err)
}
if !overwrite {
pterm.Warning.Println("Operation cancelled.")
return nil
}
// Remove existing directory
if err := os.RemoveAll(appPath); err != nil {
return fmt.Errorf("failed to remove existing directory: %w", err)
}
}
if err := os.MkdirAll(appPath, 0755); err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}
pterm.Printfln("\nCreating a new %s %s", ci.Language, ci.Template)
spinner, _ := pterm.DefaultSpinner.Start("Copying template files...")
if err := create.CopyTemplateFiles(appPath, ci.Language, ci.Template); err != nil {
spinner.Fail("Failed to copy template files")
return fmt.Errorf("failed to copy template files: %w", err)
}
spinner.Success()
nextSteps, err := create.InstallDependencies(appPath, ci)
if err != nil {
return fmt.Errorf("failed to install dependencies: %w", err)
}
pterm.Success.Println("🎉 Kernel app created successfully!")
pterm.Println()
pterm.FgYellow.Println(nextSteps)
return nil
}
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new application",
Long: buildCreateLongHelp(),
Example: strings.Join([]string{
"create --name my-app --language typescript --template anthropic-computer-use",
"create -n my-app -l py -t sample-app",
}, "\n"),
RunE: runCreateApp,
}
func init() {
createCmd.Flags().StringP("name", "n", "", "Name of the application")
createCmd.Flags().StringP("language", "l", "", fmt.Sprintf("Language of the application (%s)", strings.Join(supportedLanguageDisplay(), ", ")))
createCmd.Flags().StringP("template", "t", "", "Template to use for the application (see 'kernel create --help' for the full list)")
}
// supportedLanguageDisplay returns each supported language with its shorthand,
// e.g. ["typescript|ts", "python|py"], for inline flag-usage hints.
func supportedLanguageDisplay() []string {
out := make([]string, 0, len(create.SupportedLanguages))
for _, l := range create.SupportedLanguages {
switch l {
case create.LanguageTypeScript:
out = append(out, l+"|"+create.LanguageShorthandTypeScript)
case create.LanguagePython:
out = append(out, l+"|"+create.LanguageShorthandPython)
default:
out = append(out, l)
}
}
return out
}
// buildCreateLongHelp renders the Long help text for `kernel create`,
// listing supported languages and every template (with descriptions and
// which languages it supports) so agents and scripts can pick non-interactively.
func buildCreateLongHelp() string {
var b strings.Builder
b.WriteString("Commands for creating new Kernel applications.\n\n")
b.WriteString("Pass --name, --language and --template to scaffold non-interactively;\n")
b.WriteString("any omitted flag falls back to an interactive prompt.\n\n")
b.WriteString("Languages:\n")
for _, l := range create.SupportedLanguages {
switch l {
case create.LanguageTypeScript:
fmt.Fprintf(&b, " %s (shorthand: %s)\n", l, create.LanguageShorthandTypeScript)
case create.LanguagePython:
fmt.Fprintf(&b, " %s (shorthand: %s)\n", l, create.LanguageShorthandPython)
default:
fmt.Fprintf(&b, " %s\n", l)
}
}
keys := make([]string, 0, len(create.Templates))
for k := range create.Templates {
keys = append(keys, k)
}
sort.Strings(keys)
keyWidth := 0
for _, k := range keys {
if len(k) > keyWidth {
keyWidth = len(k)
}
}
b.WriteString("\nTemplates:\n")
for _, k := range keys {
info := create.Templates[k]
langs := append([]string(nil), info.Languages...)
sort.Strings(langs)
fmt.Fprintf(&b, " %-*s %s [%s]\n", keyWidth, k, info.Description, strings.Join(langs, ", "))
}
return strings.TrimRight(b.String(), "\n")
}
func runCreateApp(cmd *cobra.Command, args []string) error {
appName, _ := cmd.Flags().GetString("name")
language, _ := cmd.Flags().GetString("language")
template, _ := cmd.Flags().GetString("template")
appName, err := create.PromptForAppName(appName)
if err != nil {
return fmt.Errorf("failed to get app name: %w", err)
}
language, err = create.PromptForLanguage(language)
if err != nil {
return fmt.Errorf("failed to get language: %w", err)
}
template, err = create.PromptForTemplate(template, language)
if err != nil {
return fmt.Errorf("failed to get template: %w", err)
}
c := CreateCmd{}
return c.Create(cmd.Context(), create.CreateInput{
Name: appName,
Language: language,
Template: template,
})
}