From a0845730892654bd2fa3cf15217c1783e04cfcb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20=C3=9Cst=C3=BCn?= Date: Tue, 15 Aug 2023 20:22:26 +0300 Subject: [PATCH] Add language selection and help options - 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. --- README.md | 27 +++++++++++++++++++++------ main.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b73bfee..f4da72c 100644 --- a/README.md +++ b/README.md @@ -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 ------------- @@ -21,7 +23,6 @@ Installation 1. Clone this repository: - ```bash git clone https://github.com/omerbustun/git-lucky.git @@ -29,7 +30,6 @@ Installation 2. Navigate to the cloned directory and build the project: - ```bash go build @@ -37,7 +37,7 @@ Installation 3. Run `git-lucky`: - + ```bash ./git-lucky ``` @@ -48,7 +48,7 @@ Configuration 1. Copy the sample configuration file: - + ```bash cp config.sample.json config.json ``` @@ -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 ------------ @@ -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. \ No newline at end of file diff --git a/main.go b/main.go index 76b45e5..bb6bc11 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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 {