forgejo-runner

git clone git://git.lin.moe/forgejo-runner.git

 1// Copyright 2023 The Gitea Authors. All rights reserved.
 2// SPDX-License-Identifier: MIT
 3
 4package config
 5
 6import (
 7	"encoding/json"
 8	"os"
 9)
10
11const registrationWarning = "This file is automatically generated by act-runner. Do not edit it manually unless you know what you are doing. Removing this file will cause act runner to re-register as a new runner."
12
13// Registration is the registration information for a runner
14type Registration struct {
15	Warning string `json:"WARNING"` // Warning message to display, it's always the registrationWarning constant
16
17	ID      int64    `json:"id"`
18	UUID    string   `json:"uuid"`
19	Name    string   `json:"name"`
20	Token   string   `json:"token"`
21	Address string   `json:"address"`
22	Labels  []string `json:"labels"`
23}
24
25func LoadRegistration(file string) (*Registration, error) {
26	f, err := os.Open(file)
27	if err != nil {
28		return nil, err
29	}
30	defer f.Close()
31
32	var reg Registration
33	if err := json.NewDecoder(f).Decode(&reg); err != nil {
34		return nil, err
35	}
36
37	reg.Warning = ""
38
39	return &reg, nil
40}
41
42func SaveRegistration(file string, reg *Registration) error {
43	f, err := os.Create(file)
44	if err != nil {
45		return err
46	}
47	defer f.Close()
48
49	reg.Warning = registrationWarning
50
51	enc := json.NewEncoder(f)
52	enc.SetIndent("", "  ")
53	return enc.Encode(reg)
54}