mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-06 17:40:58 +00:00
feat: add check for newer versions (#1562)
* feat: add check for newer versions * fix: support JSON logger and rever updates to go.mod * fix: keep version updated in source code * fix: lint errors * fix: revert go.*
This commit is contained in:
parent
409d161ed1
commit
e83ef12e1b
3 changed files with 114 additions and 12 deletions
90
cmd/notices.go
Normal file
90
cmd/notices.go
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Notice struct {
|
||||||
|
Level string `json:"level"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayNotices(input *Input) {
|
||||||
|
select {
|
||||||
|
case notices := <-noticesLoaded:
|
||||||
|
if len(notices) > 0 {
|
||||||
|
noticeLogger := log.New()
|
||||||
|
if input.jsonLogger {
|
||||||
|
noticeLogger.SetFormatter(&log.JSONFormatter{})
|
||||||
|
} else {
|
||||||
|
noticeLogger.SetFormatter(&log.TextFormatter{
|
||||||
|
DisableQuote: true,
|
||||||
|
DisableTimestamp: true,
|
||||||
|
PadLevelText: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n")
|
||||||
|
for _, notice := range notices {
|
||||||
|
level, err := log.ParseLevel(notice.Level)
|
||||||
|
if err != nil {
|
||||||
|
level = log.InfoLevel
|
||||||
|
}
|
||||||
|
noticeLogger.Log(level, notice.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second * 1):
|
||||||
|
log.Debugf("Timeout waiting for notices")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var noticesLoaded = make(chan []Notice)
|
||||||
|
|
||||||
|
func loadVersionNotices(version string) {
|
||||||
|
go func() {
|
||||||
|
noticesLoaded <- getVersionNotices(version)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
const NoticeURL = "https://api.nektosact.com/notices"
|
||||||
|
|
||||||
|
func getVersionNotices(version string) []Notice {
|
||||||
|
if os.Getenv("ACT_DISABLE_VERSION_CHECK") == "1" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
noticeURL, err := url.Parse(NoticeURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
query := noticeURL.Query()
|
||||||
|
query.Add("os", runtime.GOOS)
|
||||||
|
query.Add("arch", runtime.GOARCH)
|
||||||
|
query.Add("version", version)
|
||||||
|
|
||||||
|
noticeURL.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
resp, err := http.Get(noticeURL.String())
|
||||||
|
if err != nil {
|
||||||
|
log.Debug(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
notices := []Notice{}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(¬ices); err != nil {
|
||||||
|
log.Debug(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return notices
|
||||||
|
}
|
14
cmd/root.go
14
cmd/root.go
|
@ -34,7 +34,8 @@ func Execute(ctx context.Context, version string) {
|
||||||
Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
|
Short: "Run GitHub actions locally by specifying the event name (e.g. `push`) or an action name directly.",
|
||||||
Args: cobra.MaximumNArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
RunE: newRunCommand(ctx, input),
|
RunE: newRunCommand(ctx, input),
|
||||||
PersistentPreRun: setupLogging,
|
PersistentPreRun: setup(input),
|
||||||
|
PersistentPostRun: cleanup(input),
|
||||||
Version: version,
|
Version: version,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
|
@ -244,11 +245,20 @@ func readArgsFile(file string, split bool) []string {
|
||||||
return args
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogging(cmd *cobra.Command, _ []string) {
|
func setup(inputs *Input) func(*cobra.Command, []string) {
|
||||||
|
return func(cmd *cobra.Command, _ []string) {
|
||||||
verbose, _ := cmd.Flags().GetBool("verbose")
|
verbose, _ := cmd.Flags().GetBool("verbose")
|
||||||
if verbose {
|
if verbose {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
}
|
}
|
||||||
|
loadVersionNotices(cmd.Version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanup(inputs *Input) func(*cobra.Command, []string) {
|
||||||
|
return func(cmd *cobra.Command, _ []string) {
|
||||||
|
displayNotices(inputs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseEnvs(env []string, envs map[string]string) bool {
|
func parseEnvs(env []string, envs map[string]string) bool {
|
||||||
|
|
4
main.go
4
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
_ "embed"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -9,7 +10,8 @@ import (
|
||||||
"github.com/nektos/act/cmd"
|
"github.com/nektos/act/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "v0.2.27-dev" // Manually bump after tagging next release
|
//go:embed VERSION
|
||||||
|
var version string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue