1
0
Fork 0
mirror of https://gitea.com/jolheiser/sip synced 2024-06-10 17:06:05 +02:00
sip/flag/flag.go
jolheiser 67326a7948
Start work on flags
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2020-09-18 14:28:02 -05:00

127 lines
2.6 KiB
Go

package flag
import (
"fmt"
"sync"
"gitea.com/jolheiser/sip/config"
"gitea.com/jolheiser/sip/git"
"github.com/urfave/cli/v2"
)
type Remote struct {
Name string
URL string
Owner string
Repo string
}
var (
// Set via config
Origin Remote
Upstream Remote
// Set via flags
URL string
Owner string
Repo string
Token string
// Set via Before
OwnerRepoCtxSet bool
Flags = []cli.Flag{
&cli.StringFlag{
Name: "origin",
Usage: "The origin remote",
Value: config.Origin,
DefaultText: "Configured origin",
Destination: &Origin.Name,
},
&cli.StringFlag{
Name: "upstream",
Usage: "The upstream remote",
Value: config.Upstream,
DefaultText: "Configured upstream",
Destination: &Upstream.Name,
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "The base URL to the Gitea instance",
Value: getUpstream().URL,
DefaultText: "Upstream Gitea instance",
Destination: &URL,
},
&cli.StringFlag{
Name: "owner",
Aliases: []string{"o"},
Usage: "The owner to target",
Value: getUpstream().Owner,
DefaultText: "Upstream owner",
Destination: &Owner,
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: "The repo to target",
Value: getUpstream().Repo,
DefaultText: "Upstream repository",
Destination: &Repo,
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "The access token to use (by name)",
Destination: &Token,
},
}
originOnce sync.Once
upstreamOnce sync.Once
)
func FullName() string {
return fmt.Sprintf("%s/%s", Owner, Repo)
}
func FullURL() string {
return fmt.Sprintf("%s/%s", URL, FullName())
}
func Before(ctx *cli.Context) error {
OwnerRepoCtxSet = ctx.IsSet("owner") || ctx.IsSet("repo")
return nil
}
func defRemote(remote, def string) string {
if remote == "" {
return def
}
return remote
}
func getUpstream() Remote {
upstreamOnce.Do(func() {
Upstream.URL, Upstream.Owner, Upstream.Repo = getOrigin().URL, getOrigin().Owner, getOrigin().Name
upstream, err := git.GetRepo(defRemote(config.Upstream, "upstream"))
if err == nil {
Upstream.URL, Upstream.Owner, Upstream.Repo = upstream[0], upstream[1], upstream[2]
}
})
return Upstream
}
func getOrigin() Remote {
originOnce.Do(func() {
Origin.URL, Origin.Owner, Origin.Name = "https://gitea.com", "jolheiser", "sip"
origin, err := git.GetRepo(defRemote(config.Origin, "origin"))
if err == nil {
Origin.URL, Origin.Owner, Origin.Repo = origin[0], origin[1], origin[2]
}
})
return Origin
}