Skip to content

Commit

Permalink
wip request update for a given machine
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme authored and Antoine Grondin committed Jan 14, 2023
1 parent 941c0a3 commit e7ad32d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"strings"

"github.com/aybabtme/rgbterm"
"github.com/blang/semver"
Expand Down Expand Up @@ -150,11 +152,21 @@ func newApp() *cli.App {
var (
ctx context.Context
cancel context.CancelFunc
statefile *StateFile
updateRes <-chan *checkForUpdateRes
)
app.Before = func(c *cli.Context) error {
ctx, cancel = signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
updateRes = checkForUpdate(ctx, &checkForUpdateReq{})
req := &checkForUpdateReq{
arch: runtime.GOARCH,
os: runtime.GOOS,
current: Version,
}
if statefile != nil {
req.accountID = statefile.AccountID
req.machineID = statefile.MachineID
}
updateRes = checkForUpdate(ctx, req)
return nil
}
app.After = func(c *cli.Context) error {
Expand Down Expand Up @@ -287,14 +299,20 @@ type checkForUpdateRes struct {
sha256 string
}

func checkForUpdate(ctx context.Context) <-chan *checkForUpdateRes {
func checkForUpdate(ctx context.Context, req *checkForUpdateReq) <-chan *checkForUpdateRes {
out := make(chan *checkForUpdateRes, 1)
go func() {
defer close(out)
client := &http.Client{}
updateClient := cliupdatev1connect.NewUpdateServiceClient(client, apiURL)
res, err := updateClient.GetNextUpdate(ctx, &connect.Request[cliupdatepb.GetNextUpdateRequest]{
Msg: &cliupdatepb.GetNextUpdateRequest{CurrentVersion: Version},
Msg: &cliupdatepb.GetNextUpdateRequest{
CurrentVersion: Version,
AccountId: req.accountID,
MachineId: req.machineID,
MachineArchitecture: req.arch,
MachineOperatingSystem: req.os,
},
})
if err != nil {
log.Printf("looking for update failed: %v", err)
Expand Down
14 changes: 14 additions & 0 deletions cmd/humanlog/statefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

type StateFile struct {
AccountID string `json:"account_id"`
MachineID string `json:"machine_id"`
}

func readStatefile() (*StateFile, error) {

}

func writeStatefile(sf *StateFile) error {

}

0 comments on commit e7ad32d

Please sign in to comment.