Skip to content

Commit 3bd9365

Browse files
committed
feat: add auto-init prompt for uninitialized projects
1 parent 3d0c86c commit 3bd9365

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

cmd/info.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Use --global to check global skills.`,
2121
Run: func(cmd *cobra.Command, args []string) {
2222
global, _ := cmd.Flags().GetBool("global")
2323
skillName := args[0]
24+
25+
// Ensure project is initialized for non-global operations
26+
if !global {
27+
if !ensureInitialized() {
28+
return
29+
}
30+
}
31+
2432
skillsDir := config.GetSkillsDirByScope(global)
2533
skillPath := filepath.Join(skillsDir, skillName)
2634

cmd/install.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ If no agent is specified, skills are installed to .agent/skills/ by default.`,
7373
}
7474
}
7575

76+
// Ensure project is initialized for non-global, non-agent-specific operations
77+
if !global && len(agents) == 0 {
78+
if !ensureInitialized() {
79+
return
80+
}
81+
}
82+
7683
// Track installation results
7784
var succeeded, failed []string
7885

cmd/list.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ Use --agent (-a) to list skills for specific agents (checks agent directories).`
2323
all, _ := cmd.Flags().GetBool("all")
2424
agents, _ := cmd.Flags().GetStringSlice("agent")
2525

26+
// Ensure project is initialized for non-global operations
27+
if !global && !all && len(agents) == 0 {
28+
if !ensureInitialized() {
29+
return
30+
}
31+
}
32+
2633
// Validate agent names
2734
for _, agent := range agents {
2835
if !config.IsValidAgent(agent) {

cmd/outdated.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ Use --global to check global skills.`,
2222
Run: func(cmd *cobra.Command, args []string) {
2323
global, _ := cmd.Flags().GetBool("global")
2424

25-
cfg, err := config.LoadConfigByScope(global)
26-
if err != nil {
27-
if os.IsNotExist(err) && !global {
28-
fmt.Println("No ask.yaml found. Run 'ask init' first.")
25+
// Ensure project is initialized for non-global operations
26+
if !global {
27+
if !ensureInitialized() {
2928
return
3029
}
30+
}
31+
32+
cfg, err := config.LoadConfigByScope(global)
33+
if err != nil {
3134
fmt.Printf("Error loading config: %v\n", err)
3235
os.Exit(1)
3336
}

cmd/uninstall.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ If no agent is specified, uninstalls from .agent/skills/ by default.`,
2828
global, _ := cmd.Flags().GetBool("global")
2929
agents, _ := cmd.Flags().GetStringSlice("agent")
3030

31+
// Ensure project is initialized for non-global operations
32+
if !global && len(agents) == 0 {
33+
if !ensureInitialized() {
34+
return
35+
}
36+
}
37+
3138
// Validate agent names
3239
for _, agent := range agents {
3340
if !config.IsValidAgent(agent) {

cmd/update.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ Use --global to update global skills.`,
2828
Run: func(cmd *cobra.Command, args []string) {
2929
global, _ := cmd.Flags().GetBool("global")
3030

31-
cfg, err := config.LoadConfigByScope(global)
32-
if err != nil {
33-
if os.IsNotExist(err) && !global {
34-
fmt.Println("No ask.yaml found. Run 'ask init' first.")
31+
// Ensure project is initialized for non-global operations
32+
if !global {
33+
if !ensureInitialized() {
3534
return
3635
}
36+
}
37+
38+
cfg, err := config.LoadConfigByScope(global)
39+
if err != nil {
3740
fmt.Printf("Error loading config: %v\n", err)
3841
os.Exit(1)
3942
}

cmd/utils.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cmd
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/yeasy/ask/internal/config"
10+
)
11+
12+
// ensureInitialized checks if ask.yaml exists. If not, prompts user to init.
13+
// Returns true if initialized (or user chose to init), false if user declined.
14+
func ensureInitialized() bool {
15+
if _, err := os.Stat("ask.yaml"); err == nil {
16+
return true // Already initialized
17+
}
18+
19+
fmt.Print("Project not initialized. Run 'ask init' now? [Y/n]: ")
20+
reader := bufio.NewReader(os.Stdin)
21+
input, _ := reader.ReadString('\n')
22+
input = strings.TrimSpace(strings.ToLower(input))
23+
24+
// Default to yes
25+
if input == "" || input == "y" || input == "yes" {
26+
runInit()
27+
return true
28+
}
29+
30+
fmt.Println("Aborted. Run 'ask init' to initialize the project.")
31+
return false
32+
}
33+
34+
// runInit executes the initialization logic
35+
func runInit() {
36+
skillsDir := config.DefaultSkillsDir
37+
if err := os.MkdirAll(skillsDir, 0755); err != nil {
38+
fmt.Printf("Error creating skills directory: %v\n", err)
39+
return
40+
}
41+
42+
if err := config.CreateDefaultConfig(); err != nil {
43+
fmt.Printf("Error creating ask.yaml: %v\n", err)
44+
return
45+
}
46+
47+
fmt.Println("✓ Initialized ASK project")
48+
fmt.Printf(" Created: ask.yaml, %s/\n", skillsDir)
49+
}

0 commit comments

Comments
 (0)