Skip to content
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d3d84a8
feat: initial setup for "create" cmd in cli
archandatta Dec 2, 2025
cb1dde5
feat: add ts sample-app template
archandatta Dec 3, 2025
966ea89
feat: add cli prompts
archandatta Dec 3, 2025
f93d77c
feat: add file copying function into new directory
archandatta Dec 3, 2025
ee4ac92
feat: add types
archandatta Dec 4, 2025
078e7e9
feat: add types_test.go
archandatta Dec 4, 2025
fd452dd
fix: remove old files
archandatta Dec 4, 2025
628b530
feat: add testing for create_test.go
archandatta Dec 5, 2025
39f13c7
hide `create` command
archandatta Dec 5, 2025
89aa20b
self review
archandatta Dec 5, 2025
d1376a1
review: refactor prompting to use pterm
archandatta Dec 8, 2025
4db2ecd
review: add app name validation with flag
archandatta Dec 8, 2025
f54d0d4
review: update copy text
archandatta Dec 8, 2025
261ead0
refactor: fix test and refactor structure
archandatta Dec 8, 2025
3a20a69
feat: add python and ts templates
archandatta Dec 5, 2025
b5091a1
feat: add fetch templates logic
archandatta Dec 5, 2025
bd95ae9
feat: add template validation
archandatta Dec 5, 2025
9a78824
enable py templates
archandatta Dec 5, 2025
5570832
remove test
archandatta Dec 5, 2025
35220b4
fix: update ts template readme
archandatta Dec 8, 2025
ed1d016
fix: update ts package.json to new pkg versions
archandatta Dec 8, 2025
7dc6f47
fix: update py package.json to new pkg versions
archandatta Dec 8, 2025
b8dced4
fix: adding sorting for UX consistency && tests
archandatta Dec 8, 2025
412069a
feat: add dependecy pkg for installations
archandatta Dec 8, 2025
f9d23ca
feat: add dependency installation tests
archandatta Dec 9, 2025
31a7eb2
review: add installation step && update error message
archandatta Dec 9, 2025
ea70fee
review: update installation step
archandatta Dec 9, 2025
4337c65
refactor: update dependencies
archandatta Dec 10, 2025
2b33946
Merge branch 'main' into archand/install-dependencies
archandatta Dec 10, 2025
2cc2438
Merge branch 'main' into archand/install-dependencies
archandatta Dec 10, 2025
66d4e7b
review
archandatta Dec 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package cmd

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/onkernel/cli/pkg/create"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type CreateInput struct {
Name string
Language string
Template string
}

// 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 CreateInput) error {
appPath, err := filepath.Abs(ci.Name)
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", ci.Name)
}

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", 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)
}

if err := create.InstallDependencies(appPath, ci.Language); err != nil {
spinner.Fail("Failed to install dependencies")
return fmt.Errorf("failed to install dependencies: %w", err)
}

