Skip to content

Commit af8da15

Browse files
authored
Merge pull request #683 from sm43/remove-fmt-printf
Replace fmt.Print with cobra stdoutwriter
2 parents 8dbcf6b + 8a3c9a9 commit af8da15

File tree

15 files changed

+64
-71
lines changed

15 files changed

+64
-71
lines changed

pkg/cli/webhook/github.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/AlecAivazis/survey/v2"
1111
"github.com/google/go-github/v43/github"
12+
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
1213
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
1314
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
1415
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
@@ -19,6 +20,7 @@ const apiPublicURL = "https://api.github.com/"
1920

2021
type gitHubConfig struct {
2122
Client *github.Client
23+
IOStream *cli.IOStreams
2224
controllerURL string
2325
repoOwner string
2426
repoName string
@@ -74,8 +76,7 @@ func (gh *gitHubConfig) askGHWebhookConfig(repoURL, controllerURL, apiURL string
7476
// confirm whether to use the detected url
7577
if gh.controllerURL != "" {
7678
var answer bool
77-
// nolint
78-
fmt.Printf("👀 I have detected a controller url: %s", gh.controllerURL)
79+
fmt.Fprintf(gh.IOStream.Out, "👀 I have detected a controller url: %s", gh.controllerURL)
7980
err := prompt.SurveyAskOne(&survey.Confirm{
8081
Message: "Do you want me to use it?",
8182
Default: true,
@@ -102,10 +103,8 @@ func (gh *gitHubConfig) askGHWebhookConfig(repoURL, controllerURL, apiURL string
102103
return err
103104
}
104105

105-
// nolint:forbidigo
106-
fmt.Println("ℹ ️You now need to create a GitHub personal token with scopes `public_repo` & `admin:repo_hook`")
107-
// nolint:forbidigo
108-
fmt.Println("ℹ ️Go to this URL to generate a new token https://github.com/settings/tokens/new, see https://is.gd/G5gBFI for documentation ")
106+
fmt.Fprintln(gh.IOStream.Out, "ℹ ️You now need to create a GitHub personal token with scopes `public_repo` & `admin:repo_hook`")
107+
fmt.Fprintln(gh.IOStream.Out, "ℹ ️Go to this URL to generate a new token https://github.com/settings/tokens/new, see https://is.gd/G5gBFI for documentation ")
109108
if err := prompt.SurveyAskOne(&survey.Password{
110109
Message: "Please enter the GitHub access token: ",
111110
}, &gh.personalAccessToken, survey.WithValidator(survey.Required)); err != nil {
@@ -162,8 +161,7 @@ func (gh *gitHubConfig) create(ctx context.Context) error {
162161
gh.repoOwner, gh.repoName, res.Response.StatusCode, payload)
163162
}
164163

165-
// nolint:forbidigo
166-
fmt.Printf("✓ Webhook has been created on repository %v/%v\n", gh.repoOwner, gh.repoName)
164+
fmt.Fprintf(gh.IOStream.Out, "✓ Webhook has been created on repository %v/%v\n", gh.repoOwner, gh.repoName)
167165
return nil
168166
}
169167

pkg/cli/webhook/github_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
89
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
910
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
1011
"gotest.tools/v3/assert"
1112
rtesting "knative.dev/pkg/reconciler/testing"
1213
)
1314

1415
func TestAskGHWebhookConfig(t *testing.T) {
16+
// nolint
17+
io, _, _, _ := cli.IOTest()
1518
tests := []struct {
1619
name string
1720
wantErrStr string
@@ -56,7 +59,7 @@ func TestAskGHWebhookConfig(t *testing.T) {
5659
if tt.askStubs != nil {
5760
tt.askStubs(as)
5861
}
59-
gh := gitHubConfig{}
62+
gh := gitHubConfig{IOStream: io}
6063
err := gh.askGHWebhookConfig(tt.repoURL, tt.controllerURL, "")
6164
if tt.wantErrStr != "" {
6265
assert.Equal(t, err.Error(), tt.wantErrStr)
@@ -70,6 +73,8 @@ func TestAskGHWebhookConfig(t *testing.T) {
7073
func TestCreate(t *testing.T) {
7174
fakeclient, mux, _, teardown := ghtesthelper.SetupGH()
7275
defer teardown()
76+
// nolint
77+
io, _, _, _ := cli.IOTest()
7378

7479
// webhook created for repo pac/valid
7580
mux.HandleFunc("/repos/pac/valid/hooks", func(w http.ResponseWriter, r *http.Request) {
@@ -105,6 +110,7 @@ func TestCreate(t *testing.T) {
105110
t.Run(tt.name, func(t *testing.T) {
106111
ctx, _ := rtesting.SetupFakeContext(t)
107112
gh := gitHubConfig{
113+
IOStream: io,
108114
Client: fakeclient,
109115
repoOwner: tt.repoOwner,
110116
repoName: tt.repoName,

pkg/cli/webhook/gitlab.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"net/http"
88

99
"github.com/AlecAivazis/survey/v2"
10+
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
1011
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
1112
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
1213
"github.com/xanzy/go-gitlab"
1314
)
1415

1516
type gitLabConfig struct {
1617
Client *gitlab.Client
18+
IOStream *cli.IOStreams
1719
controllerURL string
1820
projectID string
1921
webhookSecret string
@@ -53,8 +55,7 @@ func (gl *gitLabConfig) askGLWebhookConfig(controllerURL, apiURL string) error {
5355
// confirm whether to use the detected url
5456
if gl.controllerURL != "" {
5557
var answer bool
56-
// nolint
57-
fmt.Printf("👀 I have detected a controller url: %s", gl.controllerURL)
58+
fmt.Fprintf(gl.IOStream.Out, "👀 I have detected a controller url: %s", gl.controllerURL)
5859
err := prompt.SurveyAskOne(&survey.Confirm{
5960
Message: "Do you want me to use it?",
6061
Default: true,
@@ -81,10 +82,8 @@ func (gl *gitLabConfig) askGLWebhookConfig(controllerURL, apiURL string) error {
8182
return err
8283
}
8384

84-
// nolint:forbidigo
85-
fmt.Println("ℹ ️You now need to create a GitLab personal access token with `api` scope")
86-
// nolint:forbidigo
87-
fmt.Println("ℹ ️Go to this URL to generate one https://gitlab.com/-/profile/personal_access_tokens, see https://is.gd/rOEo9B for documentation ")
85+
fmt.Fprintln(gl.IOStream.Out, "ℹ ️You now need to create a GitLab personal access token with `api` scope")
86+
fmt.Fprintln(gl.IOStream.Out, "ℹ ️Go to this URL to generate one https://gitlab.com/-/profile/personal_access_tokens, see https://is.gd/rOEo9B for documentation ")
8887
if err := prompt.SurveyAskOne(&survey.Password{
8988
Message: "Please enter the GitLab access token: ",
9089
}, &gl.personalAccessToken, survey.WithValidator(survey.Required)); err != nil {
@@ -133,8 +132,7 @@ func (gl *gitLabConfig) create() error {
133132
resp.Response.StatusCode, payload)
134133
}
135134

136-
// nolint:forbidigo
137-
fmt.Printf("✓ Webhook has been created on your repository\n")
135+
fmt.Fprintln(gl.IOStream.Out, "✓ Webhook has been created on your repository")
138136
return nil
139137
}
140138

pkg/cli/webhook/gitlab_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
89
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
910
thelp "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab/test"
1011
"gotest.tools/v3/assert"
1112
rtesting "knative.dev/pkg/reconciler/testing"
1213
)
1314

1415
func TestAskGLWebhookConfig(t *testing.T) {
16+
// nolint
17+
io, _, _, _ := cli.IOTest()
1518
tests := []struct {
1619
name string
1720
wantErrStr string
@@ -48,7 +51,7 @@ func TestAskGLWebhookConfig(t *testing.T) {
4851
if tt.askStubs != nil {
4952
tt.askStubs(as)
5053
}
51-
gl := gitLabConfig{}
54+
gl := gitLabConfig{IOStream: io}
5255
err := gl.askGLWebhookConfig(tt.controllerURL, "")
5356
if tt.wantErrStr != "" {
5457
assert.Equal(t, err.Error(), tt.wantErrStr)
@@ -63,6 +66,8 @@ func TestGLCreate(t *testing.T) {
6366
ctx, _ := rtesting.SetupFakeContext(t)
6467
fakeclient, mux, teardown := thelp.Setup(ctx, t)
6568
defer teardown()
69+
// nolint
70+
io, _, _, _ := cli.IOTest()
6671

6772
// webhook created
6873
mux.HandleFunc("/projects/11/hooks", func(w http.ResponseWriter, r *http.Request) {
@@ -96,6 +101,7 @@ func TestGLCreate(t *testing.T) {
96101
for _, tt := range tests {
97102
t.Run(tt.name, func(t *testing.T) {
98103
gl := gitLabConfig{
104+
IOStream: io,
99105
Client: fakeclient,
100106
projectID: tt.projectID,
101107
}

pkg/cli/webhook/secret.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import (
1212
const (
1313
providerTokenKey = "provider.token"
1414
webhookSecretKey = "webhook.secret"
15-
16-
// nolint
17-
githubWebhookSecretName = "github-webhook-secret"
18-
// nolint
19-
gitlabWebhookSecretName = "gitlab-webhook-secret"
2015
)
2116

2217
func (w *Options) createWebhookSecret(ctx context.Context, response *response) error {
@@ -33,8 +28,7 @@ func (w *Options) createWebhookSecret(ctx context.Context, response *response) e
3328
return err
3429
}
3530

36-
// nolint:forbidigo
37-
fmt.Printf("🔑 Webhook Secret %s has been created in the %s namespace\n", w.RepositoryName, w.RepositoryNamespace)
31+
fmt.Fprintf(w.IOStreams.Out, "🔑 Webhook Secret %s has been created in the %s namespace.", w.RepositoryName, w.RepositoryNamespace)
3832
return nil
3933
}
4034

@@ -68,7 +62,6 @@ func (w *Options) updateRepositoryCR(ctx context.Context, res *response) error {
6862
return err
6963
}
7064

71-
// nolint:forbidigo
72-
fmt.Printf("🔑 Repository CR %s has been updated with webhook secret in the %s namespace\n", w.RepositoryName, w.RepositoryNamespace)
65+
fmt.Fprintf(w.IOStreams.Out, "🔑 Repository CR %s has been updated with webhook secret in the %s namespace\n", w.RepositoryName, w.RepositoryNamespace)
7366
return nil
7467
}

pkg/cli/webhook/webhook.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/AlecAivazis/survey/v2"
9+
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
910
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/info"
1011
"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/prompt"
1112
"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/tknpac/bootstrap"
@@ -20,6 +21,7 @@ type Interface interface {
2021

2122
type Options struct {
2223
Run *params.Run
24+
IOStreams *cli.IOStreams
2325
PACNamespace string
2426
ControllerURL string
2527
RepositoryURL string
@@ -53,12 +55,11 @@ func (w *Options) Install(ctx context.Context) error {
5355
}
5456

5557
// figure out which git provider from the Repo URL
56-
webhookProvider := detectProvider(w.RepositoryURL)
58+
webhookProvider := detectProvider(w.RepositoryURL, w.IOStreams)
5759

5860
if !w.GitHubWebhook && webhookProvider != nil {
5961
if webhookProvider.GetName() == provider.ProviderGitHubWebhook && pacInfo.Provider == provider.ProviderGitHubApp {
60-
// nolint
61-
fmt.Printf("✓ Skips configuring GitHub Webhook as GitHub App is already configured." +
62+
fmt.Fprintln(w.IOStreams.Out, "✓ Skips configuring GitHub Webhook as GitHub App is already configured."+
6263
" Please pass --github-webhook flag to still configure it")
6364
return nil
6465
}
@@ -81,7 +82,7 @@ func (w *Options) Install(ctx context.Context) error {
8182
}
8283

8384
if webhookProvider == nil {
84-
if webhookProvider, err = askProvider(); webhookProvider == nil || err != nil {
85+
if webhookProvider, err = askProvider(w.IOStreams); webhookProvider == nil || err != nil {
8586
return err
8687
}
8788
}
@@ -111,7 +112,7 @@ func (w *Options) Install(ctx context.Context) error {
111112
return w.updateRepositoryCR(ctx, response)
112113
}
113114

114-
func askProvider() (Interface, error) {
115+
func askProvider(ioStreams *cli.IOStreams) (Interface, error) {
115116
var answer string
116117
if err := survey.AskOne(&survey.Select{
117118
Message: "Please select the provider you wish to configure with your repository:",
@@ -121,18 +122,18 @@ func askProvider() (Interface, error) {
121122
}
122123

123124
if answer == "GitHub" {
124-
return &gitHubConfig{Hosted: true}, nil
125+
return &gitHubConfig{Hosted: true, IOStream: ioStreams}, nil
125126
} else if answer == "GitLab" {
126-
return &gitLabConfig{Hosted: true}, nil
127+
return &gitLabConfig{Hosted: true, IOStream: ioStreams}, nil
127128
}
128129
return nil, nil
129130
}
130131

131-
func detectProvider(url string) Interface {
132+
func detectProvider(url string, ioStreams *cli.IOStreams) Interface {
132133
if strings.Contains(url, "https://github.com") {
133-
return &gitHubConfig{}
134+
return &gitHubConfig{IOStream: ioStreams}
134135
} else if strings.Contains(url, "https://gitlab.com") {
135-
return &gitLabConfig{}
136+
return &gitLabConfig{IOStream: ioStreams}
136137
}
137138
return nil
138139
}

pkg/cmd/tknpac/bootstrap/bootstrap.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ const successTmpl = `
6161

6262
func install(ctx context.Context, run *params.Run, opts *bootstrapOpts) error {
6363
if !opts.forceInstall {
64-
// nolint:forbidigo
65-
fmt.Println("🏃 Checking if Pipelines as Code is installed.")
64+
fmt.Fprintln(opts.ioStreams.Out, "🏃 Checking if Pipelines as Code is installed.")
6665
}
6766
tektonInstalled, err := checkPipelinesInstalled(run)
6867
if err != nil {
@@ -82,8 +81,7 @@ func install(ctx context.Context, run *params.Run, opts *bootstrapOpts) error {
8281
}
8382

8483
if !opts.forceInstall && err == nil {
85-
// nolint:forbidigo
86-
fmt.Println("👌 Pipelines as Code is already installed.")
84+
fmt.Fprintln(opts.ioStreams.Out, "👌 Pipelines as Code is already installed.")
8785
} else if err := installPac(ctx, run, opts); err != nil {
8886
return err
8987
}
@@ -143,8 +141,7 @@ func Command(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
143141

144142
if !opts.forceGitHubApp {
145143
if pacInfo.Provider == provider.ProviderGitHubApp {
146-
// nolint
147-
fmt.Printf("👌 Skips bootstrapping GitHub App, as one is already configured. Please pass --force-configure to override existing\n")
144+
fmt.Fprintln(opts.ioStreams.Out, "👌 Skips bootstrapping GitHub App, as one is already configured. Please pass --force-configure to override existing")
148145
return nil
149146
}
150147
}
@@ -202,13 +199,12 @@ func GithubApp(run *params.Run, ioStreams *cli.IOStreams) *cobra.Command {
202199

203200
if !opts.forceGitHubApp {
204201
if pacInfo.Provider == provider.ProviderGitHubApp {
205-
// nolint
206-
fmt.Printf("👌 Skips bootstrapping GitHub App, as one is already configured. Please pass --force-configure to override existing\n")
202+
fmt.Fprintln(opts.ioStreams.Out, "👌 Skips bootstrapping GitHub App, as one is already configured. Please pass --force-configure to override existing")
207203
return nil
208204
}
209205
}
210206

211-
if b, _ := askYN(false, "", "Are you using GitHub Enterprise?"); b {
207+
if b, _ := askYN(false, "", "Are you using GitHub Enterprise?", opts.ioStreams.Out); b {
212208
opts.providerType = "github-enterprise-app"
213209
}
214210

pkg/cmd/tknpac/bootstrap/install.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func installPac(ctx context.Context, run *params.Run, opts *bootstrapOpts) error
7272
if !opts.forceInstall {
7373
doinstall, err := askYN(true,
7474
fmt.Sprintf("🕵️ Pipelines as Code doesn't seems to be installed in %s namespace", opts.targetNamespace),
75-
fmt.Sprintf("Do you want me to install Pipelines as Code %s?", latestVersion))
75+
fmt.Sprintf("Do you want me to install Pipelines as Code %s?", latestVersion), opts.ioStreams.Out)
7676
if err != nil {
7777
return err
7878
}
@@ -85,7 +85,6 @@ func installPac(ctx context.Context, run *params.Run, opts *bootstrapOpts) error
8585
return err
8686
}
8787

88-
// nolint:forbidigo
89-
fmt.Printf("✓ Pipelines-as-Code %s has been installed\n", latestVersion)
88+
fmt.Fprintf(opts.ioStreams.Out, "✓ Pipelines-as-Code %s has been installed\n", latestVersion)
9089
return nil
9190
}

pkg/cmd/tknpac/bootstrap/kubestuff.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func createPacSecret(ctx context.Context, run *params.Run, opts *bootstrapOpts,
3333
return err
3434
}
3535

36-
// nolint:forbidigo
37-
fmt.Printf("🔑 Secret %s has been created in the %s namespace\n", secretName, opts.targetNamespace)
38-
36+
fmt.Fprintf(opts.ioStreams.Out, "🔑 Secret %s has been created in the %s namespace\n", secretName, opts.targetNamespace)
3937
return nil
4038
}
4139

pkg/cmd/tknpac/bootstrap/questions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bootstrap
22

33
import (
44
"fmt"
5+
"io"
56
"strings"
67

78
"github.com/AlecAivazis/survey/v2"
@@ -10,11 +11,10 @@ import (
1011

1112
const defaultPublicGithub = "https://github.com"
1213

13-
func askYN(deflt bool, title, question string) (bool, error) {
14+
func askYN(deflt bool, title, question string, writer io.Writer) (bool, error) {
1415
var answer bool
15-
// nolint:forbidigo
1616
if title != "" {
17-
fmt.Printf("%s\n", title)
17+
fmt.Fprintf(writer, "%s\n", title)
1818
}
1919
err := prompt.SurveyAskOne(&survey.Confirm{
2020
Message: question,
@@ -63,7 +63,7 @@ func askQuestions(opts *bootstrapOpts) error {
6363
if opts.RouteName != "" {
6464
answer, err := askYN(true,
6565
fmt.Sprintf("👀 I have detected an OpenShift Route on: %s", opts.RouteName),
66-
"Do you want me to use it?")
66+
"Do you want me to use it?", opts.ioStreams.Out)
6767
if err != nil {
6868
return err
6969
}

0 commit comments

Comments
 (0)