mirror of https://github.com/go-gitea/gitea.git
Make git push options accept short name (#32245)
Just like what most CLI parsers do: `--opt` means `opt=true` Then users could use `-o force-push` as `-o force-push=true`pull/31610/head^2
parent
900ac62251
commit
afa8dd45af
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package private
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/optional"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GitPushOptions is a wrapper around a map[string]string
|
||||||
|
type GitPushOptions map[string]string
|
||||||
|
|
||||||
|
// GitPushOptions keys
|
||||||
|
const (
|
||||||
|
GitPushOptionRepoPrivate = "repo.private"
|
||||||
|
GitPushOptionRepoTemplate = "repo.template"
|
||||||
|
GitPushOptionForcePush = "force-push"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bool checks for a key in the map and parses as a boolean
|
||||||
|
// An option without value is considered true, eg: "-o force-push" or "-o repo.private"
|
||||||
|
func (g GitPushOptions) Bool(key string) optional.Option[bool] {
|
||||||
|
if val, ok := g[key]; ok {
|
||||||
|
if val == "" {
|
||||||
|
return optional.Some(true)
|
||||||
|
}
|
||||||
|
if b, err := strconv.ParseBool(val); err == nil {
|
||||||
|
return optional.Some(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return optional.None[bool]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddFromKeyValue adds a key value pair to the map by "key=value" format or "key" for empty value
|
||||||
|
func (g GitPushOptions) AddFromKeyValue(line string) {
|
||||||
|
kv := strings.SplitN(line, "=", 2)
|
||||||
|
if len(kv) == 2 {
|
||||||
|
g[kv[0]] = kv[1]
|
||||||
|
} else {
|
||||||
|
g[kv[0]] = ""
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package private
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGitPushOptions(t *testing.T) {
|
||||||
|
o := GitPushOptions{}
|
||||||
|
|
||||||
|
v := o.Bool("no-such")
|
||||||
|
assert.False(t, v.Has())
|
||||||
|
assert.False(t, v.Value())
|
||||||
|
|
||||||
|
o.AddFromKeyValue("opt1=a=b")
|
||||||
|
o.AddFromKeyValue("opt2=false")
|
||||||
|
o.AddFromKeyValue("opt3=true")
|
||||||
|
o.AddFromKeyValue("opt4")
|
||||||
|
|
||||||
|
assert.Equal(t, "a=b", o["opt1"])
|
||||||
|
assert.False(t, o.Bool("opt1").Value())
|
||||||
|
assert.True(t, o.Bool("opt2").Has())
|
||||||
|
assert.False(t, o.Bool("opt2").Value())
|
||||||
|
assert.True(t, o.Bool("opt3").Value())
|
||||||
|
assert.True(t, o.Bool("opt4").Value())
|
||||||
|
}
|
Loading…
Reference in New Issue