Skip to content

Commit

Permalink
feat: new structure with commented out errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed May 17, 2023
1 parent dad9131 commit 2eb1bec
Show file tree
Hide file tree
Showing 12 changed files with 537 additions and 508 deletions.
75 changes: 37 additions & 38 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package minercraft

import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"time"

Expand All @@ -28,41 +25,41 @@ type Client struct {

// AddMiner will add a new miner to the list of miners
func (c *Client) AddMiner(miner Miner) error {

// TODO: Align with new structure
// Make sure we have the basic requirements
if len(miner.Name) == 0 {
return errors.New("missing miner name")
} else if len(miner.URL) == 0 {
return errors.New("missing miner url")
}

// Check if a miner with that name already exists
existingMiner := c.MinerByName(miner.Name)
if existingMiner != nil {
return fmt.Errorf("miner %s already exists", miner.Name)
}

// Check if a miner with the minerID already exists
if len(miner.MinerID) > 0 {
if existingMiner = c.MinerByID(miner.MinerID); existingMiner != nil {
return fmt.Errorf("miner %s already exists", miner.MinerID)
}
}

// Ensure that we have a protocol
if !strings.Contains(miner.URL, "http") {
return fmt.Errorf("miner %s is missing http from url", miner.Name)
}

// Test parsing the url
parsedURL, err := url.Parse(miner.URL)
if err != nil {
return err
}
miner.URL = parsedURL.String()

// Append the new miner
c.miners = append(c.miners, &miner)
// if len(miner.Name) == 0 {
// return errors.New("missing miner name")
// } else if len(miner.URL) == 0 {
// return errors.New("missing miner url")
// }

// // Check if a miner with that name already exists
// existingMiner := c.MinerByName(miner.Name)
// if existingMiner != nil {
// return fmt.Errorf("miner %s already exists", miner.Name)
// }

// // Check if a miner with the minerID already exists
// if len(miner.MinerID) > 0 {
// if existingMiner = c.MinerByID(miner.MinerID); existingMiner != nil {
// return fmt.Errorf("miner %s already exists", miner.MinerID)
// }
// }

// // Ensure that we have a protocol
// if !strings.Contains(miner.URL, "http") {
// return fmt.Errorf("miner %s is missing http from url", miner.Name)
// }

// // Test parsing the url
// parsedURL, err := url.Parse(miner.URL)
// if err != nil {
// return err
// }
// // miner.URL = parsedURL.String()

// // Append the new miner
// c.miners = append(c.miners, &miner)
return nil
}

Expand Down Expand Up @@ -112,7 +109,8 @@ func MinerByID(miners []*Miner, minerID string) *Miner {
// MinerUpdateToken will find a miner by name and update the token
func (c *Client) MinerUpdateToken(name, token string) {
if miner := c.MinerByName(name); miner != nil {
miner.Token = token
// TODO: Align with new structure
// miner.Token = token
}
}

Expand Down Expand Up @@ -190,6 +188,7 @@ func createClient(options *ClientOptions, customHTTPClient HTTPInterface, custom
c.Options = options

// Load custom vs pre-defined
// TODO: Maybe try to pass mode here in createClient
if len(customMiners) > 0 {
c.miners = customMiners
} else {
Expand Down
Loading

0 comments on commit 2eb1bec

Please sign in to comment.