Skip to content

Commit

Permalink
pull api key from user's environment (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
cadensstudio authored Jan 13, 2024
1 parent d4d7e0d commit dae445f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 51 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .env.example

This file was deleted.

40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# 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.
Hermes is a command-line interface (CLI) application built in Go that accelerates the process of downloading web-optimized Google Font files in the WOFF2 format. Hermes takes an opinionated approach by downloading variable font files, if available. Otherwise, Hermes 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.
- **Efficient Font Downloads**: 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.
- **Variable Font Support**: Prioritizes downloading a single variable font file (when available) for efficiency.

- **CSS Integration**: The tool generates CSS code, making it seamless to incorporate the downloaded fonts into your project.
- **CSS Integration**: 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).
Hermes requires a Google Fonts API Key. Obtain your key [here](https://console.cloud.google.com/apis/credentials).

### Installation

Expand All @@ -24,13 +24,31 @@ Hermes requires a Google Fonts API Key to run commands. Obtain your key [here](h
brew tap cadensstudio/tap && brew install hermes
```

#### Download the binary files
#### Download the binary

See releases for binary files.
See [releases](https://github.com/cadensstudio/hermes/releases).

### Usage

Hermes currently provides the following commands
Ensure you set your Google Fonts API key by running `export GFONTS_KEY=<YOUR KEY>`.

Run `hermes --help` to view all available hermes commands:

```bash
Usage:
hermes [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
get Downloads web-optimized font files for a specified font family
help Help about any command
list Lists the 10 most trending Google Fonts

Flags:
-h, --help help for hermes

Use "hermes [command] --help" for more information about a command.
```

## Contributions

Expand All @@ -50,10 +68,10 @@ Contributions to Hermes are welcome! Feel free to open issues, submit pull reque
cd hermes
```

3. Setup your Google Fonts API Key in a `.env` file:
3. Set your Google Fonts API Key:

```bash
cp .env.example .env
export GFONTS_KEY=<YOUR KEY>
```

4. Build Hermes:
Expand All @@ -65,7 +83,7 @@ Contributions to Hermes are welcome! Feel free to open issues, submit pull reque
5. Run Hermes:

```bash
./hermes get roboto
./hermes get inter
```

## License
Expand Down
13 changes: 3 additions & 10 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ 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,15 +101,10 @@ func parseFontFamily(fontFamily string) (parsedFontFamily string) {
}

func getFontUrl(fontFamily string) (fontResponse Font) {
// 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)
}
if key == nil {
fmt.Println(`Error: required variable "GFONTS_KEY" not found. Get a key at: https://console.cloud.google.com/apis/credentials`)
os.Exit(1)
}

url := "https://www.googleapis.com/webfonts/v1/webfonts?key=" + fmt.Sprint(key) + "&family=" + fontFamily + "&capability=WOFF2&capability=VF"
Expand Down
14 changes: 3 additions & 11 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ 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)
}
if key == nil {
fmt.Println(`Error: required variable "GFONTS_KEY" not found. Get a key at: https://console.cloud.google.com/apis/credentials`)
os.Exit(1)
}
url := "https://www.googleapis.com/webfonts/v1/webfonts?key=" + fmt.Sprint(key) + "&sort=trending"

Expand Down Expand Up @@ -83,7 +78,4 @@ 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: 1 addition & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/cobra"
)

var ApiKey string
// var ApiKey string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -24,17 +24,3 @@ func Execute() {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.hermes.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}


4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
)

func main() {
viper.SetConfigFile(".env")
viper.ReadInConfig()

viper.AutomaticEnv()
cmd.Execute()
}

0 comments on commit dae445f

Please sign in to comment.