|
1 | 1 | package cli |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
| 6 | + "io" |
5 | 7 | "os" |
6 | 8 | "path/filepath" |
| 9 | + "strings" |
7 | 10 |
|
| 11 | + "github.com/devrimcavusoglu/skern/internal/cli/instructions" |
8 | 12 | "github.com/devrimcavusoglu/skern/internal/output" |
9 | 13 | "github.com/spf13/cobra" |
10 | 14 | ) |
11 | 15 |
|
12 | 16 | func newInitCmd() *cobra.Command { |
| 17 | + var ( |
| 18 | + writeInstr bool |
| 19 | + toolForming bool |
| 20 | + printInstr bool |
| 21 | + targetPaths []string |
| 22 | + ) |
| 23 | + |
13 | 24 | cmd := &cobra.Command{ |
14 | 25 | Use: "init", |
15 | 26 | Short: "Initialize a .skern project directory", |
16 | | - Long: "Creates .skern/ and .skern/skills/ directories in the current project. Idempotent — safe to run multiple times.", |
17 | | - Args: cobra.NoArgs, |
| 27 | + Long: `Creates .skern/ and .skern/skills/ directories in the current project. |
| 28 | +Optionally writes a skern usage snippet into agent instruction files |
| 29 | +(AGENTS.md, CLAUDE.md, .claude/CLAUDE.md) so the agent uses skern for |
| 30 | +all skill-related tasks. |
| 31 | +
|
| 32 | +Idempotent — safe to run multiple times. The instruction snippet is |
| 33 | +wrapped in start/end markers so re-running updates the block in place.`, |
| 34 | + Args: cobra.NoArgs, |
18 | 35 | RunE: func(cmd *cobra.Command, args []string) error { |
19 | | - skillsDir := filepath.Join(".", ".skern", "skills") |
20 | | - |
21 | | - // Check if already initialized |
22 | | - if info, err := os.Stat(skillsDir); err == nil && info.IsDir() { |
23 | | - result := output.InitResult{ |
24 | | - Path: filepath.Join(".", ".skern"), |
25 | | - Created: false, |
26 | | - } |
27 | | - text := fmt.Sprintf("Already initialized: %s\n", filepath.Join(".", ".skern")) |
28 | | - getContext(cmd).Printer.PrintResult(result, text) |
29 | | - return nil |
30 | | - } |
31 | | - |
32 | | - if err := os.MkdirAll(skillsDir, 0o755); err != nil { |
33 | | - return fmt.Errorf("creating .skern directory: %w", err) |
34 | | - } |
35 | | - |
36 | | - result := output.InitResult{ |
37 | | - Path: filepath.Join(".", ".skern"), |
38 | | - Created: true, |
39 | | - } |
40 | | - text := fmt.Sprintf("Initialized skern project at %s\n", filepath.Join(".", ".skern")) |
41 | | - getContext(cmd).Printer.PrintResult(result, text) |
42 | | - return nil |
| 36 | + return runInit(cmd, runInitOpts{ |
| 37 | + writeInstr: writeInstr, |
| 38 | + toolForming: toolForming, |
| 39 | + printInstr: printInstr, |
| 40 | + targetPaths: targetPaths, |
| 41 | + }) |
43 | 42 | }, |
44 | 43 | } |
45 | 44 |
|
| 45 | + cmd.Flags().BoolVar(&writeInstr, "instructions", false, |
| 46 | + "write the skern usage snippet to agent instruction files (AGENTS.md, CLAUDE.md, .claude/CLAUDE.md by default)") |
| 47 | + cmd.Flags().BoolVar(&toolForming, "tool-forming-loop", false, |
| 48 | + "include the tool-forming-loop section in the instruction snippet (search-before-create workflow)") |
| 49 | + cmd.Flags().BoolVar(&printInstr, "print-instructions", false, |
| 50 | + "print the rendered instruction snippet to stdout instead of writing files") |
| 51 | + cmd.Flags().StringSliceVar(&targetPaths, "target", nil, |
| 52 | + "explicit instruction file path to write to; repeatable. Disables auto-discovery when set.") |
| 53 | + |
46 | 54 | return cmd |
47 | 55 | } |
| 56 | + |
| 57 | +type runInitOpts struct { |
| 58 | + writeInstr bool |
| 59 | + toolForming bool |
| 60 | + printInstr bool |
| 61 | + targetPaths []string |
| 62 | +} |
| 63 | + |
| 64 | +func runInit(cmd *cobra.Command, opts runInitOpts) error { |
| 65 | + cc := getContext(cmd) |
| 66 | + |
| 67 | + skillsDir := filepath.Join(".", ".skern", "skills") |
| 68 | + skernPath := filepath.Join(".", ".skern") |
| 69 | + created := true |
| 70 | + if info, err := os.Stat(skillsDir); err == nil && info.IsDir() { |
| 71 | + created = false |
| 72 | + } else if err := os.MkdirAll(skillsDir, 0o755); err != nil { |
| 73 | + return fmt.Errorf("creating .skern directory: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + instrResult, err := handleInstructions(cmd, cc, opts) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + result := output.InitResult{ |
| 82 | + Path: skernPath, |
| 83 | + Created: created, |
| 84 | + Instructions: instrResult, |
| 85 | + } |
| 86 | + text := initTextSummary(skernPath, created, instrResult) |
| 87 | + cc.Printer.PrintResult(result, text) |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// handleInstructions resolves the user's choices (flags + interactive |
| 92 | +// prompts), writes or prints the snippet, and returns a structured result. |
| 93 | +// Returns nil when the user did not opt in to writing instructions. |
| 94 | +func handleInstructions(cmd *cobra.Command, cc *CommandContext, opts runInitOpts) (*output.InstructionsResult, error) { |
| 95 | + wantInstr, wantToolForming, err := resolveInstructionChoices(cmd, cc, opts) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + if !wantInstr { |
| 100 | + return nil, nil |
| 101 | + } |
| 102 | + |
| 103 | + rendered := instructions.Render(wantToolForming) |
| 104 | + |
| 105 | + if opts.printInstr { |
| 106 | + _, _ = io.WriteString(cmd.OutOrStdout(), rendered) |
| 107 | + return &output.InstructionsResult{ |
| 108 | + ToolForming: wantToolForming, |
| 109 | + Targets: nil, |
| 110 | + Writes: nil, |
| 111 | + Printed: true, |
| 112 | + }, nil |
| 113 | + } |
| 114 | + |
| 115 | + targets, err := resolveTargets(opts) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + res := &output.InstructionsResult{ |
| 121 | + ToolForming: wantToolForming, |
| 122 | + Targets: targets, |
| 123 | + Writes: []output.InstructionWriteResult{}, |
| 124 | + } |
| 125 | + for _, t := range targets { |
| 126 | + w, werr := instructions.Write(t, rendered) |
| 127 | + if werr != nil { |
| 128 | + return nil, werr |
| 129 | + } |
| 130 | + res.Writes = append(res.Writes, output.InstructionWriteResult{ |
| 131 | + Path: w.Path, |
| 132 | + Action: w.Action, |
| 133 | + Created: w.Created, |
| 134 | + }) |
| 135 | + } |
| 136 | + return res, nil |
| 137 | +} |
| 138 | + |
| 139 | +// resolveInstructionChoices folds flag values + TTY interactivity into the |
| 140 | +// final (writeInstructions, toolFormingLoop) decision. |
| 141 | +func resolveInstructionChoices(cmd *cobra.Command, cc *CommandContext, opts runInitOpts) (bool, bool, error) { |
| 142 | + flags := cmd.Flags() |
| 143 | + |
| 144 | + wantInstr := opts.writeInstr || opts.printInstr || len(opts.targetPaths) > 0 |
| 145 | + wantToolForming := opts.toolForming |
| 146 | + |
| 147 | + // Skip prompting when JSON mode (machine-driven) or when stdin is not a |
| 148 | + // terminal (CI, scripts, redirected input, tests). |
| 149 | + in := cmd.InOrStdin() |
| 150 | + canPrompt := !cc.Printer.IsJSON() && isTerminal(in) |
| 151 | + |
| 152 | + // Prompts go to stderr so they never collide with --print-instructions |
| 153 | + // output on stdout when scripts pipe init through. |
| 154 | + promptOut := cmd.ErrOrStderr() |
| 155 | + |
| 156 | + if !wantInstr && canPrompt && !flags.Changed("instructions") && |
| 157 | + !flags.Changed("print-instructions") && len(opts.targetPaths) == 0 { |
| 158 | + yes, err := promptYesNo(in, promptOut, |
| 159 | + "Append skern usage instructions to agent config files (AGENTS.md, CLAUDE.md, .claude/CLAUDE.md)?", false) |
| 160 | + if err != nil { |
| 161 | + return false, false, err |
| 162 | + } |
| 163 | + wantInstr = yes |
| 164 | + } |
| 165 | + |
| 166 | + if wantInstr && !wantToolForming && canPrompt && !flags.Changed("tool-forming-loop") { |
| 167 | + yes, err := promptYesNo(in, promptOut, |
| 168 | + "Include tool-forming-loop section (instructs the agent to search before creating)?", false) |
| 169 | + if err != nil { |
| 170 | + return false, false, err |
| 171 | + } |
| 172 | + wantToolForming = yes |
| 173 | + } |
| 174 | + |
| 175 | + return wantInstr, wantToolForming, nil |
| 176 | +} |
| 177 | + |
| 178 | +// resolveTargets returns the list of files to write to. Explicit --target |
| 179 | +// paths win; otherwise auto-discovery probes CandidateProjectFiles in cwd. |
| 180 | +func resolveTargets(opts runInitOpts) ([]string, error) { |
| 181 | + if len(opts.targetPaths) > 0 { |
| 182 | + return opts.targetPaths, nil |
| 183 | + } |
| 184 | + return instructions.DiscoverTargets(".") |
| 185 | +} |
| 186 | + |
| 187 | +// isTerminal reports whether r is a *os.File backed by a character device |
| 188 | +// (terminal). Returns false for non-file readers (e.g. test injectees) so |
| 189 | +// tests never trigger interactive prompts. |
| 190 | +func isTerminal(r io.Reader) bool { |
| 191 | + f, ok := r.(*os.File) |
| 192 | + if !ok { |
| 193 | + return false |
| 194 | + } |
| 195 | + info, err := f.Stat() |
| 196 | + if err != nil { |
| 197 | + return false |
| 198 | + } |
| 199 | + return (info.Mode() & os.ModeCharDevice) != 0 |
| 200 | +} |
| 201 | + |
| 202 | +// promptYesNo writes prompt to w and reads a y/n answer from r. The default |
| 203 | +// (returned when the user just hits enter) is controlled by defaultYes. |
| 204 | +func promptYesNo(r io.Reader, w io.Writer, prompt string, defaultYes bool) (bool, error) { |
| 205 | + suffix := " [y/N]: " |
| 206 | + if defaultYes { |
| 207 | + suffix = " [Y/n]: " |
| 208 | + } |
| 209 | + if _, err := fmt.Fprint(w, prompt+suffix); err != nil { |
| 210 | + return false, err |
| 211 | + } |
| 212 | + scanner := bufio.NewScanner(r) |
| 213 | + if !scanner.Scan() { |
| 214 | + // EOF / no input — fall back to default. |
| 215 | + return defaultYes, nil |
| 216 | + } |
| 217 | + answer := strings.ToLower(strings.TrimSpace(scanner.Text())) |
| 218 | + switch answer { |
| 219 | + case "y", "yes": |
| 220 | + return true, nil |
| 221 | + case "n", "no": |
| 222 | + return false, nil |
| 223 | + case "": |
| 224 | + return defaultYes, nil |
| 225 | + default: |
| 226 | + return defaultYes, nil |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +func initTextSummary(skernPath string, created bool, instr *output.InstructionsResult) string { |
| 231 | + var b strings.Builder |
| 232 | + if created { |
| 233 | + fmt.Fprintf(&b, "Initialized skern project at %s\n", skernPath) |
| 234 | + } else { |
| 235 | + fmt.Fprintf(&b, "Already initialized: %s\n", skernPath) |
| 236 | + } |
| 237 | + if instr == nil { |
| 238 | + return b.String() |
| 239 | + } |
| 240 | + if instr.Printed { |
| 241 | + return b.String() // snippet already streamed to stdout above |
| 242 | + } |
| 243 | + if len(instr.Writes) == 0 { |
| 244 | + fmt.Fprintln(&b, "No agent instruction files found (looked for AGENTS.md, CLAUDE.md, .claude/CLAUDE.md).") |
| 245 | + fmt.Fprintln(&b, "Pass --target <path> to write to a specific file.") |
| 246 | + return b.String() |
| 247 | + } |
| 248 | + tfTag := "" |
| 249 | + if instr.ToolForming { |
| 250 | + tfTag = " (with tool-forming-loop)" |
| 251 | + } |
| 252 | + fmt.Fprintf(&b, "Wrote skern usage snippet%s to:\n", tfTag) |
| 253 | + for _, w := range instr.Writes { |
| 254 | + fmt.Fprintf(&b, " %s [%s]\n", w.Path, w.Action) |
| 255 | + } |
| 256 | + return b.String() |
| 257 | +} |
0 commit comments