-
Notifications
You must be signed in to change notification settings - Fork 5
Create kernel app port #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d3d84a8
feat: initial setup for "create" cmd in cli
archandatta cb1dde5
feat: add ts sample-app template
archandatta 966ea89
feat: add cli prompts
archandatta f93d77c
feat: add file copying function into new directory
archandatta ee4ac92
feat: add types
archandatta 078e7e9
feat: add types_test.go
archandatta fd452dd
fix: remove old files
archandatta 628b530
feat: add testing for create_test.go
archandatta 39f13c7
hide `create` command
archandatta 89aa20b
self review
archandatta d1376a1
review: refactor prompting to use pterm
archandatta 4db2ecd
review: add app name validation with flag
archandatta f54d0d4
review: update copy text
archandatta 261ead0
refactor: fix test and refactor structure
archandatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/onkernel/cli/pkg/create" | ||
| "github.com/pterm/pterm" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var createCmd = &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a new application", | ||
| Long: "Commands for creating new Kernel applications", | ||
| RunE: runCreateApp, | ||
| } | ||
|
|
||
| func init() { | ||
| createCmd.Flags().StringP("name", "n", "", "Name of the application") | ||
| createCmd.Flags().StringP("language", "l", "", "Language of the application") | ||
| createCmd.Flags().StringP("template", "t", "", "Template to use for the application") | ||
| } | ||
|
|
||
| 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) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get template: %w", err) | ||
| } | ||
|
|
||
| // Get absolute path for the app directory | ||
| appPath, err := filepath.Abs(appName) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to resolve app path: %w", err) | ||
| } | ||
|
|
||
| // TODO: handle overwrite gracefully (prompt user) | ||
| // Check if directory already exists | ||
| if _, err := os.Stat(appPath); err == nil { | ||
| return fmt.Errorf("directory %s already exists", appName) | ||
| } | ||
|
|
||
| // Create the app directory | ||
| if err := os.MkdirAll(appPath, 0755); err != nil { | ||
| return fmt.Errorf("failed to create directory: %w", err) | ||
| } | ||
|
|
||
| pterm.Println(fmt.Sprintf("\nCreating a new %s %s\n", language, template)) | ||
|
|
||
| spinner, _ := pterm.DefaultSpinner.Start("Copying template files...") | ||
|
|
||
| if err := create.CopyTemplateFiles(appPath, language, template); err != nil { | ||
| spinner.Fail("Failed to copy template files") | ||
| return fmt.Errorf("failed to copy template files: %w", err) | ||
| } | ||
| spinner.Success("✔ TypeScript environment set up successfully") | ||
|
|
||
| nextSteps := fmt.Sprintf(`Next steps: | ||
| brew install onkernel/tap/kernel | ||
| cd %s | ||
| kernel login # or: export KERNEL_API_KEY=<YOUR_API_KEY> | ||
| kernel deploy index.ts | ||
| kernel invoke ts-basic get-page-title --payload '{"url": "https://www.google.com"}' | ||
| # Do this in a separate tab | ||
| kernel login # or: export KERNEL_API_KEY=<YOUR_API_KEY> | ||
| kernel logs ts-basic --follow | ||
archandatta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| `, appName) | ||
|
|
||
| pterm.Success.Println("🎉 Kernel app created successfully!") | ||
| pterm.Println() | ||
| pterm.FgYellow.Println(nextSteps) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCreateCommand(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| wantErr bool | ||
| errContains string | ||
| validate func(t *testing.T, appPath string) | ||
| }{ | ||
| { | ||
| name: "create typescript sample-app", | ||
| args: []string{"--name", "test-app", "--language", "typescript", "--template", "sample-app"}, | ||
| validate: func(t *testing.T, appPath string) { | ||
| // Verify files were created | ||
| assert.FileExists(t, filepath.Join(appPath, "index.ts")) | ||
| assert.FileExists(t, filepath.Join(appPath, "package.json")) | ||
| assert.FileExists(t, filepath.Join(appPath, ".gitignore")) | ||
| assert.NoFileExists(t, filepath.Join(appPath, "_gitignore")) | ||
| }, | ||
| }, | ||
| { | ||
| name: "fail with python sample-app (template not found)", | ||
| args: []string{"--name", "test-app", "--language", "python", "--template", "sample-app"}, | ||
| wantErr: true, | ||
| errContains: "template not found: python/sample-app", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
|
|
||
| orgDir, err := os.Getwd() | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.Chdir(tmpDir) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Cleanup(func() { | ||
| os.Chdir(orgDir) | ||
| }) | ||
|
|
||
| createCmd.SetArgs(tt.args) | ||
| err = createCmd.Execute() | ||
|
|
||
| // Check if error is expected | ||
| if tt.wantErr { | ||
| require.Error(t, err, "expected command to fail but it succeeded") | ||
| if tt.errContains != "" { | ||
| assert.Contains(t, err.Error(), tt.errContains, "error message should contain expected text") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| require.NoError(t, err, "failed to execute create command") | ||
|
|
||
| // Validate the created app | ||
| appPath := filepath.Join(tmpDir, "test-app") | ||
| assert.DirExists(t, appPath, "app directory should be created") | ||
|
|
||
| if tt.validate != nil { | ||
| tt.validate(t, appPath) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package create | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/onkernel/cli/pkg/templates" | ||
| ) | ||
|
|
||
| const ( | ||
| DIR_PERM = 0755 // rwxr-xr-x | ||
| FILE_PERM = 0644 // rw-r--r-- | ||
| ) | ||
|
|
||
| // CopyTemplateFiles copies all files and directories from the specified embedded template | ||
| // into the target application path. It uses the given language and template names | ||
| // to locate the template inside the embedded filesystem. | ||
| // | ||
| // - appPath: filesystem path where the files should be written (the project directory) | ||
| // - language: language subdirectory (e.g., "typescript") | ||
| // - template: template subdirectory (e.g., "sample-app") | ||
| // | ||
| // The function will recursively walk through the embedded template directory and | ||
| // replicate all files and folders in appPath. If a file named "_gitignore" is encountered, | ||
| // it is renamed to ".gitignore" in the output, to work around file embedding limitations. | ||
| // | ||
| // Returns an error if the template path is invalid, empty, or if any file operations fail. | ||
| func CopyTemplateFiles(appPath, language, template string) error { | ||
| // Build the template path within the embedded FS (e.g., "typescript/sample-app") | ||
| templatePath := filepath.Join(language, template) | ||
|
|
||
| // Check if the template exists and is non-empty | ||
| entries, err := fs.ReadDir(templates.FS, templatePath) | ||
| if err != nil { | ||
| return fmt.Errorf("template not found: %s/%s", language, template) | ||
| } | ||
| if len(entries) == 0 { | ||
| return fmt.Errorf("template directory is empty: %s/%s", language, template) | ||
| } | ||
|
|
||
| // Walk through the embedded template directory and copy contents | ||
| return fs.WalkDir(templates.FS, templatePath, func(path string, d fs.DirEntry, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Determine the path relative to the root of the template | ||
| relPath, err := filepath.Rel(templatePath, path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Skip the template root directory itself | ||
| if relPath == "." { | ||
| return nil | ||
| } | ||
|
|
||
| destPath := filepath.Join(appPath, relPath) | ||
|
|
||
| if d.IsDir() { | ||
| return os.MkdirAll(destPath, DIR_PERM) | ||
| } | ||
|
|
||
| // Read the file content from the embedded filesystem | ||
| content, err := fs.ReadFile(templates.FS, path) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read template file %s: %w", path, err) | ||
| } | ||
|
|
||
| // Rename _gitignore to .gitignore in the destination | ||
| if filepath.Base(destPath) == "_gitignore" { | ||
| destPath = filepath.Join(filepath.Dir(destPath), ".gitignore") | ||
| } | ||
|
|
||
| // Write the file to disk in the target project directory | ||
| if err := os.WriteFile(destPath, content, FILE_PERM); err != nil { | ||
| return fmt.Errorf("failed to write file %s: %w", destPath, err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.