Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): create endpoint to list hardwares #49

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ docker:
--build-arg TARGET=$(TARGET) \
--build-arg VERSION=$(VERSION) \
-f build/package/Dockerfile .

# Make sure you have `inotify-tools` installed.
watch:
while true; do \
$(MAKE) build; \
# TODO: Run server and restart it on changes.
inotifywait -qre close_write .; \
done
20 changes: 20 additions & 0 deletions cmd/metal/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/google/go-github/v56/github"
)

var (
client *github.Client
)

// NewGitHubClient returns a pointer to a configured GitHub client.
func NewGitHubClient() *github.Client {
if client != nil {
return client
}

client = github.NewClient(nil)

return client
}
58 changes: 54 additions & 4 deletions cmd/metal/hardwares.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"os"

Expand All @@ -11,24 +12,35 @@ import (
)

const (
repository = "nicklasfrahm/infrastructure"
fileURLTemplate = "https://raw.githubusercontent.com/%s/main/%s"
repoOwner = "nicklasfrahm"
repoName = "infrastructure"
hardwareConfigPath = "configs/hardwares"
fileURLTemplate = "https://raw.githubusercontent.com/%s/%s/main/%s"
)

// Hardware returns a fiber app for the hardware resource.
func Hardware(useRemote bool) *fiber.App {
app := fiber.New()

app.Get("/", func(c *fiber.Ctx) error {
hardwares, err := ListHardwares()
if err != nil {
return err
}

return c.Status(200).JSON(hardwares)
})

app.Get("/:name<alpha>", func(c *fiber.Ctx) error {
name := c.Params("name")

var bytes []byte
var err error

path := fmt.Sprintf("configs/hardwares/%s.yaml", name)
path := fmt.Sprintf("%s/%s.yaml", hardwareConfigPath, name)
if useRemote {
// Use the GitHub API to fetch the file.
path = fmt.Sprintf(fileURLTemplate, repository, path)
path = fmt.Sprintf(fileURLTemplate, repoOwner, repoName, path)
status, body, errs := fiber.Get(path).Bytes()
if len(errs) > 0 {
return errs[0]
Expand All @@ -48,6 +60,7 @@ func Hardware(useRemote bool) *fiber.App {
}
}

// Parse the YAML file into a hardware object.
hardware := &apiv1.Hardware{}
if err := yaml.Unmarshal(bytes, hardware); err != nil {
return err
Expand All @@ -58,3 +71,40 @@ func Hardware(useRemote bool) *fiber.App {

return app
}

// ListHardwares returns a list of all available hardwares.
func ListHardwares() ([]*apiv1.Hardware, error) {
client := NewGitHubClient()

_, dir, _, err := client.Repositories.GetContents(context.TODO(), repoOwner, repoName, hardwareConfigPath, nil)
if err != nil {
return nil, err
}

hardwares := []*apiv1.Hardware{}
for _, entry := range dir {
file, _, _, err := client.Repositories.GetContents(context.TODO(), repoOwner, repoName, entry.GetPath(), nil)
if err != nil {
return nil, err
}

content, err := file.GetContent()
if err != nil {
return nil, err
}

hardware := &apiv1.Hardware{}
if err := yaml.Unmarshal([]byte(content), hardware); err != nil {
return nil, err
}

hardwares = append(hardwares, hardware)
}

return hardwares, nil
}

// ReadHardware returns a hardware by name.
func ReadHardware(name string) (*apiv1.Hardware, error) {
return nil, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cloudflare/cloudflare-go v0.79.0
github.com/go-playground/validator/v10 v10.15.5
github.com/gofiber/fiber/v2 v2.49.2
github.com/google/go-github/v56 v56.0.0
github.com/joho/godotenv v1.5.1
github.com/pulumi/pulumi-cloudflare/sdk/v5 v5.13.0
github.com/pulumi/pulumi/sdk/v3 v3.89.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4=
github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down