소스 검색

Update check cleanup, gofmt and gocyclo = 3

jamesread 5 년 전
부모
커밋
a4def7571c
6개의 변경된 파일55개의 추가작업 그리고 39개의 파일을 삭제
  1. 1 1
      Makefile
  2. 1 1
      cmd/OliveTin/main.go
  3. 3 1
      internal/config/config.go
  4. 11 4
      internal/executor/executor.go
  5. 5 5
      internal/grpcapi/grpcApi.go
  6. 34 27
      internal/updatecheck/updateCheck.go

+ 1 - 1
Makefile

@@ -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

+ 1 - 1
cmd/OliveTin/main.go

@@ -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)
 

+ 3 - 1
internal/config/config.go

@@ -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
 }

+ 11 - 4
internal/executor/executor.go

@@ -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,

+ 5 - 5
internal/grpcapi/grpcApi.go

@@ -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)

+ 34 - 27
internal/updatecheck/updateCheck.go

@@ -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"
+	config "github.com/jamesread/OliveTin/internal/config"
+	log "github.com/sirupsen/logrus"
+	"io/ioutil"
 	"net/http"
-	"encoding/json"
-	"bytes"
+	"runtime"
 	"time"
-	"io/ioutil"
 )
 
-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)
+		return
+	}
 
-		log.WithFields(log.Fields {
-			"NewVersion": string(newVersion),
-		}).Infof("Update check complete");
+	newVersion, _ := ioutil.ReadAll(resp.Body)
 
-		defer resp.Body.Close();
-	}
+	log.WithFields(log.Fields{
+		"NewVersion": string(newVersion),
+	}).Infof("Update check complete")
 
+	defer resp.Body.Close()
 }