diff --git a/Makefile b/Makefile index 9c4ecf5..ca4a969 100644 --- a/Makefile +++ b/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 diff --git a/cmd/OliveTin/main.go b/cmd/OliveTin/main.go index 6c522bf..4fedede 100644 --- a/cmd/OliveTin/main.go +++ b/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) diff --git a/internal/config/config.go b/internal/config/config.go index 2d05929..787c7fb 100644 --- a/internal/config/config.go +++ b/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 } diff --git a/internal/executor/executor.go b/internal/executor/executor.go index 489346d..bf2e903 100644 --- a/internal/executor/executor.go +++ b/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, diff --git a/internal/grpcapi/grpcApi.go b/internal/grpcapi/grpcApi.go index 54cf2b8..a9373c0 100644 --- a/internal/grpcapi/grpcApi.go +++ b/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) diff --git a/internal/updatecheck/updateCheck.go b/internal/updatecheck/updateCheck.go index 46423e5..a6b2ad5 100644 --- a/internal/updatecheck/updateCheck.go +++ b/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" - "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() }