chore: modernize code (#857)

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/857
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
Reviewed-by: Gusted <gusted@noreply.code.forgejo.org>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-committed-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Michael Kriese 2025-08-15 04:54:13 +00:00 committed by Michael Kriese
parent 886bf2a4f3
commit 27f425987c
No known key found for this signature in database
GPG key ID: F128CBE6AB3A7201
49 changed files with 316 additions and 363 deletions

View file

@ -5,6 +5,7 @@ package config
import (
"fmt"
"maps"
"os"
"path/filepath"
"time"
@ -109,9 +110,7 @@ func LoadDefault(file string) (*Config, error) {
if cfg.Runner.Envs == nil {
cfg.Runner.Envs = map[string]string{}
}
for k, v := range envs {
cfg.Runner.Envs[k] = v
}
maps.Copy(cfg.Runner.Envs, envs)
}
}

View file

@ -81,7 +81,7 @@ func NewReporter(ctx context.Context, cancel context.CancelFunc, c client.Client
func (r *Reporter) ResetSteps(l int) {
r.stateMu.Lock()
defer r.stateMu.Unlock()
for i := 0; i < l; i++ {
for i := range l {
r.state.Steps = append(r.state.Steps, &runnerv1.StepState{
Id: int64(i),
})
@ -189,14 +189,14 @@ func (r *Reporter) RunDaemon() {
time.AfterFunc(r.reportInterval, r.RunDaemon)
}
func (r *Reporter) Logf(format string, a ...interface{}) {
func (r *Reporter) Logf(format string, a ...any) {
r.stateMu.Lock()
defer r.stateMu.Unlock()
r.logf(format, a...)
}
func (r *Reporter) logf(format string, a ...interface{}) {
func (r *Reporter) logf(format string, a ...any) {
if !r.duringSteps() {
r.logRows = append(r.logRows, &runnerv1.LogRow{
Time: timestamppb.Now(),
@ -210,7 +210,7 @@ func (r *Reporter) SetOutputs(outputs map[string]string) error {
defer r.stateMu.Unlock()
var errs []error
recordError := func(format string, a ...interface{}) {
recordError := func(format string, a ...any) {
r.logf(format, a...)
errs = append(errs, fmt.Errorf(format, a...))
}
@ -341,7 +341,7 @@ func (r *Reporter) ReportState() error {
r.stateMu.RUnlock()
outputs := make(map[string]string)
r.outputs.Range(func(k, v interface{}) bool {
r.outputs.Range(func(k, v any) bool {
if val, ok := v.(string); ok {
outputs[k.(string)] = val
}
@ -365,7 +365,7 @@ func (r *Reporter) ReportState() error {
}
var noSent []string
r.outputs.Range(func(k, v interface{}) bool {
r.outputs.Range(func(k, v any) bool {
if _, ok := v.(string); ok {
noSent = append(noSent, k.(string))
}
@ -396,7 +396,7 @@ var stringToResult = map[string]runnerv1.Result{
"cancelled": runnerv1.Result_RESULT_CANCELLED,
}
func (r *Reporter) parseResult(result interface{}) (runnerv1.Result, bool) {
func (r *Reporter) parseResult(result any) (runnerv1.Result, bool) {
str := ""
if v, ok := result.(string); ok { // for jobResult
str = v

View file

@ -49,7 +49,7 @@ func mockReporter(t *testing.T) (*Reporter, *mocks.Client, func()) {
client := mocks.NewClient(t)
ctx, cancel := context.WithCancel(context.Background())
taskCtx, err := structpb.NewStruct(map[string]interface{}{})
taskCtx, err := structpb.NewStruct(map[string]any{})
require.NoError(t, err)
reporter := NewReporter(ctx, cancel, client, &runnerv1.Task{
Context: taskCtx,
@ -64,7 +64,7 @@ func TestReporterSetOutputs(t *testing.T) {
assertEqual := func(t *testing.T, expected map[string]string, actual *sync.Map) {
t.Helper()
actualMap := map[string]string{}
actual.Range(func(k, v interface{}) bool {
actual.Range(func(k, v any) bool {
val, ok := v.(string)
require.True(t, ok)
actualMap[k.(string)] = val
@ -268,7 +268,7 @@ func TestReporter_Fire(t *testing.T) {
return connect_go.NewResponse(&runnerv1.UpdateTaskResponse{}), nil
})
dataStep0 := map[string]interface{}{
dataStep0 := map[string]any{
"stage": "Main",
"stepNumber": 0,
"raw_output": true,