loading up the forgejo repo on tangled to test page performance
1// Copyright twenty-panda <twenty-panda@posteo.com>
2// SPDX-License-Identifier: MIT
3
4package pushoptions
5
6import (
7 "fmt"
8 "os"
9 "strconv"
10 "strings"
11)
12
13type Key string
14
15const (
16 RepoPrivate = Key("repo.private")
17 RepoTemplate = Key("repo.template")
18 AgitTopic = Key("topic")
19 AgitForcePush = Key("force-push")
20 AgitTitle = Key("title")
21 AgitDescription = Key("description")
22
23 envPrefix = "GIT_PUSH_OPTION"
24 EnvCount = envPrefix + "_COUNT"
25 EnvFormat = envPrefix + "_%d"
26)
27
28type Interface interface {
29 ReadEnv() Interface
30 Parse(string) bool
31 Map() map[string]string
32
33 ChangeRepoSettings() bool
34
35 Empty() bool
36
37 GetBool(key Key, def bool) bool
38 GetString(key Key) (val string, ok bool)
39}
40
41type gitPushOptions map[string]string
42
43func New() Interface {
44 pushOptions := gitPushOptions(make(map[string]string))
45 return &pushOptions
46}
47
48func NewFromMap(o *map[string]string) Interface {
49 return (*gitPushOptions)(o)
50}
51
52func (o *gitPushOptions) ReadEnv() Interface {
53 if pushCount, err := strconv.Atoi(os.Getenv(EnvCount)); err == nil {
54 for idx := 0; idx < pushCount; idx++ {
55 _ = o.Parse(os.Getenv(fmt.Sprintf(EnvFormat, idx)))
56 }
57 }
58 return o
59}
60
61func (o *gitPushOptions) Parse(data string) bool {
62 key, value, found := strings.Cut(data, "=")
63 if !found {
64 value = "true"
65 }
66 switch Key(key) {
67 case RepoPrivate:
68 case RepoTemplate:
69 case AgitTopic:
70 case AgitForcePush:
71 case AgitTitle:
72 case AgitDescription:
73 default:
74 return false
75 }
76 (*o)[key] = value
77 return true
78}
79
80func (o gitPushOptions) Map() map[string]string {
81 return o
82}
83
84func (o gitPushOptions) ChangeRepoSettings() bool {
85 if o.Empty() {
86 return false
87 }
88 for _, key := range []Key{RepoPrivate, RepoTemplate} {
89 _, ok := o[string(key)]
90 if ok {
91 return true
92 }
93 }
94 return false
95}
96
97func (o gitPushOptions) Empty() bool {
98 return len(o) == 0
99}
100
101func (o gitPushOptions) GetBool(key Key, def bool) bool {
102 if val, ok := o[string(key)]; ok {
103 if b, err := strconv.ParseBool(val); err == nil {
104 return b
105 }
106 }
107 return def
108}
109
110func (o gitPushOptions) GetString(key Key) (string, bool) {
111 val, ok := o[string(key)]
112 return val, ok
113}