Update check cleanup, gofmt and gocyclo = 3

This commit is contained in:
jamesread 2021-06-22 06:00:24 +01:00
parent 4bd2be6317
commit a4def7571c
6 changed files with 57 additions and 41 deletions

View File

@ -15,7 +15,7 @@ daemon-codestyle:
go fmt ./... go fmt ./...
go vet ./... go vet ./...
golint ./... golint ./...
gocyclo -over 4 cmd internal gocyclo -over 3 cmd internal
daemon-unittests: daemon-unittests:
mkdir -p reports mkdir -p reports

View File

@ -60,7 +60,7 @@ func main() {
log.Debugf("Config: %+v", cfg) log.Debugf("Config: %+v", cfg)
go updatecheck.CheckForUpdate(version, commit, cfg) go updatecheck.StartUpdateChecker(version, commit, cfg)
go grpcapi.Start(cfg) go grpcapi.Start(cfg)

View File

@ -4,7 +4,7 @@ import ()
// ActionButton represents a button that is shown in the webui. // ActionButton represents a button that is shown in the webui.
type ActionButton struct { type ActionButton struct {
Id string ID string
Title string Title string
Icon string Icon string
Shell string Shell string
@ -32,6 +32,7 @@ type Config struct {
LogLevel string LogLevel string
ActionButtons []ActionButton `mapstructure:"actions"` ActionButtons []ActionButton `mapstructure:"actions"`
Entities []Entity `mapstructure:"omitempty"` Entities []Entity `mapstructure:"omitempty"`
CheckForUpdates bool
} }
// DefaultConfig gets a new Config structure with sensible default values. // DefaultConfig gets a new Config structure with sensible default values.
@ -43,6 +44,7 @@ func DefaultConfig() *Config {
config.ListenAddressGrpcActions = "localhost:1339" config.ListenAddressGrpcActions = "localhost:1339"
config.ListenAddressWebUI = "localhost:1340" config.ListenAddressWebUI = "localhost:1340"
config.LogLevel = "INFO" config.LogLevel = "INFO"
config.CheckForUpdates = true
return &config return &config
} }

View File

@ -13,9 +13,6 @@ import (
// ExecAction executes an action. // ExecAction executes an action.
func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse { func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
res := &pb.StartActionResponse{}
res.TimedOut = false
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"actionName": action, "actionName": action,
}).Infof("StartAction") }).Infof("StartAction")
@ -24,9 +21,19 @@ func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
if err != nil { if err != nil {
log.Errorf("Error finding action %s, %s", err, action) log.Errorf("Error finding action %s, %s", err, action)
return res
return &pb.StartActionResponse{
TimedOut: false,
}
} }
return execAction(cfg, actualAction)
}
func execAction(cfg *config.Config, actualAction *config.ActionButton) *pb.StartActionResponse {
res := &pb.StartActionResponse{}
res.TimedOut = false
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"title": actualAction.Title, "title": actualAction.Title,
"timeout": actualAction.Timeout, "timeout": actualAction.Timeout,

View File

@ -2,12 +2,12 @@ package grpcapi
import ( import (
ctx "context" ctx "context"
"crypto/md5"
"fmt"
pb "github.com/jamesread/OliveTin/gen/grpc" pb "github.com/jamesread/OliveTin/gen/grpc"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"google.golang.org/grpc" "google.golang.org/grpc"
"net" "net"
"crypto/md5"
"fmt"
config "github.com/jamesread/OliveTin/internal/config" config "github.com/jamesread/OliveTin/internal/config"
executor "github.com/jamesread/OliveTin/internal/executor" executor "github.com/jamesread/OliveTin/internal/executor"
@ -30,9 +30,9 @@ func (api *oliveTinAPI) GetButtons(ctx ctx.Context, req *pb.GetButtonsRequest) (
for _, action := range cfg.ActionButtons { for _, action := range cfg.ActionButtons {
btn := pb.ActionButton{ btn := pb.ActionButton{
Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))), Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Title: action.Title, Title: action.Title,
Icon: lookupHTMLIcon(action.Icon), Icon: lookupHTMLIcon(action.Icon),
} }
res.Actions = append(res.Actions, &btn) res.Actions = append(res.Actions, &btn)

View File

@ -1,27 +1,27 @@
package updatecheck package updatecheck
import ( import (
config "github.com/jamesread/OliveTin/internal/config" "bytes"
log "github.com/sirupsen/logrus" "encoding/json"
"github.com/denisbrodbeck/machineid" "github.com/denisbrodbeck/machineid"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"runtime" config "github.com/jamesread/OliveTin/internal/config"
"net/http" log "github.com/sirupsen/logrus"
"encoding/json"
"bytes"
"time"
"io/ioutil" "io/ioutil"
"net/http"
"runtime"
"time"
) )
type UpdateRequest struct { type updateRequest struct {
CurrentVersion string CurrentVersion string
CurrentCommit string CurrentCommit string
OS string OS string
Arch string Arch string
MachineId string MachineID string
} }
func machineId() string { func machineID() string {
v, err := machineid.ProtectedID("OliveTin") v, err := machineid.ProtectedID("OliveTin")
if err != nil { if err != nil {
@ -29,16 +29,23 @@ func machineId() string {
return "?" return "?"
} }
return v; return v
} }
func CheckForUpdate(currentVersion string, currentCommit string, cfg *config.Config) { // StartUpdateChecker will start a job that runs periodically, checking
payload := UpdateRequest { // for updates.
func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config) {
if !cfg.CheckForUpdates {
log.Warn("Update checking is disabled")
return
}
payload := updateRequest{
CurrentVersion: currentVersion, CurrentVersion: currentVersion,
CurrentCommit: currentCommit, CurrentCommit: currentCommit,
OS: runtime.GOOS, OS: runtime.GOOS,
Arch: runtime.GOARCH, Arch: runtime.GOARCH,
MachineId: machineId(), MachineID: machineID(),
} }
s := gocron.NewScheduler(time.UTC) s := gocron.NewScheduler(time.UTC)
@ -50,7 +57,7 @@ func CheckForUpdate(currentVersion string, currentCommit string, cfg *config.Con
s.StartAsync() s.StartAsync()
} }
func actualCheckForUpdate(payload UpdateRequest) { func actualCheckForUpdate(payload updateRequest) {
jsonUpdateRequest, err := json.Marshal(payload) jsonUpdateRequest, err := json.Marshal(payload)
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewReader(jsonUpdateRequest)) req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewReader(jsonUpdateRequest))
@ -66,14 +73,14 @@ func actualCheckForUpdate(payload UpdateRequest) {
if err != nil { if err != nil {
log.Errorf("Update check failed %v", err) log.Errorf("Update check failed %v", err)
} else { return
newVersion, _ := ioutil.ReadAll(resp.Body)
log.WithFields(log.Fields {
"NewVersion": string(newVersion),
}).Infof("Update check complete");
defer resp.Body.Close();
} }
newVersion, _ := ioutil.ReadAll(resp.Body)
log.WithFields(log.Fields{
"NewVersion": string(newVersion),
}).Infof("Update check complete")
defer resp.Body.Close()
} }