Skip to content

Commit

Permalink
fix: add api key flag for commands (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cadensstudio authored Dec 15, 2023
1 parent 9b1f6a6 commit d4d7e0d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
hermes
*.woff2

dist/
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
32 changes: 13 additions & 19 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ type Font struct {
var getCmd = &cobra.Command{
Use: "get <font>",
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()
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
}
16 changes: 6 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d4d7e0d

Please sign in to comment.