-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: confirm tokens are stored after configuration setup is complete (…
- Loading branch information
Showing
29 changed files
with
933 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Built binaries | ||
etime | ||
/etime | ||
dist/* | ||
|
||
# Development leftovers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package etime | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/zimeg/emporia-time/pkg/config" | ||
"github.com/zimeg/emporia-time/pkg/energy" | ||
"github.com/zimeg/emporia-time/pkg/times" | ||
) | ||
|
||
// CommandResult holds information from the run command | ||
type CommandResult struct { | ||
energy.EnergyResult | ||
times.TimeMeasurement | ||
ExitCode int | ||
} | ||
|
||
// Run executes the command and returns the usage statistics | ||
func Run(cmd []string, cfg config.Configure) (results CommandResult, err error) { | ||
available, err := cfg.API().Status() | ||
if err != nil { | ||
return CommandResult{}, err | ||
} else if !available { | ||
return CommandResult{}, fmt.Errorf("Error: Cannot measure energy during Emporia maintenance") | ||
} | ||
measurements, err := times.TimeExec(cmd) | ||
if err != nil { | ||
if exitError, ok := err.(*exec.ExitError); ok { | ||
results.ExitCode = exitError.ExitCode() | ||
} else { | ||
return CommandResult{}, err | ||
} | ||
results.TimeMeasurement = measurements | ||
} else { | ||
results.TimeMeasurement = measurements | ||
} | ||
usage, err := cfg.API().GetChartUsage(results.TimeMeasurement) | ||
if err != nil { | ||
return results, err | ||
} else { | ||
results.EnergyResult = usage | ||
} | ||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/zimeg/emporia-time/cmd/etime" | ||
"github.com/zimeg/emporia-time/internal/display/templates" | ||
"github.com/zimeg/emporia-time/pkg/api" | ||
"github.com/zimeg/emporia-time/pkg/cognito" | ||
"github.com/zimeg/emporia-time/pkg/config" | ||
) | ||
|
||
// Root facilitates the setup and execution of the command | ||
func Root( | ||
ctx context.Context, | ||
cog cognito.Cognitoir, | ||
fs afero.Fs, | ||
req api.Emporiac, | ||
args []string, | ||
version string, | ||
) ( | ||
etime.CommandResult, | ||
error, | ||
) { | ||
cmd, flags, err := config.ParseFlags(args) | ||
if err != nil { | ||
return etime.CommandResult{}, err | ||
} else if flags.Version { | ||
fmt.Printf("%s\n", version) | ||
return etime.CommandResult{}, nil | ||
} else if flags.Help { | ||
templates.PrintHelpMessage() | ||
return etime.CommandResult{}, nil | ||
} | ||
cfg, err := config.Load(ctx, cog, fs, req, flags) | ||
if err != nil { | ||
return etime.CommandResult{}, err | ||
} | ||
results, err := etime.Run(cmd, &cfg) | ||
if err != nil { | ||
return etime.CommandResult{}, err | ||
} | ||
stats, err := templates.FormatUsage(results, flags.Portable) | ||
if err != nil { | ||
return results, err | ||
} else { | ||
fmt.Fprintf(os.Stderr, "%s\n", stats) | ||
} | ||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"log" | ||
"os" | ||
|
||
etime "github.com/zimeg/emporia-time/cmd" | ||
"github.com/zimeg/emporia-time/internal/display/templates" | ||
"github.com/zimeg/emporia-time/pkg/emporia" | ||
"github.com/spf13/afero" | ||
"github.com/zimeg/emporia-time/cmd" | ||
"github.com/zimeg/emporia-time/pkg/api" | ||
"github.com/zimeg/emporia-time/pkg/cognito" | ||
) | ||
|
||
// version is the title of this current build | ||
var version = "development" | ||
|
||
const ( | ||
clientID string = "4qte47jbstod8apnfic0bunmrq" // Emporia AWS Cognito client ID | ||
region string = "us-east-2" // Emporia AWS region | ||
) | ||
|
||
// main manages the lifecycle of this program | ||
func main() { | ||
command, client, err := etime.Setup(os.Args) | ||
ctx := context.Background() | ||
fs := afero.NewOsFs() | ||
req := api.New() | ||
cog, err := cognito.NewClient(ctx, clientID, region) | ||
if err != nil { | ||
log.Fatalf("Error: %s", err) | ||
} else if command.Flags.Version { | ||
fmt.Printf("%s\n", version) | ||
os.Exit(0) | ||
} else if command.Flags.Help { | ||
templates.PrintHelpMessage() | ||
os.Exit(0) | ||
} | ||
if available, err := emporia.EmporiaStatus(); err != nil { | ||
log.Fatalf("Error: %s", err) | ||
} else if !available { | ||
log.Fatalf("Error: Cannot measure energy during Emporia maintenance\n") | ||
} | ||
results, err := etime.Run(command, client) | ||
result, err := cmd.Root(ctx, cog, fs, req, os.Args, version) | ||
if err != nil { | ||
log.Fatalf("Error: %s", err) | ||
} | ||
if stats, err := templates.FormatUsage(results, command.Flags.Portable); err != nil { | ||
log.Fatalf("Error: %s", err) | ||
} else { | ||
fmt.Fprintf(os.Stderr, "%s\n", stats) | ||
} | ||
os.Exit(results.ExitCode) | ||
os.Exit(result.ExitCode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/zimeg/emporia-time/pkg/energy" | ||
"github.com/zimeg/emporia-time/pkg/times" | ||
) | ||
|
||
// Emporiac communicates with the Emporia API | ||
type Emporiac interface { | ||
GetChartUsage(times times.TimeMeasurement) (results energy.EnergyResult, err error) | ||
GetCustomerDevices() (devices []Device, err error) | ||
Status() (available bool, err error) | ||
|
||
SetDevice(deviceID string) | ||
SetToken(token string) | ||
} | ||
|
||
// Emporia holds information for and from the Emporia API | ||
type Emporia struct { | ||
client interface { | ||
// Do does the HTTP request and is often implmented using net/http | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
deviceID string | ||
token string | ||
} | ||
|
||
// RequestURL is the base URL of the API | ||
const RequestURL string = "https://api.emporiaenergy.com" | ||
|
||
// New creates a new client to interact with Emporia HTTP APIs | ||
func New() *Emporia { | ||
return &Emporia{ | ||
client: &http.Client{}, | ||
} | ||
} | ||
|
||
// SetToken sets the token for the client | ||
func (emp *Emporia) SetToken(token string) { | ||
emp.token = token | ||
} | ||
|
||
// SetDevice sets the device ID for the client | ||
func (emp *Emporia) SetDevice(deviceID string) { | ||
emp.deviceID = deviceID | ||
} | ||
|
||
// get makes an authenticated GET request to URL and saves the response to data | ||
func (emp *Emporia) get(url string, data any) error { | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
if emp.token != "" { | ||
req.Header.Add("authToken", emp.token) | ||
} | ||
resp, err := emp.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return json.Unmarshal(body, &data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type EmporiaMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (em *EmporiaMock) SetDevice(deviceID string) { | ||
em.Called(deviceID) | ||
} | ||
|
||
func (em *EmporiaMock) SetToken(token string) { | ||
em.Called(token) | ||
} |
Oops, something went wrong.