|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
6 | 9 |
|
7 | 10 | "github.com/spf13/cobra" |
8 | 11 | ) |
9 | 12 |
|
10 | 13 | var initCmd = &cobra.Command{ |
11 | 14 | Use: "init", |
12 | 15 | Short: "Create a runtime.yml in the current directory", |
13 | | - RunE: func(cmd *cobra.Command, args []string) error { |
14 | | - if _, err := os.Stat("runtime.yml"); err == nil { |
15 | | - return fmt.Errorf("runtime.yml already exists") |
| 16 | + RunE: runInit, |
| 17 | +} |
| 18 | + |
| 19 | +func init() { |
| 20 | + initCmd.Flags().String("php", "", "PHP version (default: 8.3)") |
| 21 | + initCmd.Flags().String("mysql", "", "MySQL version (optional)") |
| 22 | + initCmd.Flags().Bool("nginx", true, "Use Nginx as web server") |
| 23 | + initCmd.Flags().Bool("apache", false, "Use Apache as web server") |
| 24 | + initCmd.Flags().BoolP("yes", "y", false, "Skip interactive prompts") |
| 25 | + rootCmd.AddCommand(initCmd) |
| 26 | +} |
| 27 | + |
| 28 | +func runInit(cmd *cobra.Command, args []string) error { |
| 29 | + cwd, err := os.Getwd() |
| 30 | + if err != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + manifestPath := filepath.Join(cwd, "runtime.yml") |
| 34 | + if _, err := os.Stat(manifestPath); err == nil { |
| 35 | + yes, _ := cmd.Flags().GetBool("yes") |
| 36 | + if !yes && !confirm("runtime.yml already exists. Overwrite? [y/N] ", false) { |
| 37 | + fmt.Println("Cancelled.") |
| 38 | + return nil |
16 | 39 | } |
| 40 | + } |
17 | 41 |
|
18 | | - content := `phpvm: "1" |
| 42 | + if detected := detectProjectType(cwd); detected != "" { |
| 43 | + fmt.Printf("Detected: %s\n\n", detected) |
| 44 | + } |
19 | 45 |
|
20 | | -runtime: |
21 | | - php: "8.3" |
| 46 | + phpVersion := promptOrFlag(cmd, "php", "PHP version", "8.3") |
| 47 | + mysqlVersion := promptOrFlag(cmd, "mysql", "MySQL version (leave blank to skip)", "") |
| 48 | + serverEngine := "nginx" |
| 49 | + if apache, _ := cmd.Flags().GetBool("apache"); apache { |
| 50 | + serverEngine = "apache" |
| 51 | + } |
22 | 52 |
|
23 | | -database: |
24 | | - engine: none |
| 53 | + content := buildManifestContent(phpVersion, mysqlVersion, serverEngine, cwd) |
| 54 | + if err := os.WriteFile(manifestPath, []byte(content), 0644); err != nil { |
| 55 | + return fmt.Errorf("cannot write runtime.yml: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + fmt.Println("Created runtime.yml") |
| 59 | + fmt.Println("Next: run 'phpvm install' then 'phpvm up'") |
| 60 | + return nil |
| 61 | +} |
25 | 62 |
|
26 | | -server: |
27 | | - engine: nginx |
28 | | - root: public |
29 | | -` |
30 | | - if err := os.WriteFile("runtime.yml", []byte(content), 0644); err != nil { |
31 | | - return fmt.Errorf("cannot write runtime.yml: %w", err) |
| 63 | +func buildManifestContent(php, mysql, server, projectDir string) string { |
| 64 | + name := filepath.Base(projectDir) |
| 65 | + domain := strings.ToLower(strings.ReplaceAll(name, "_", "-")) + ".local" |
| 66 | + lines := []string{ |
| 67 | + "# runtime.yml - managed by phpvm", |
| 68 | + `phpvm: "1"`, |
| 69 | + "", |
| 70 | + "runtime:", |
| 71 | + fmt.Sprintf(` php: "%s"`, php), |
| 72 | + } |
| 73 | + if mysql != "" { |
| 74 | + lines = append(lines, |
| 75 | + "", |
| 76 | + "database:", |
| 77 | + " engine: mysql", |
| 78 | + fmt.Sprintf(` version: "%s"`, mysql), |
| 79 | + ) |
| 80 | + } else { |
| 81 | + lines = append(lines, "", "database:", " engine: none") |
| 82 | + } |
| 83 | + lines = append(lines, |
| 84 | + "", |
| 85 | + "server:", |
| 86 | + fmt.Sprintf(" engine: %s", server), |
| 87 | + " root: public", |
| 88 | + "", |
| 89 | + "project:", |
| 90 | + fmt.Sprintf(" name: %s", name), |
| 91 | + fmt.Sprintf(" domain: %s", domain), |
| 92 | + ) |
| 93 | + return strings.Join(lines, "\n") + "\n" |
| 94 | +} |
| 95 | + |
| 96 | +func detectProjectType(dir string) string { |
| 97 | + data, err := os.ReadFile(filepath.Join(dir, "composer.json")) |
| 98 | + if err != nil { |
| 99 | + if _, statErr := os.Stat(filepath.Join(dir, "index.php")); statErr == nil { |
| 100 | + return "PHP project" |
| 101 | + } |
| 102 | + return "" |
| 103 | + } |
| 104 | + content := string(data) |
| 105 | + switch { |
| 106 | + case strings.Contains(content, "laravel/framework"): |
| 107 | + if strings.Contains(content, `"^11`) { |
| 108 | + return "Laravel 11 project" |
| 109 | + } |
| 110 | + if strings.Contains(content, `"^10`) { |
| 111 | + return "Laravel 10 project" |
32 | 112 | } |
| 113 | + return "Laravel project" |
| 114 | + case strings.Contains(content, "symfony/symfony"): |
| 115 | + return "Symfony project" |
| 116 | + case strings.Contains(strings.ToLower(content), "wordpress"): |
| 117 | + return "WordPress project" |
| 118 | + default: |
| 119 | + return "PHP project (composer.json found)" |
| 120 | + } |
| 121 | +} |
33 | 122 |
|
34 | | - fmt.Println("Created runtime.yml") |
35 | | - fmt.Println("Next: run 'phpvm install' then 'phpvm up'") |
36 | | - return nil |
37 | | - }, |
| 123 | +func promptOrFlag(cmd *cobra.Command, flag, label, defaultVal string) string { |
| 124 | + if cmd.Flags().Changed(flag) { |
| 125 | + val, _ := cmd.Flags().GetString(flag) |
| 126 | + return val |
| 127 | + } |
| 128 | + yes, _ := cmd.Flags().GetBool("yes") |
| 129 | + if yes { |
| 130 | + return defaultVal |
| 131 | + } |
| 132 | + if defaultVal == "" { |
| 133 | + fmt.Printf("%s: ", label) |
| 134 | + } else { |
| 135 | + fmt.Printf("%s [%s]: ", label, defaultVal) |
| 136 | + } |
| 137 | + reader := bufio.NewReader(os.Stdin) |
| 138 | + answer, _ := reader.ReadString('\n') |
| 139 | + answer = strings.TrimSpace(answer) |
| 140 | + if answer == "" { |
| 141 | + return defaultVal |
| 142 | + } |
| 143 | + return answer |
38 | 144 | } |
39 | 145 |
|
40 | | -func init() { |
41 | | - rootCmd.AddCommand(initCmd) |
| 146 | +func confirm(prompt string, defaultYes bool) bool { |
| 147 | + fmt.Print(prompt) |
| 148 | + reader := bufio.NewReader(os.Stdin) |
| 149 | + answer, _ := reader.ReadString('\n') |
| 150 | + answer = strings.ToLower(strings.TrimSpace(answer)) |
| 151 | + if answer == "" { |
| 152 | + return defaultYes |
| 153 | + } |
| 154 | + return strings.HasPrefix(answer, "y") |
42 | 155 | } |
0 commit comments