Skip to content

Commit

Permalink
Add language selection and help options
Browse files Browse the repository at this point in the history
- Introduced `-lang` flag allowing users to specify their desired programming language for the random repo.
- Added `-h` flag to display the help text and provide users with usage information.
- Included validation for the provided language, ensuring it's one of the supported languages.
  • Loading branch information
omerbustun committed Aug 15, 2023
1 parent 68c0c26 commit a084573
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Discover a random GitHub repository from popular programming languages with just
Features
--------

* Randomly selects from popular programming languages.
* Randomly selects from popular programming languages or allows you to specify a language of your choice.
* Fetches repositories sorted by the latest commit.
* Provides a simple command-line interface.
* Configurable via a JSON configuration file.
* Option to select a specific programming language using the `-lang` flag.
* View the help text and options using the `-h` flag.

Prerequisites
-------------
Expand All @@ -21,23 +23,21 @@ Installation

1. Clone this repository:



```bash
git clone https://github.com/omerbustun/git-lucky.git
```

2. Navigate to the cloned directory and build the project:



```bash
go build
```

3. Run `git-lucky`:


```bash
./git-lucky
```
Expand All @@ -48,7 +48,7 @@ Configuration

1. Copy the sample configuration file:


```bash
cp config.sample.json config.json
```
Expand All @@ -68,10 +68,25 @@ Usage

Simply run:


```bash
./git-lucky
```

To specify a language:


```bash
./git-lucky -lang=Python
```

For help:


```bash
./git-lucky -h
```

Contributing
------------

Expand All @@ -80,4 +95,4 @@ Pull requests are welcome. For major changes, please open an issue first to disc
License
-------

This project is licensed under the GNU General Public License, version 3 (GPLv3). See the [LICENSE](LICENSE) file for the full license text.
This project is licensed under the GNU General Public License, version 3 (GPLv3). See the [LICENSE](LICENSE) file for the full license text.
28 changes: 26 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ func LoadConfig(filePath string) (Config, error) {
func main() {
rand.Seed(time.Now().UnixNano())

var language string
configPath := flag.String("config", "config.json", "Path to the configuration file")
flag.StringVar(&language, "lang", "", "Specify the programming language (e.g. Go, Python, JavaScript). If not specified, a random language will be chosen.")
help := flag.Bool("h", false, "Display the help text.")
flag.Parse()

if *help {
flag.Usage()
return
}

config, err := LoadConfig(*configPath)
if err != nil {
fmt.Printf("Error loading configuration: %s\n", err)
Expand All @@ -48,11 +56,27 @@ func main() {
}

languages := []string{"Python", "JavaScript", "Ruby", "Go", "Java", "C++", "TypeScript", "PHP", "C#", "Swift", "Kotlin", "Rust", "R", "Scala", "Perl", "Objective-C", "Lua", "Shell", "Haskell", "Dart"}
randomLanguage := languages[rand.Intn(len(languages))]

if language != "" {
validLanguage := false
for _, validLang := range languages {
if validLang == language {
validLanguage = true
break
}
}

if !validLanguage {
fmt.Printf("Error: Invalid language specified. Please choose from: %v\n", languages)
return
}
} else {
language = languages[rand.Intn(len(languages))]
}

randomPage := rand.Intn(10) + 1

apiURL := fmt.Sprintf("https://api.github.com/search/repositories?q=language:%s&sort=updated&order=desc&per_page=100&page=%d", randomLanguage, randomPage)
apiURL := fmt.Sprintf("https://api.github.com/search/repositories?q=language:%s&sort=updated&order=desc&per_page=100&page=%d", language, randomPage)

req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
Expand Down

0 comments on commit a084573

Please sign in to comment.