From d4d7e0dbc70f9a761d1212d53856f67156698746 Mon Sep 17 00:00:00 2001 From: cadensstudio <54109914+cadensstudio@users.noreply.github.com> Date: Fri, 15 Dec 2023 07:09:38 -0800 Subject: [PATCH] fix: add api key flag for commands (#2) * update command descriptions * make api key a required flag * read key from .env or cmd flag * remove getApiKey func * add readme * update readme * update readme --- .gitignore | 1 + README.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/get.go | 32 ++++++++++------------- cmd/list.go | 12 +++++++++ cmd/root.go | 16 +++++------- 5 files changed, 105 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 0816081..dfb018e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env hermes +*.woff2 dist/ diff --git a/README.md b/README.md index e69de29..b3eeb63 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,73 @@ +# Hermes - Google Fonts Downloader + +Hermes is a command-line interface (CLI) application built in Go that simplifies the process of downloading web-optimized Google Font files in the WOFF2 format. Hermes takes an opinionated approach, aiming to download the single variable font file if available; otherwise, it downloads each individual font weight file separately. Additionally, Hermes generates the necessary CSS code to easily integrate the downloaded fonts into your project. + +## Features + +- **Efficient Font Downloads**: Hermes optimizes the download process by retrieving only the necessary font files in WOFF2 format. + +- **Variable Font Support**: When available, Hermes prioritizes the download of a single variable font file for efficiency. + +- **CSS Integration**: The tool generates CSS code, making it seamless to incorporate the downloaded fonts into your project. + +## Getting Started + +### Prerequisites + +Hermes requires a Google Fonts API Key to run commands. Obtain your key [here](https://console.cloud.google.com/apis/credentials). + +### Installation + +#### Install using Homebrew + +```bash +brew tap cadensstudio/tap && brew install hermes +``` + +#### Download the binary files + +See releases for binary files. + +### Usage + +Hermes currently provides the following commands + +## Contributions + +Contributions to Hermes are welcome! Feel free to open issues, submit pull requests, or provide feedback to improve the tool. + +### Local Dev Setup + +1. Clone the repository: + + ```bash + git clone https://github.com/cadensstudio/hermes.git + ``` + +2. Navigate to the project directory: + + ```bash + cd hermes + ``` + +3. Setup your Google Fonts API Key in a `.env` file: + + ```bash + cp .env.example .env + ``` + +4. Build Hermes: + + ```bash + go build + ``` + +5. Run Hermes: + + ```bash + ./hermes get roboto + ``` + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. diff --git a/cmd/get.go b/cmd/get.go index 1844470..ab871db 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -40,9 +40,7 @@ type Font struct { var getCmd = &cobra.Command{ Use: "get ", Short: "Downloads web-optimized font files for a specified font family", - Long: `Download the specified font family in WOFF2 format. -If a single variable format is available, it will be downloaded; -otherwise, each individual font weight file will be downloaded.`, + Long: `Download the specified font family in WOFF2 format.`, Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { cmd.Help() @@ -62,6 +60,8 @@ func init() { getCmd.PersistentFlags().StringVarP(&Dir, "dir", "d", "", "Directory to write font files to (defaults to current directory)") viper.BindPFlag("dir", getCmd.PersistentFlags().Lookup("dir")) + getCmd.PersistentFlags().StringVarP(&ApiKey, "key", "k", "", "Your Google Fonts API Key (https://console.cloud.google.com/apis/credentials)") + viper.BindPFlag("key", getCmd.PersistentFlags().Lookup("key")) // Validate the dir flag cobra.OnInitialize(validateDir) } @@ -103,17 +103,18 @@ func parseFontFamily(fontFamily string) (parsedFontFamily string) { } func getFontUrl(fontFamily string) (fontResponse Font) { - // try reading key from .env + // try grabbing key from .env file, if it exists key := viper.Get("GFONTS_KEY") - if key == nil { - // try reading key from os environment - key = os.Getenv("GFONTS_KEY") - // get key using user prompt - key = getApiKey() + if key == nil { + // if no .env, grab key from cmd flag + key = viper.GetString("key") + if len(fmt.Sprint(key)) < 1 { + fmt.Println(`Error: required flag "key" not set`) + os.Exit(1) + } } url := "https://www.googleapis.com/webfonts/v1/webfonts?key=" + fmt.Sprint(key) + "&family=" + fontFamily + "&capability=WOFF2&capability=VF" - // Make the GET request res, err := http.Get(url) if err != nil { @@ -140,11 +141,11 @@ func getFontUrl(fontFamily string) (fontResponse Font) { } return fontResponse } else if res.StatusCode == 400 { - fmt.Println("Error: Invalid API Key") + fmt.Println("Error: invalid API Key") os.Exit(1) return } else if res.StatusCode == 500 { - fmt.Println("Error: Could not find specified font:", fontFamily) + fmt.Println("Error: could not find specified font:", fontFamily) os.Exit(1) return } else { @@ -256,11 +257,4 @@ func printCssConfig(fontResponse Font, hasVariable bool) { fmt.Println(newCssString) } } -} - -func getApiKey() (apiKey string) { - fmt.Println("Please enter your Google Fonts API Key (found here: https://console.cloud.google.com/apis/credentials): ") - fmt.Scanf("%s", &apiKey) - - return apiKey } \ No newline at end of file diff --git a/cmd/list.go b/cmd/list.go index e0fd200..88cb0c3 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -23,7 +23,16 @@ var listCmd = &cobra.Command{ Long: `Lists the 10 most trending Google Fonts, providing inspiration for your next project.`, Run: func(cmd *cobra.Command, args []string) { + // try grabbing key from .env file, if it exists key := viper.Get("GFONTS_KEY") + if key == nil { + // if no .env, grab key from cmd flag + key = viper.GetString("key") + if len(fmt.Sprint(key)) < 1 { + fmt.Println(`Error: required flag "key" not set`) + os.Exit(1) + } + } url := "https://www.googleapis.com/webfonts/v1/webfonts?key=" + fmt.Sprint(key) + "&sort=trending" // Make the GET request @@ -74,4 +83,7 @@ providing inspiration for your next project.`, func init() { rootCmd.AddCommand(listCmd) + + listCmd.PersistentFlags().StringVarP(&ApiKey, "key", "k", "", "Your Google Fonts API Key (https://console.cloud.google.com/apis/credentials)") + viper.BindPFlag("key", listCmd.PersistentFlags().Lookup("key")) } diff --git a/cmd/root.go b/cmd/root.go index 0128765..34814bc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,19 +5,15 @@ import ( "github.com/spf13/cobra" ) +var ApiKey string + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "hermes", - Short: "Hermes simplifies font downloading from Google Fonts.", - Long: ` -Tailored for developers and designers, Hermes automates the process -of downloading web-optimized font files from Google Fonts. - -Key Features: -- Effortlessly download fonts in the highly efficient WOFF2 format, ideal for web use. -- Automatic retrieval of variable font formats when available, ensuring flexibility in design. -- Empower your web projects with the 'list' command, revealing the top 10 trending Google Fonts. -- Expedites font acquisition, allowing developers to self-host fonts and significantly boost website load speeds.`, + Short: "Hermes simplifies the process of downloading Google Font files.", + Long: `Hermes simplifies the process of downloading web-optimized Google Font files +in the WOFF2 format and generates the necessary CSS code to easily integrate +the downloaded fonts into your project.`, } // Execute adds all child commands to the root command and sets flags appropriately.