spinner.Success(fmt.Sprintf("✔ %s environment set up successfully", ci.Language))

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"}'
`, ci.Name)

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: "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, language)
if err != nil {
return fmt.Errorf("failed to get template: %w", err)
}

c := CreateCmd{}
return c.Create(cmd.Context(), CreateInput{
Name: appName,
Language: language,
Template: template,
})
}
247 changes: 247 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package cmd

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/onkernel/cli/pkg/create"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCreateCommand(t *testing.T) {
tests := []struct {
name string
input CreateInput
wantErr bool
errContains string
validate func(t *testing.T, appPath string)
}{
{
name: "create typescript sample-app",
input: CreateInput{
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 invalid template",
input: CreateInput{
Name: "test-app",
Language: "typescript",
Template: "nonexistent",
},
wantErr: true,
errContains: "template not found: typescript/nonexistent",
},
}

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)
})

c := CreateCmd{}
err = c.Create(context.Background(), tt.input)

// 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, tt.input.Name)
assert.DirExists(t, appPath, "app directory should be created")

if tt.validate != nil {
tt.validate(t, appPath)
}
})
}
}

// TestAllTemplatesWithDependencies tests all available templates and verifies dependencies are installed
func TestAllTemplatesWithDependencies(t *testing.T) {
if testing.Short() {
t.Skip("Skipping dependency installation tests in short mode")
}

tests := getTemplateInfo()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"

orgDir, err := os.Getwd()
require.NoError(t, err)

err = os.Chdir(tmpDir)
require.NoError(t, err)

t.Cleanup(func() {
os.Chdir(orgDir)
})

// Create the app
c := CreateCmd{}
err = c.Create(context.Background(), CreateInput{
Name: appName,
Language: tt.language,
Template: tt.template,
})
require.NoError(t, err, "failed to create app")

appPath := filepath.Join(tmpDir, appName)

// Verify app directory exists
assert.DirExists(t, appPath, "app directory should exist")

// Language-specific validations
switch tt.language {
case create.LanguageTypeScript:
validateTypeScriptTemplate(t, appPath, true)
case create.LanguagePython:
validatePythonTemplate(t, appPath, true)
}
})
}
}

// TestAllTemplatesCreation tests that all templates can be created without installing dependencies
func TestAllTemplatesCreation(t *testing.T) {
tests := getTemplateInfo()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
appPath := filepath.Join(tmpDir, appName)

// Create app directory
err := os.MkdirAll(appPath, 0755)
require.NoError(t, err, "failed to create app directory")

// Copy template files without installing dependencies
err = create.CopyTemplateFiles(appPath, tt.language, tt.template)
require.NoError(t, err, "failed to copy template files")

// Verify app directory exists
assert.DirExists(t, appPath, "app directory should exist")

// Language-specific validations (without dependency checks)
switch tt.language {
case create.LanguageTypeScript:
validateTypeScriptTemplate(t, appPath, false)
case create.LanguagePython:
validatePythonTemplate(t, appPath, false)
}
})
}
}

// validateTypeScriptTemplate verifies TypeScript template structure and optionally dependencies
func validateTypeScriptTemplate(t *testing.T, appPath string, checkDependencies bool) {
t.Helper()

// Verify essential files exist
assert.FileExists(t, filepath.Join(appPath, "package.json"), "package.json should exist")
assert.FileExists(t, filepath.Join(appPath, "tsconfig.json"), "tsconfig.json should exist")
assert.FileExists(t, filepath.Join(appPath, "index.ts"), "index.ts should exist")
assert.FileExists(t, filepath.Join(appPath, ".gitignore"), ".gitignore should exist")

// Verify _gitignore was renamed
assert.NoFileExists(t, filepath.Join(appPath, "_gitignore"), "_gitignore should not exist")

if checkDependencies {
// Verify node_modules exists (dependencies were installed)
nodeModulesPath := filepath.Join(appPath, "node_modules")
if _, err := os.Stat(nodeModulesPath); err == nil {
// Only check contents if node_modules exists
entries, err := os.ReadDir(nodeModulesPath)
require.NoError(t, err, "should be able to read node_modules directory")
assert.NotEmpty(t, entries, "node_modules should contain installed packages")
} else {
t.Logf("Warning: node_modules not found at %s (npm install may have failed)", nodeModulesPath)
}
}
}

// validatePythonTemplate verifies Python template structure and optionally dependencies
func validatePythonTemplate(t *testing.T, appPath string, checkDependencies bool) {
t.Helper()

// Verify essential files exist
assert.FileExists(t, filepath.Join(appPath, "pyproject.toml"), "pyproject.toml should exist")
assert.FileExists(t, filepath.Join(appPath, "main.py"), "main.py should exist")
assert.FileExists(t, filepath.Join(appPath, ".gitignore"), ".gitignore should exist")

// Verify _gitignore was renamed
assert.NoFileExists(t, filepath.Join(appPath, "_gitignore"), "_gitignore should not exist")

if checkDependencies {
// Verify .venv exists (virtual environment was created)
venvPath := filepath.Join(appPath, ".venv")
if _, err := os.Stat(venvPath); err == nil {
// Only check contents if .venv exists
binPath := filepath.Join(venvPath, "bin")
assert.DirExists(t, binPath, ".venv/bin directory should exist")

pythonPath := filepath.Join(binPath, "python")
assert.FileExists(t, pythonPath, ".venv/bin/python should exist")
} else {
t.Logf("Warning: .venv not found at %s (uv venv may have failed)", venvPath)
}
}
}

func getTemplateInfo() []struct {
name string
language string
template string
} {
tests := make([]struct {
name string
language string
template string
}, 0)

for templateKey, templateInfo := range create.Templates {
for _, lang := range templateInfo.Languages {
tests = append(tests, struct {
name string
language string
template string
}{
name: lang + "/" + templateKey,
language: lang,
template: templateKey,
})
}
}

return tests
}
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func isAuthExempt(cmd *cobra.Command) bool {
}
for c := cmd; c != nil; c = c.Parent() {
switch c.Name() {
case "login", "logout", "auth", "help", "completion":
case "login", "logout", "auth", "help", "completion",
"create":
return true
}
}
Expand Down Expand Up @@ -128,6 +129,7 @@ func init() {
rootCmd.AddCommand(profilesCmd)
rootCmd.AddCommand(proxies.ProxiesCmd)
rootCmd.AddCommand(extensionsCmd)
rootCmd.AddCommand(createCmd)

rootCmd.PersistentPostRunE = func(cmd *cobra.Command, args []string) error {
// running synchronously so we never slow the command
Expand Down
Loading