Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
ci: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jlec committed Jan 8, 2024
1 parent 2e85afb commit 0a6165d
Show file tree
Hide file tree
Showing 21 changed files with 318 additions and 124 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
)

require (
Expand Down Expand Up @@ -54,10 +54,10 @@ require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/grpc v1.60.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 h1:EDuYyU/MkFXllv9QF9819VlI9a4tzGuCbhG0ExK9o1U=
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -175,8 +175,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -196,8 +196,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand All @@ -224,8 +224,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 h1:gphdwh0npgs8elJ4T6J+DQJHPVF7RsuJHCfwztUb4J4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1/go.mod h1:daQN87bsDqDoe316QbbvX60nMoJQa4r6Ds0ZuoAe5yA=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
Expand Down
39 changes: 25 additions & 14 deletions internal/api/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package turingpi

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -12,14 +13,14 @@ import (
const TimeOut int = 10

type Client struct {
ApiURI string
APIURI string
HTTPClient *http.Client
}

func NewClient(endpoint string) (*Client, error) {
c := Client{
HTTPClient: &http.Client{Timeout: time.Duration(TimeOut) * time.Second},
ApiURI: fmt.Sprintf("http://%s/api/bmc", endpoint),
APIURI: fmt.Sprintf("http://%s/api/bmc", endpoint),
}

return &c, nil
Expand All @@ -28,45 +29,55 @@ func NewClient(endpoint string) (*Client, error) {
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
return nil, fmt.Errorf("%w: '%d', body: %s", ErrInvalidStatus, res.StatusCode, body)
}

return body, err
return body, fmt.Errorf("failure: %w", err)
}

func (c *Client) Get(params string) ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?%s", c.ApiURI, params), nil)
func (c *Client) Get(ctx context.Context, params string) ([]byte, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s?%s", c.APIURI, params),
nil,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}

return body, nil
}

func (c *Client) Set(params string) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s?%s", c.ApiURI, params), nil)
func (c *Client) Set(ctx context.Context, params string) ([]byte, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s?%s", c.APIURI, params),
nil,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}

body, err := c.doRequest(req)
if err != nil {
return nil, err
return nil, fmt.Errorf("failure: %w", err)
}

return body, nil
Expand Down
7 changes: 7 additions & 0 deletions internal/api/error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package turingpi

import "errors"

// ErrInvalidMode Define a static error.
var ErrInvalidMode = errors.New("invalid mode")

var ErrInvalidStatus = errors.New("invalid status")

type ResultError struct {
Reason string
}
Expand Down
16 changes: 11 additions & 5 deletions internal/api/nodeinfo.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package turingpi

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// GetNodeInfo - Returns Turing Pi BMC SD card status.
func (c *Client) GetNodeInfo() (NodeInfo, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?opt=get&type=nodeinfo", c.ApiURI), nil)
func (c *Client) GetNodeInfo(ctx context.Context) (NodeInfo, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s?opt=get&type=nodeinfo", c.APIURI),
nil,
)
if err != nil {
return NodeInfo{}, err
return NodeInfo{}, fmt.Errorf("failed to create request: %w", err)
}

body, err := c.doRequest(req)
if err != nil {
return NodeInfo{}, err
return NodeInfo{}, fmt.Errorf("failed to call API: %w", err)
}

sdCardResponse := NodeInfoResponse{}
err = json.Unmarshal(body, &sdCardResponse)

if err != nil {
return NodeInfo{}, err
return NodeInfo{}, fmt.Errorf("failed to unmarshal json: %w", err)
}

return sdCardResponse.Response[0], nil
Expand Down
16 changes: 11 additions & 5 deletions internal/api/power.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package turingpi

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// GetPower - Returns Turing Pi BMC SD card status.
func (c *Client) GetPower() (Power, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?opt=get&type=power", c.ApiURI), nil)
func (c *Client) GetPower(ctx context.Context) (Power, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s?opt=get&type=power", c.APIURI),
nil,
)
if err != nil {
return Power{}, err
return Power{}, fmt.Errorf("failed to create request: %w", err)
}

body, err := c.doRequest(req)
if err != nil {
return Power{}, err
return Power{}, fmt.Errorf("failed to call API: %w", err)
}

sdCardResponse := PowerResponse{}
err = json.Unmarshal(body, &sdCardResponse)

if err != nil {
return Power{}, err
return Power{}, fmt.Errorf("failed to unmarshal json: %w", err)
}

return sdCardResponse.Response[0], nil
Expand Down
16 changes: 11 additions & 5 deletions internal/api/sdcard.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package turingpi

import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// GetSDCard - Returns Turing Pi BMC SD card status.
func (c *Client) GetSDCard() (SDCard, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s?opt=get&type=sdcard", c.ApiURI), nil)
func (c *Client) GetSDCard(ctx context.Context) (SDCard, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s?opt=get&type=sdcard", c.APIURI),
nil,
)
if err != nil {
return SDCard{}, err
return SDCard{}, fmt.Errorf("failed to create request: %w", err)
}

body, err := c.doRequest(req)
if err != nil {
return SDCard{}, err
return SDCard{}, fmt.Errorf("failed to call API: %w", err)
}

sdCardResponse := SDCardResponse{}
err = json.Unmarshal(body, &sdCardResponse)

if err != nil {
return SDCard{}, err
return SDCard{}, fmt.Errorf("failed to unmarshal json: %w", err)
}

return sdCardResponse.Response[0], nil
Expand Down
23 changes: 12 additions & 11 deletions internal/api/usb.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package turingpi

import (
"context"
"encoding/json"
"fmt"
)

// GetUsb - Returns Turing Pi BMC SD card status.
func (c *Client) GetUsb() (Usb, error) {
func (c *Client) GetUsb(ctx context.Context) (Usb, error) {
response := UsbResponse{}

body, err := c.Get("opt=get&type=usb")
body, err := c.Get(ctx, "opt=get&type=usb")
if err != nil {
return Usb{}, err
}

err = json.Unmarshal(body, &response)
if err != nil {
return Usb{}, err
return Usb{}, fmt.Errorf("failed to unmarshal response: %w", err)
}

return response.Response[0], nil
}

// SetUsb - Returns Turing Pi BMC SD card status.
func (c *Client) SetUsb(mode, node int64) (Result, error) {
func (c *Client) SetUsb(ctx context.Context, mode, node int64) (Result, error) {
response := ResultResponse{}

body, err := c.Set(fmt.Sprintf("opt=set&type=usb&mode=%d&node=%d", mode, node))
body, err := c.Set(ctx, fmt.Sprintf("opt=set&type=usb&mode=%d&node=%d", mode, node))
if err != nil {
return Result{}, err
return Result{}, fmt.Errorf("failed to set mode: %w", err)
}

err = json.Unmarshal(body, &response)
if err != nil {
return Result{}, err
return Result{}, fmt.Errorf("failed to unmarshal response: %w", err)
}

if response.Response[0].Result != "ok" {
Expand All @@ -43,24 +44,24 @@ func (c *Client) SetUsb(mode, node int64) (Result, error) {
return response.Response[0], nil
}

func ApiToMode(mode int64) (string, error) {
func APIToMode(mode int64) (string, error) {
switch mode {
case 0:
return "host", nil
case 1:
return "device", nil
default:
return "", fmt.Errorf("'%d' not a valid mode", mode)
return "", fmt.Errorf("%w: '%d'", ErrInvalidMode, mode)
}
}

func ModeToApi(mode string) (int64, error) {
func ModeToAPI(mode string) (int64, error) {
switch mode {
case "host":
return 0, nil
case "device":
return 1, nil
default:
return -1, fmt.Errorf("'%s' not a valid mode", mode)
return -1, fmt.Errorf("%w: '%s'", ErrInvalidMode, mode)
}
}
Loading

0 comments on commit 0a6165d

Please sign in to comment.