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