summaryrefslogtreecommitdiffstats
path: root/modules/git/pushoptions/pushoptions.go
blob: 9709a8be79657d43f119458d320a6b480f941365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
}