Skip to content

Commit

Permalink
scrap auto flag (#16)
Browse files Browse the repository at this point in the history
* scrap auto flag, make auto-detect my default and implement logic for error if a language isn't supported

* update README with new instructions
  • Loading branch information
hacktivist123 authored Aug 4, 2023
1 parent 9c29d68 commit f921519
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 51 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Go pkg:
go get -u github.com/hacktivist123/goignore/cmd/goignore@latest
```
## Usage

## Auto-Detecting Language
If you want goignore to automatically detect the programming language based on the files in your project directory, you can run the following command:

```sh
goignore new
```

### Generating .gitignore File
To generate a .gitignore file for a specific programming language, you can use the following command:

Expand All @@ -38,12 +46,7 @@ goignore new --language=python
```
You can also use the -l shorthand for the --language flag.

## Auto-Detecting Language
If you want goignore to automatically detect the programming language based on the files in your project directory, you can use the auto keyword:

```sh
goignore new --language=auto
```

## Listing Supported Languages
To list all supported programming languages for generating .gitignore files, you can use the following command:
Expand Down Expand Up @@ -74,7 +77,7 @@ Example:

- [ ] Allow users to choose to initialize the git repo in their folder

- [ ] Use `goignore new` to auto-detect language and scrap `goignore new language=auto`
- [x] Use `goignore new` to auto-detect language and scrap `goignore new language=auto`

- [ ] Github actions to build main.go file and automatically create an executable file that can be downloaded

Expand Down
77 changes: 32 additions & 45 deletions cmd/goignore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main

import (
"embed"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"

"github.com/fatih/color"
Expand Down Expand Up @@ -35,20 +36,20 @@ var newCmd = &cobra.Command{
Use: "new",
Short: "Generate and add .gitignore file to your project",
Run: func(cmd *cobra.Command, args []string) {
if language == "" && !autoDetect {
color.Red("Please provide a programming language.")
return
}

if autoDetect {
if language == "" || autoDetect {
language = detectLanguage()
if language == "" {
color.Red("Unable to auto-detect programming language. Please provide a language manually.")
return
}
}

// Check if .git repo exists, if not initialize it
if language == "" {
color.Red("Please provide a programming language.")
return
}

// // Check if .git repo exists, if not initialize it
// _, err := os.Stat(".git")
// if err != nil {
// color.Yellow("Initializing a new Git repository...")
Expand All @@ -61,8 +62,14 @@ var newCmd = &cobra.Command{

// Read .gitignore template content from file
templateContent, err := readTemplateFile(language)

// if the language template does not exist
if errors.Is(err, fs.ErrNotExist) {
err = fmt.Errorf("language '%s' not supported", language)
}

if err != nil {
color.Red("Error reading template file:", err)
color.Red("Error: %s", err)
return
}

Expand Down Expand Up @@ -97,11 +104,11 @@ var listCmd = &cobra.Command{
}

func detectLanguage() string {
fileExt := ""
fileExt := ""
err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if err != nil {
return err
}
ext := filepath.Ext(path)
for lang, exts := range extensions {
for _, e := range exts {
Expand All @@ -111,10 +118,10 @@ func detectLanguage() string {
}
}
return nil
})
if err != nil {
return ""
}
})
if err != nil {
return ""
}
return fileExt
}

Expand All @@ -126,32 +133,12 @@ func getSupportedLanguages() []string {
}
return result
}

func init() {
rootCmd.AddCommand(newCmd)
rootCmd.AddCommand(listCmd)

// Define the 'language' flag for the newCmd
newCmd.Flags().StringVarP(&language, "language", "l", "", "Programming language for .gitignore file (use 'auto' for auto-detection)")

// Set PersistentPreRun function to handle flag validation and auto-detection
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
// Skip flag validation for the 'list' command
if cmd.Name() != listCmd.Name() {
if language == "" {
fmt.Println("Error: Please provide a programming language or use 'auto' for auto-detection.")
os.Exit(1)
}

if language == "auto" {
language = detectLanguage()
if language == "" {
fmt.Println("Error: Unable to auto-detect programming language. Please provide a language manually.")
os.Exit(1)
}
}
}
}
// Define the 'language' and 'auto-detect' flags for the newCmd
newCmd.Flags().StringVarP(&language, "language", "l", "", "Programming language for .gitignore file")
}

func main() {
Expand Down Expand Up @@ -188,9 +175,9 @@ func generateGitignore(content string) error {
return nil
}

func execCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// func execCommand(command string, args ...string) error {
// cmd := exec.Command(command, args...)
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
// return cmd.Run()
// }

0 comments on commit f921519

Please sign in to comment.