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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package cmdutils
import (
"fmt"
"strings"
gitlab "gitlab.com/gitlab-org/api/client-go"
"gitlab.com/gitlab-org/cli/api"
"gitlab.com/gitlab-org/cli/internal/config"
"gitlab.com/gitlab-org/cli/internal/glrepo"
"gitlab.com/gitlab-org/cli/pkg/git"
"gitlab.com/gitlab-org/cli/pkg/glinstance"
"gitlab.com/gitlab-org/cli/pkg/iostreams"
)
var (
CachedConfig config.Config
ConfigError error
)
type Factory struct {
HttpClient func() (*gitlab.Client, error)
BaseRepo func() (glrepo.Interface, error)
Remotes func() (glrepo.Remotes, error)
Config func() (config.Config, error)
Branch func() (string, error)
IO *iostreams.IOStreams
}
// MIT License
//
// Copyright (c) 2019 GitHub Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
func (f *Factory) RepoOverride(repo string) error {
f.BaseRepo = func() (glrepo.Interface, error) {
return glrepo.FromFullName(repo)
}
newRepo, err := f.BaseRepo()
if err != nil {
return err
}
// Initialise new http client for new repo host
cfg, err := f.Config()
if err == nil {
OverrideAPIProtocol(cfg, newRepo)
}
f.HttpClient = func() (*gitlab.Client, error) {
return LabClientFunc(newRepo.RepoHost(), cfg, false)
}
return nil
}
func LabClientFunc(repoHost string, cfg config.Config, isGraphQL bool) (*gitlab.Client, error) {
c, err := api.NewClientWithCfg(repoHost, cfg, isGraphQL)
if err != nil {
return nil, err
}
return c.Lab(), nil
}
func remotesFunc() (glrepo.Remotes, error) {
hostOverride := ""
if !strings.EqualFold(glinstance.Default(), glinstance.OverridableDefault()) {
hostOverride = glinstance.OverridableDefault()
}
rr := &remoteResolver{
readRemotes: git.Remotes,
getConfig: configFunc,
}
fn := rr.Resolver(hostOverride)
return fn()
}
func configFunc() (config.Config, error) {
if CachedConfig != nil || ConfigError != nil {
return CachedConfig, ConfigError
}
CachedConfig, ConfigError = initConfig()
return CachedConfig, ConfigError
}
func baseRepoFunc() (glrepo.Interface, error) {
remotes, err := remotesFunc()
if err != nil {
return nil, err
}
return remotes[0], nil
}
// OverrideAPIProtocol sets api protocol for host to initialize http client
func OverrideAPIProtocol(cfg config.Config, repo glrepo.Interface) {
protocol, _ := cfg.Get(repo.RepoHost(), "api_protocol")
api.SetProtocol(protocol)
}
func HTTPClientFactory(f *Factory) {
f.HttpClient = func() (*gitlab.Client, error) {
cfg, err := configFunc()
if err != nil {
return nil, err
}
repo, err := baseRepoFunc()
if err != nil {
// use default hostname if remote resolver fails
repo = glrepo.NewWithHost("", "", glinstance.OverridableDefault())
}
OverrideAPIProtocol(cfg, repo)
return LabClientFunc(repo.RepoHost(), cfg, false)
}
}
func NewFactory() *Factory {
return &Factory{
Config: configFunc,
Remotes: remotesFunc,
HttpClient: func() (*gitlab.Client, error) {
// do not initialize httpclient since it may not be required by
// some commands like version, help, etc...
// It should be explicitly initialize with HTTPClientFactory()
return nil, nil
},
BaseRepo: baseRepoFunc,
Branch: func() (string, error) {
currentBranch, err := git.CurrentBranch()
if err != nil {
return "", fmt.Errorf("could not determine current branch: %w", err)
}
return currentBranch, nil
},
IO: iostreams.Init(),
}
}
func initConfig() (config.Config, error) {
return config.Init()
}
|