|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/yeasy/ask/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +// updateCmd represents the update command |
| 14 | +var updateCmd = &cobra.Command{ |
| 15 | + Use: "update [skill-name]", |
| 16 | + Short: "Update installed skills to latest version", |
| 17 | + Long: `Update one or all installed skills to their latest versions. |
| 18 | +If no skill name is provided, updates all installed skills.`, |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + cfg, err := config.LoadConfig() |
| 21 | + if err != nil { |
| 22 | + if os.IsNotExist(err) { |
| 23 | + fmt.Println("No ask.yaml found. Run 'ask init' first.") |
| 24 | + return |
| 25 | + } |
| 26 | + fmt.Printf("Error loading config: %v\n", err) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + if len(cfg.Skills) == 0 { |
| 31 | + fmt.Println("No skills installed.") |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // Determine which skills to update |
| 36 | + var skillsToUpdate []string |
| 37 | + if len(args) > 0 { |
| 38 | + // Update specific skill |
| 39 | + skillName := args[0] |
| 40 | + found := false |
| 41 | + for _, s := range cfg.Skills { |
| 42 | + if s == skillName { |
| 43 | + found = true |
| 44 | + break |
| 45 | + } |
| 46 | + } |
| 47 | + if !found { |
| 48 | + fmt.Printf("Skill '%s' is not installed.\n", skillName) |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | + skillsToUpdate = []string{skillName} |
| 52 | + } else { |
| 53 | + // Update all skills |
| 54 | + skillsToUpdate = cfg.Skills |
| 55 | + } |
| 56 | + |
| 57 | + for _, skillName := range skillsToUpdate { |
| 58 | + skillPath := filepath.Join("skills", skillName) |
| 59 | + |
| 60 | + // Check if it's a git repository |
| 61 | + gitDir := filepath.Join(skillPath, ".git") |
| 62 | + if _, err := os.Stat(gitDir); os.IsNotExist(err) { |
| 63 | + fmt.Printf("Skipping %s (not a git repository)\n", skillName) |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + fmt.Printf("Updating %s...\n", skillName) |
| 68 | + |
| 69 | + // Run git pull |
| 70 | + gitCmd := exec.Command("git", "pull", "--rebase") |
| 71 | + gitCmd.Dir = skillPath |
| 72 | + gitCmd.Stdout = os.Stdout |
| 73 | + gitCmd.Stderr = os.Stderr |
| 74 | + |
| 75 | + if err := gitCmd.Run(); err != nil { |
| 76 | + fmt.Printf(" Failed to update %s: %v\n", skillName, err) |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Printf(" Updated %s successfully!\n", skillName) |
| 81 | + } |
| 82 | + }, |
| 83 | +} |
| 84 | + |
| 85 | +func init() { |
| 86 | + rootCmd.AddCommand(updateCmd) |
| 87 | +} |
0 commit comments