-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (111 loc) · 2.94 KB
/
main.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
128
129
130
131
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
)
// FunctionInfo represents information about a function
type FunctionInfo struct {
Name string
Parameters []string
ReturnType string
}
// FileInfo represents information about a file
type FileInfo struct {
Name string
FunctionInfo []FunctionInfo
}
type ProjectConfig struct {
Directory string
Language string
OutputPath string
}
func main() {
config, err := promptConfiguration()
if err != nil {
fmt.Printf("Error during configuration: %v\n", err)
return
}
files := extractFiles(config.Directory, config.Language)
readmeTemplate := generateReadmeTemplate(files)
writeReadmeFile(readmeTemplate, config.OutputPath)
}
func promptConfiguration() (ProjectConfig, error) {
config := ProjectConfig{}
questions := []*survey.Question{
{
Name: "directory",
Prompt: &survey.Input{
Message: "Enter the project directory path:",
Default: ".",
},
Validate: func(val interface{}) error {
str, ok := val.(string)
if !ok {
return fmt.Errorf("invalid input type")
}
if str == "" {
return fmt.Errorf("directory path cannot be empty")
}
// Clean the path and check if it exists
cleanPath := cleanWindowsPath(str)
if _, err := os.Stat(cleanPath); os.IsNotExist(err) {
return fmt.Errorf("directory does not exist: %s", cleanPath)
}
return nil
},
},
{
Name: "language",
Prompt: &survey.Select{
Message: "Choose the programming language:",
Options: []string{"go", "java", "python", "javascript"},
Default: "go",
},
},
{
Name: "outputPath",
Prompt: &survey.Input{
Message: "Enter the README.md output path:",
Default: "README.md",
},
Validate: survey.Required,
},
}
err := survey.Ask(questions, &config)
if err != nil {
return ProjectConfig{}, err
}
// Clean and handle paths
config.Directory = cleanWindowsPath(config.Directory)
config.OutputPath = handleOutputPath(cleanWindowsPath(config.OutputPath), config.Directory)
// Print the paths for verification
fmt.Printf("\nUsing directory: %s\n", config.Directory)
return config, nil
}
func cleanWindowsPath(path string) string {
// Remove any surrounding quotes
path = strings.Trim(path, "\"'")
// Convert forward slashes to backslashes for Windows
path = strings.ReplaceAll(path, "/", "\\")
// Clean the path
path = filepath.Clean(path)
return path
}
func handleOutputPath(outputPath, projectDir string) string {
// If output path is absolute, use it as is
if filepath.IsAbs(outputPath) {
return outputPath
}
// If the output path is just a filename, place it in the current directory
if !strings.Contains(outputPath, "\\") && !strings.Contains(outputPath, "/") {
currentDir, err := os.Getwd()
if err == nil {
return filepath.Join(currentDir, outputPath)
}
}
// Otherwise, resolve relative to project directory
return filepath.Join(projectDir, outputPath)
}