Skip to content

Commit

Permalink
chore: rename keymap package
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Jan 16, 2023
1 parent dbba132 commit a8f9dce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
87 changes: 34 additions & 53 deletions pkg/keyboard/keyboard.go → pkg/infojson/infojson.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package keyboard
/*
* infojson - Fetches and parses the info.json file from the QMK API or a local file.
*
* See https://docs.qmk.fm/#/reference_info_json for more information.
*/

package infojson

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/rs/zerolog/log"
)

type Key struct {
X float64 `json:"x"`
Y float64 `json:"y"`
W *float64 `json:"w"`
H *float64 `json:"h"`
Label string `json:"label"`
X float64 `json:"x"`
Y float64 `json:"y"`
W *float64 `json:"w"`
H *float64 `json:"h"`
}

type Layout struct {
Expand All @@ -35,45 +39,21 @@ type file struct {
Keyboards map[string]Keyboard `json:"keyboards"`
}

func fetch(url string) (*file, error) {
log.Info().Msg("Fetching keyboard layout.")
log.Debug().Str("url", url).Send()

client := http.Client{
Timeout: time.Second * 5, // Timeout after 2 seconds
}

req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
return nil, err
}

req.Header.Set("User-Agent", "zmk-layout-viewer")

res, getErr := client.Do(req)
if getErr != nil {
return nil, getErr
}

if res.Body != nil {
defer res.Body.Close()
}

body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
func FromName(name string) (Layouts, error) {
log.Debug().Str("name", name).Send()
url := "https://keyboards.qmk.fm/v1/keyboards/%v/info.json"

f := file{}
err = json.Unmarshal(body, &f)
f, err := fetch(fmt.Sprintf(url, name))
if err != nil {
return nil, err
}

return &f, nil
l := f.Keyboards[name].Layouts
return l, nil
}

func loadFile(path string) (*Keyboard, error) {
func FromFile(name, path string) (Layouts, error) {
log.Debug().Str("name", name).Str("path", path).Send()
data, err := os.ReadFile(path)
if err != nil {
return nil, err
Expand All @@ -83,29 +63,30 @@ func loadFile(path string) (*Keyboard, error) {
if err != nil {
return nil, err
}
return &f, nil
return f.Layouts, nil
}

func Fetch(name string) (Layouts, error) {
log.Debug().Str("name", name).Send()
url := "https://keyboards.qmk.fm/v1/keyboards/%v/info.json"
func fetch(url string) (*file, error) {
log.Info().Msg("Fetching keyboard layout.")
log.Debug().Str("url", url).Send()

resp, err := http.Get(url)

f, err := fetch(fmt.Sprintf(url, name))
if err != nil {
return nil, err
}
defer resp.Body.Close()

l := f.Keyboards[name].Layouts
return l, nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

func LoadFile(name, path string) (Layouts, error) {
log.Debug().Str("name", name).Send()
log.Debug().Str("path", path).Send()
f, err := loadFile(path)
f := file{}
err = json.Unmarshal(body, &f)
if err != nil {
return nil, err
}
l := f.Layouts
return l, nil

return &f, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package keyboard
package infojson

import (
"net/http"
Expand Down

0 comments on commit a8f9dce

Please sign in to comment.