|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + skillsRepoURL = "https://github.com/loops-so/skills" |
| 15 | + skillName = "loops-cli" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + skillInstallGlobal bool |
| 20 | + skillInstallYes bool |
| 21 | + skillInstallAll bool |
| 22 | +) |
| 23 | + |
| 24 | +func skillInstallArgs(global, yes, all bool) []string { |
| 25 | + args := []string{} |
| 26 | + // --yes for npx (--all implies non-interactive intent) |
| 27 | + if yes || all { |
| 28 | + args = append(args, "--yes") |
| 29 | + } |
| 30 | + args = append(args, "skills", "add", skillsRepoURL) |
| 31 | + if all { |
| 32 | + args = append(args, "--all") |
| 33 | + } else { |
| 34 | + args = append(args, "--skill", skillName) |
| 35 | + } |
| 36 | + if global { |
| 37 | + args = append(args, "--global") |
| 38 | + } |
| 39 | + // --yes for skills (--all already implies -y to skills) |
| 40 | + if yes && !all { |
| 41 | + args = append(args, "--yes") |
| 42 | + } |
| 43 | + return args |
| 44 | +} |
| 45 | + |
| 46 | +func runSkillInstall(stderr io.Writer, global, yes, all bool) error { |
| 47 | + if _, err := exec.LookPath("npx"); err != nil { |
| 48 | + return fmt.Errorf("npx not found on PATH") |
| 49 | + } |
| 50 | + |
| 51 | + args := skillInstallArgs(global, yes, all) |
| 52 | + fmt.Fprintf(stderr, "Running: npx %s\n", strings.Join(args, " ")) |
| 53 | + |
| 54 | + c := exec.Command("npx", args...) |
| 55 | + c.Stdin = os.Stdin |
| 56 | + c.Stdout = stderr |
| 57 | + c.Stderr = stderr |
| 58 | + if err := c.Run(); err != nil { |
| 59 | + return fmt.Errorf("npx: %w", err) |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +var skillInstallCmd = &cobra.Command{ |
| 65 | + Use: "install", |
| 66 | + Short: "Install the Loops CLI skill via 'skills add'", |
| 67 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | + if err := runSkillInstall(os.Stderr, skillInstallGlobal, skillInstallYes, skillInstallAll); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if isJSONOutput() { |
| 72 | + return printJSON(cmd.OutOrStdout(), Result{Success: true, Message: "skill installed"}) |
| 73 | + } |
| 74 | + return nil |
| 75 | + }, |
| 76 | +} |
| 77 | + |
| 78 | +func init() { |
| 79 | + skillInstallCmd.Flags().BoolVarP(&skillInstallGlobal, "global", "g", false, "Install the skill globally") |
| 80 | + skillInstallCmd.Flags().BoolVarP(&skillInstallYes, "yes", "y", false, "Skip confirmation prompts") |
| 81 | + skillInstallCmd.Flags().BoolVarP(&skillInstallAll, "all", "a", false, "Install all Loops skills (not just the CLI skill)") |
| 82 | + skillCmd.AddCommand(skillInstallCmd) |
| 83 | +} |
0 commit comments