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 vet ./...
golint ./...
gocyclo -over 4 cmd internal
gocyclo -over 3 cmd internal
daemon-unittests:
mkdir -p reports

View File

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

View File

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

View File

@ -13,9 +13,6 @@ import (
// ExecAction executes an action.
func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
res := &pb.StartActionResponse{}
res.TimedOut = false
log.WithFields(log.Fields{
"actionName": action,
}).Infof("StartAction")
@ -24,9 +21,19 @@ func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
if err != nil {
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{
"title": actualAction.Title,
"timeout": actualAction.Timeout,

View File

@ -2,12 +2,12 @@ package grpcapi
import (
ctx "context"
"crypto/md5"
"fmt"
pb "github.com/jamesread/OliveTin/gen/grpc"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"net"
"crypto/md5"
"fmt"
config "github.com/jamesread/OliveTin/internal/config"
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 {
btn := pb.ActionButton{
Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
Title: action.Title,
Icon: lookupHTMLIcon(action.Icon),
}
res.Actions = append(res.Actions, &btn)

View File

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