-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_git_envs.go
109 lines (94 loc) · 2.84 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
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
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
corev1 "k8s.io/api/core/v1"
)
// StepGitEnvsOptions contains the command line flags
type StepGitEnvsOptions struct {
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(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := StepGitEnvsOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
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()
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 {
secrets, err := o.LoadPipelineSecrets(kube.ValueKindGit, "")
if err != nil {
return errors.Wrap(err, "loading the pipeline secrets")
}
username, token, err := o.getGitCredentials(secrets, o.ServiceKind)
if err != nil {
return errors.Wrap(err, "retrieving the environment variables values")
}
_, _ = fmt.Fprintf(o.Out, "export GIT_USERNAME=%s\nexport GIT_API_TOKEN=%s\n", username, token)
return nil
}
func (o *StepGitEnvsOptions) getGitCredentials(secrets *corev1.SecretList, serviceKind string) (string, string, error) {
if secrets == nil {
return "", "", errors.New("no git credentials found")
}
for _, secret := range secrets.Items {
labels := secret.Labels
data := secret.Data
if data == nil {
continue
}
if labels != nil && labels[kube.LabelKind] == kube.ValueKindGit {
foundServiceKind, ok := labels[kube.LabelServiceKind]
if !ok {
continue
}
if strings.EqualFold(serviceKind, foundServiceKind) {
username := string(data[kube.SecretDataUsername])
pwd := string(data[kube.SecretDataPassword])
return username, pwd, nil
}
}
}
return "", "", errors.New("no git credentials found")
}