|
| 1 | +/* |
| 2 | +Copyright © 2024 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/cpunion/go-python/cmd/internal/create" |
| 14 | + "github.com/cpunion/go-python/cmd/internal/install" |
| 15 | + "github.com/fatih/color" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +var bold = color.New(color.Bold).SprintFunc() |
| 20 | + |
| 21 | +// isDirEmpty checks if a directory is empty |
| 22 | +func isDirEmpty(path string) (bool, error) { |
| 23 | + f, err := os.Open(path) |
| 24 | + if err != nil { |
| 25 | + return false, err |
| 26 | + } |
| 27 | + defer f.Close() |
| 28 | + |
| 29 | + _, err = f.Readdirnames(1) |
| 30 | + if err == io.EOF { |
| 31 | + return true, nil |
| 32 | + } |
| 33 | + return false, err |
| 34 | +} |
| 35 | + |
| 36 | +// promptYesNo asks user for confirmation |
| 37 | +func promptYesNo(prompt string) bool { |
| 38 | + reader := bufio.NewReader(os.Stdin) |
| 39 | + fmt.Printf("%s [y/N]: ", prompt) |
| 40 | + response, err := reader.ReadString('\n') |
| 41 | + if err != nil { |
| 42 | + return false |
| 43 | + } |
| 44 | + |
| 45 | + response = strings.ToLower(strings.TrimSpace(response)) |
| 46 | + return response == "y" || response == "yes" |
| 47 | +} |
| 48 | + |
| 49 | +// initCmd represents the init command |
| 50 | +var initCmd = &cobra.Command{ |
| 51 | + Use: "init [path]", |
| 52 | + Short: "Initialize a new go-python project", |
| 53 | + Long: `Initialize a new go-python project in the specified directory. |
| 54 | +If no path is provided, it will initialize in the current directory. |
| 55 | +
|
| 56 | +Example: |
| 57 | + gopy init |
| 58 | + gopy init my-project |
| 59 | + gopy init --debug my-project |
| 60 | + gopy init -v my-project`, |
| 61 | + Run: func(cmd *cobra.Command, args []string) { |
| 62 | + // Get project path |
| 63 | + projectPath := "." |
| 64 | + if len(args) > 0 { |
| 65 | + projectPath = args[0] |
| 66 | + } |
| 67 | + |
| 68 | + // Get flags |
| 69 | + debug, _ := cmd.Flags().GetBool("debug") |
| 70 | + verbose, _ := cmd.Flags().GetBool("verbose") |
| 71 | + goVersion, _ := cmd.Flags().GetString("go-version") |
| 72 | + pyVersion, _ := cmd.Flags().GetString("python-version") |
| 73 | + pyBuildDate, _ := cmd.Flags().GetString("python-build-date") |
| 74 | + pyFreeThreaded, _ := cmd.Flags().GetBool("python-free-threaded") |
| 75 | + tinyPkgConfigVersion, _ := cmd.Flags().GetString("tiny-pkg-config-version") |
| 76 | + |
| 77 | + // Check if directory exists |
| 78 | + if _, err := os.Stat(projectPath); err == nil { |
| 79 | + // Directory exists, check if it's empty |
| 80 | + empty, err := isDirEmpty(projectPath) |
| 81 | + if err != nil { |
| 82 | + fmt.Printf("Error checking directory: %v\n", err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if !empty { |
| 87 | + if !promptYesNo(fmt.Sprintf("Directory %s is not empty. Do you want to continue?", projectPath)) { |
| 88 | + fmt.Println("Operation cancelled") |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + } else if !os.IsNotExist(err) { |
| 93 | + fmt.Printf("Error checking directory: %v\n", err) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // Create project using the create package |
| 98 | + fmt.Printf("\n%s\n", bold("Creating project...")) |
| 99 | + if err := create.Project(projectPath, verbose); err != nil { |
| 100 | + fmt.Printf("Error creating project: %v\n", err) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // Install dependencies |
| 105 | + fmt.Printf("\n%s\n", bold("Installing dependencies...")) |
| 106 | + if err := install.Dependencies(projectPath, goVersion, tinyPkgConfigVersion, pyVersion, pyBuildDate, pyFreeThreaded, debug, verbose); err != nil { |
| 107 | + fmt.Printf("Error installing dependencies: %v\n", err) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Printf("\n%s\n", bold("Successfully initialized go-python project in "+projectPath)) |
| 112 | + fmt.Println("\nNext steps:") |
| 113 | + fmt.Println("1. cd", projectPath) |
| 114 | + fmt.Println("2. gopy run .") |
| 115 | + }, |
| 116 | +} |
| 117 | + |
| 118 | +func init() { |
| 119 | + rootCmd.AddCommand(initCmd) |
| 120 | + initCmd.Flags().Bool("debug", false, "Install debug version of Python (not available on Windows)") |
| 121 | + initCmd.Flags().BoolP("verbose", "v", false, "Enable verbose output") |
| 122 | + initCmd.Flags().String("tiny-pkg-config-version", "v0.2.0", "tiny-pkg-config version to install") |
| 123 | + initCmd.Flags().String("go-version", "1.23.3", "Go version to install") |
| 124 | + initCmd.Flags().String("python-version", "3.13.0", "Python version to install") |
| 125 | + initCmd.Flags().String("python-build-date", "20241016", "Python build date") |
| 126 | + initCmd.Flags().Bool("python-free-threaded", false, "Install free-threaded version of Python") |
| 127 | +} |
0 commit comments