diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /modules/git/pushoptions/pushoptions.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r-- | modules/git/pushoptions/pushoptions.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/modules/git/pushoptions/pushoptions.go b/modules/git/pushoptions/pushoptions.go new file mode 100644 index 0000000..9709a8b --- /dev/null +++ b/modules/git/pushoptions/pushoptions.go @@ -0,0 +1,113 @@ +// Copyright twenty-panda <twenty-panda@posteo.com> +// SPDX-License-Identifier: MIT + +package pushoptions + +import ( + "fmt" + "os" + "strconv" + "strings" +) + +type Key string + +const ( + RepoPrivate = Key("repo.private") + RepoTemplate = Key("repo.template") + AgitTopic = Key("topic") + AgitForcePush = Key("force-push") + AgitTitle = Key("title") + AgitDescription = Key("description") + + envPrefix = "GIT_PUSH_OPTION" + EnvCount = envPrefix + "_COUNT" + EnvFormat = envPrefix + "_%d" +) + +type Interface interface { + ReadEnv() Interface + Parse(string) bool + Map() map[string]string + + ChangeRepoSettings() bool + + Empty() bool + + GetBool(key Key, def bool) bool + GetString(key Key) (val string, ok bool) +} + +type gitPushOptions map[string]string + +func New() Interface { + pushOptions := gitPushOptions(make(map[string]string)) + return &pushOptions +} + +func NewFromMap(o *map[string]string) Interface { + return (*gitPushOptions)(o) +} + +func (o *gitPushOptions) ReadEnv() Interface { + if pushCount, err := strconv.Atoi(os.Getenv(EnvCount)); err == nil { + for idx := 0; idx < pushCount; idx++ { + _ = o.Parse(os.Getenv(fmt.Sprintf(EnvFormat, idx))) + } + } + return o +} + +func (o *gitPushOptions) Parse(data string) bool { + key, value, found := strings.Cut(data, "=") + if !found { + value = "true" + } + switch Key(key) { + case RepoPrivate: + case RepoTemplate: + case AgitTopic: + case AgitForcePush: + case AgitTitle: + case AgitDescription: + default: + return false + } + (*o)[key] = value + return true +} + +func (o gitPushOptions) Map() map[string]string { + return o +} + +func (o gitPushOptions) ChangeRepoSettings() bool { + if o.Empty() { + return false + } + for _, key := range []Key{RepoPrivate, RepoTemplate} { + _, ok := o[string(key)] + if ok { + return true + } + } + return false +} + +func (o gitPushOptions) Empty() bool { + return len(o) == 0 +} + +func (o gitPushOptions) GetBool(key Key, def bool) bool { + if val, ok := o[string(key)]; ok { + if b, err := strconv.ParseBool(val); err == nil { + return b + } + } + return def +} + +func (o gitPushOptions) GetString(key Key) (string, bool) { + val, ok := o[string(key)] + return val, ok +} |