-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_git_envs.go
81 lines (68 loc) · 2.15 KB
/
step_git_envs.go
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
package git
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// StepGitEnvsOptions contains the command line flags
type StepGitEnvsOptions struct {
step.StepOptions
ServiceKind string
}
var (
// StepGitEnvsLong command long description
StepGitEnvsLong = templates.LongDesc(`
This pipeline step generates a Git environment variables from the current Git provider pipeline Secrets
`)
// StepGitEnvsExample command example
StepGitEnvsExample = templates.Examples(`
# Sets the Git environment variables for the current GitHub provider
jx step git envs
# Sets the Gie environment variables for the current Gtilab provider
jx step git envs --service-kind=gitlab
`)
)
// NewCmdStepGitEnvs create the 'step git envs' command
func NewCmdStepGitEnvs(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepGitEnvsOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "envs",
Short: "Creates the Git environment variables for the current pipeline Git credentials",
Long: StepGitEnvsLong,
Example: StepGitEnvsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.ServiceKind, "service-kind", "", "github", "The kind of git service")
return cmd
}
// Run implements the command
func (o *StepGitEnvsOptions) Run() error {
gitAuthSvc, err := o.GitAuthConfigService()
if err != nil {
return errors.Wrap(err, "creating the git auth config service")
}
cfg := gitAuthSvc.Config()
server := cfg.GetServerByKind(o.ServiceKind)
if server == nil {
return fmt.Errorf("no server found of kind %q", o.ServiceKind)
}
auth := server.CurrentAuth()
if auth == nil {
return fmt.Errorf("server %q has no user auth configured", server.URL)
}
_, _ = fmt.Fprintf(o.Out, "export GIT_USERNAME=%s\nexport GIT_API_TOKEN=%s\n", auth.Username, auth.ApiToken)
return nil
}