Skip to content

Commit

Permalink
Merge pull request #1 from loftwah/dl/refactor
Browse files Browse the repository at this point in the history
make gooder
  • Loading branch information
loftwah authored Sep 2, 2024
2 parents 8b83719 + 714e152 commit 56a86d4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 95 deletions.
21 changes: 4 additions & 17 deletions cmd/grabitsh/project_detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
func DetectProjectTypes() []string {
var projectTypes []string

// Node.js / JavaScript
if fileExists("package.json") {
projectTypes = append(projectTypes, "Node.js project")
if fileExists("next.config.js") {
Expand All @@ -21,47 +20,38 @@ func DetectProjectTypes() []string {
}
}

// Ruby on Rails
if fileExists("config/application.rb") && dirExists("app") && dirExists("config") {
projectTypes = append(projectTypes, "Ruby on Rails project")
}

// Laravel (PHP)
if fileExists("artisan") && dirExists("app") && dirExists("public") {
projectTypes = append(projectTypes, "Laravel (PHP) project")
}

// Django (Python)
if fileExists("manage.py") && dirExists("templates") {
projectTypes = append(projectTypes, "Django (Python) project")
}

// Flask (Python)
if fileExists("app.py") || fileExists("wsgi.py") {
projectTypes = append(projectTypes, "Flask (Python) project")
projectTypes = append(projectTypes, "Flask/FastAPI (Python) project")
}

// Vue.js
if fileExists("vue.config.js") {
projectTypes = append(projectTypes, "Vue.js project")
}

// Angular
if fileExists("angular.json") {
projectTypes = append(projectTypes, "Angular project")
}

// .NET Core
if fileExists("Program.cs") && dirExists("bin") && dirExists("obj") {
projectTypes = append(projectTypes, ".NET Core project")
}

// Java Spring Boot
if fileExists("pom.xml") && dirExists("src/main/java") {
projectTypes = append(projectTypes, "Java Spring Boot project")
}

// Go
if fileExists("go.mod") {
projectTypes = append(projectTypes, "Go project")
}
Expand All @@ -70,14 +60,11 @@ func DetectProjectTypes() []string {
}

func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
info, err := os.Stat(filename)
return err == nil && !info.IsDir()
}

func dirExists(dirname string) bool {
info, err := os.Stat(dirname)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
return err == nil && info.IsDir()
}
118 changes: 53 additions & 65 deletions cmd/grabitsh/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,44 @@ func Execute() error {
func runGrabit(cmd *cobra.Command, args []string) {
var outputBuffer bytes.Buffer

// Repository structure
outputBuffer.WriteString("### Repository Structure ###\n")
outputBuffer.WriteString(runCommand("ls", "-lah"))
// Collect all sections
collectRepoStructure(&outputBuffer)
collectGitInfo(&outputBuffer)
collectConfigFiles(&outputBuffer)
collectLargeFiles(&outputBuffer)
collectFileTypeSummary(&outputBuffer)
collectRecentlyModifiedFiles(&outputBuffer)
collectProjectTypes(&outputBuffer)

// Output results
finalizeOutput(outputBuffer.String())
}

// Helper functions to collect information
func collectRepoStructure(buffer *bytes.Buffer) {
buffer.WriteString("### Repository Structure ###\n")
buffer.WriteString(runCommand("ls", "-lah"))
if _, err := exec.LookPath("tree"); err == nil {
outputBuffer.WriteString(runCommand("tree", "-L", "2", "-a"))
buffer.WriteString(runCommand("tree", "-L", "2", "-a"))
} else {
outputBuffer.WriteString("(Tree command not available)\n")
buffer.WriteString("(Tree command not available)\n")
}
}

func collectGitInfo(buffer *bytes.Buffer) {
buffer.WriteString("\n### Git Information ###\n")
buffer.WriteString("Recent Commits:\n")
buffer.WriteString(runCommand("git", "log", "--oneline", "-n", "5"))
buffer.WriteString("\nBranches:\n")
buffer.WriteString(runCommand("git", "branch", "-a"))
buffer.WriteString("\nRemote Repositories:\n")
buffer.WriteString(runCommand("git", "remote", "-v"))
buffer.WriteString("\nGit Status:\n")
buffer.WriteString(runCommand("git", "status", "--short"))
}

// Git information
outputBuffer.WriteString("\n### Git Information ###\n")
outputBuffer.WriteString("Recent Commits:\n")
outputBuffer.WriteString(runCommand("git", "log", "--oneline", "-n", "5"))
outputBuffer.WriteString("\nBranches:\n")
outputBuffer.WriteString(runCommand("git", "branch", "-a"))
outputBuffer.WriteString("\nRemote Repositories:\n")
outputBuffer.WriteString(runCommand("git", "remote", "-v"))
outputBuffer.WriteString("\nGit Status:\n")
outputBuffer.WriteString(runCommand("git", "status", "--short"))

// Configuration and important files
outputBuffer.WriteString("\n### Configuration and Important Files ###\n")
func collectConfigFiles(buffer *bytes.Buffer) {
buffer.WriteString("\n### Configuration and Important Files ###\n")
importantFiles := []string{
".gitignore", "README*", "LICENSE*", "Dockerfile*", ".env*",
"Makefile", "package.json", "go.mod", "requirements.txt",
Expand All @@ -71,32 +87,33 @@ func runGrabit(cmd *cobra.Command, args []string) {
matches, err := filepath.Glob(filepath.Join(".", file))
if err == nil && len(matches) > 0 {
for _, match := range matches {
outputBuffer.WriteString(match + "\n")
buffer.WriteString(match + "\n")
}
}
}
}

// Large files
outputBuffer.WriteString("\n### Large Files (top 5) ###\n")
outputBuffer.WriteString(findLargeFiles())
func collectLargeFiles(buffer *bytes.Buffer) {
buffer.WriteString("\n### Large Files (top 5) ###\n")
buffer.WriteString(runCommand("bash", "-c", "find . -type f -exec du -h {} + | sort -rh | head -n 5"))
}

// File types summary
outputBuffer.WriteString("\n### File Types Summary ###\n")
outputBuffer.WriteString(summarizeFileTypes())
func collectFileTypeSummary(buffer *bytes.Buffer) {
buffer.WriteString("\n### File Types Summary ###\n")
buffer.WriteString(runCommand("bash", "-c", "find . -type f | sed -e 's/.*\\.//' | sort | uniq -c | sort -rn | head -n 10"))
}

// Recent changes
outputBuffer.WriteString("\n### Recently Modified Files ###\n")
outputBuffer.WriteString(findRecentlyModifiedFiles())
func collectRecentlyModifiedFiles(buffer *bytes.Buffer) {
buffer.WriteString("\n### Recently Modified Files ###\n")
buffer.WriteString(runCommand("find", ".", "-type", "f", "-mtime", "-7", "-not", "-path", "./.git/*"))
}

// Project type detection
outputBuffer.WriteString("\n### Project Type Detection ###\n")
func collectProjectTypes(buffer *bytes.Buffer) {
buffer.WriteString("\n### Project Type Detection ###\n")
projectTypes := DetectProjectTypes()
for _, projectType := range projectTypes {
outputBuffer.WriteString(fmt.Sprintf("- %s\n", projectType))
buffer.WriteString(fmt.Sprintf("- %s\n", projectType))
}

// Output results
finalizeOutput(outputBuffer.String())
}

func runCommand(name string, arg ...string) string {
Expand All @@ -108,40 +125,12 @@ func runCommand(name string, arg ...string) string {
return string(out)
}

func findLargeFiles() string {
cmd := exec.Command("bash", "-c", "find . -type f -exec du -h {} + | sort -rh | head -n 5")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("Error finding large files: %v\n", err)
}
return string(out)
}

func summarizeFileTypes() string {
cmd := exec.Command("bash", "-c", "find . -type f | sed -e 's/.*\\.//' | sort | uniq -c | sort -rn | head -n 10")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("Error summarizing file types: %v\n", err)
}
return string(out)
}

func findRecentlyModifiedFiles() string {
cmd := exec.Command("find", ".", "-type", "f", "-mtime", "-7", "-not", "-path", "./.git/*")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("Error finding recently modified files: %v\n", err)
}
return string(out)
}

func finalizeOutput(content string) {
switch outputMethod {
case "stdout":
color.Green(content)
case "clipboard":
err := clipboard.WriteAll(content)
if err != nil {
if err := clipboard.WriteAll(content); err != nil {
color.Red("Failed to copy to clipboard: %v", err)
} else {
color.Green("Copied to clipboard successfully.")
Expand All @@ -151,8 +140,7 @@ func finalizeOutput(content string) {
color.Red("Output file path must be specified when using file output method.")
return
}
err := os.WriteFile(outputFile, []byte(content), 0644)
if err != nil {
if err := os.WriteFile(outputFile, []byte(content), 0644); err != nil {
color.Red("Failed to write to file: %v", err)
} else {
color.Green("Output written to file: %s", outputFile)
Expand Down
28 changes: 15 additions & 13 deletions pages/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,23 @@ <h2 class="text-3xl font-extrabold text-gray-900">How to Use GRABIT.SH</h2>
<strong>Generate a Custom Script Using AI:</strong> Copy the output from GRABIT.SH and use the prompt below to generate a tailored bash script with AI (ChatGPT, Claude, etc.):
<h3 class="text-2xl font-bold text-gray-900 mt-8">Prompt for AI:</h3>
<pre class="bg-gray-100 p-4 rounded mt-2 text-sm">
You are an AI expert in DevOps and scripting. I have used a tool to gather detailed information about my project repository. Below is the output:
You are an expert in DevOps and Bash scripting. Based on the provided output, I need you to generate a comprehensive Bash script that performs the following tasks:

[PASTE THE OUTPUT FROM GRABIT.SH HERE]
1. Analyzes the project structure and lists all directories and files in a clear, hierarchical format.
2. Displays the contents of important configuration files such as .yml, .json, .rb, .js, and Docker-related files.
3. Identifies large files and lists them with their file paths to provide insights into storage usage.
4. Summarizes recent Git commits, branches, and statuses to give an overview of repository activity.
5. Supports multiple output methods: terminal display, clipboard copy, or saving to a file, based on user preference.
6. Checks for required dependencies and provides clear installation instructions if any are missing.
7. Ensures the script is modular, well-commented, follows best practices, and is optimized for performance and readability.

Based on this output, I want you to generate a bash script that:
1. Analyzes the project structure and lists all directories and files.
2. Displays the contents of all configuration files such as .yml, .json, .rb, .js, and Docker-related files.
3. Provides insights into large files and their locations.
4. Summarizes recent Git commits, branches, and statuses.
5. Ensures that the script can output to the terminal, clipboard, or a file based on user preference.
6. Checks for required dependencies and provides installation instructions if missing.
Requirements:
- The script should be modular and easy to understand, with functions clearly separated by task.
- Include robust error handling to manage missing files, failed commands, or any unexpected conditions.
- Ensure cross-platform compatibility for macOS and Linux environments, with appropriate commands for clipboard operations and other OS-specific tasks.
- Avoid including specific examples in the output; focus on generating a general-purpose script that meets these criteria.

Additionally, provide a brief instruction guide on how to use the generated script effectively for managing the repository.

Please ensure the script is well-commented, follows best practices, and is optimized for performance and readability.</pre>
Ensure the final script adheres to these requirements and accurately reflects the provided output.</pre>
</li>
</ol>
</section>
Expand Down Expand Up @@ -125,4 +127,4 @@ <h2 class="text-3xl font-extrabold text-gray-900">Useful AI Prompts</h2>
</div>
</footer>
</body>
</html>
</html>

0 comments on commit 56a86d4

Please sign in to comment.