Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add feedback command #969

Merged
merged 6 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/scw/testdata/test-all-usage-feedback-bug-usage.stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Send a bug-report to the Scaleway CLI team.

USAGE:
scw feedback bug

FLAGS:
-h, --help help for bug

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Send a feature request to the Scaleway CLI team.

USAGE:
scw feedback feature

FLAGS:
-h, --help help for feature

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use
18 changes: 18 additions & 0 deletions cmd/scw/testdata/test-all-usage-feedback-usage.stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Send feedback to the Scaleway CLI Team!

USAGE:
scw feedback <command>

AVAILABLE COMMANDS:
bug Send a bug-report
feature Send a feature request

FLAGS:
-h, --help help for feedback

GLOBAL FLAGS:
-D, --debug Enable debug mode
-o, --output string Output format: json or human
-p, --profile string The config profile to use

Use "scw feedback [command] --help" for more information about a command.
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-main-usage-usage.stderr.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ AVAILABLE COMMANDS:
object Object-storage utils
version Display cli version
registry Docker registry API
feedback Send feedback to the Scaleway CLI Team!
help Help about any command

FLAGS:
Expand Down
91 changes: 91 additions & 0 deletions internal/namespaces/feedback/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package feedback

import (
"context"
"fmt"
"log"
"reflect"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
)

func GetCommands() *core.Commands {
return core.NewCommands(
feedbackRoot(),
feedbackBugCommand(),
feedbackFeatureRequestCommand(),
)
}

func feedbackRoot() *core.Command {
return &core.Command{
Short: `Send feedback to the Scaleway CLI Team!`,
Namespace: "feedback",
ArgsType: reflect.TypeOf(struct{}{}),
ArgSpecs: core.ArgSpecs{},
}
}

func feedbackBugCommand() *core.Command {
return &core.Command{
Short: `Send a bug-report`,
Long: `Send a bug-report to the Scaleway CLI team.`,
Namespace: "feedback",
Resource: `bug`,
ArgsType: reflect.TypeOf(struct{}{}),
ArgSpecs: core.ArgSpecs{},
NoClient: true,

Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
buildInfoStr, err := human.Marshal(core.ExtractBuildInfo(ctx), nil)
if err != nil {
return nil, err
}
issue := issue{
IssueTemplate: bug,
BuildInfoStr: buildInfoStr,
}
err = issue.openInBrowser(ctx)
if err != nil {
log.Fatal(err)
remyleone marked this conversation as resolved.
Show resolved Hide resolved
}
return &core.SuccessResult{
Message: "Successfully opened the page",
Details: fmt.Sprintf(" (%s)", issue.getURL()),
}, nil
},

}
}

func feedbackFeatureRequestCommand() *core.Command {
return &core.Command{
Short: `Send a feature request`,
Long: `Send a feature request to the Scaleway CLI team.`,
Namespace: "feedback",
Resource: `feature`,
ArgsType: reflect.TypeOf(struct{}{}),
ArgSpecs: core.ArgSpecs{},
NoClient: true,
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
buildInfoStr, err := human.Marshal(core.ExtractBuildInfo(ctx), nil)
if err != nil {
return nil, err
}
issue := issue{
IssueTemplate: feature,
BuildInfoStr: buildInfoStr,
}
err = issue.openInBrowser(ctx)
if err != nil {
log.Fatal(err)
remyleone marked this conversation as resolved.
Show resolved Hide resolved
}
return &core.SuccessResult{
Message: "Successfully opened the page",
Details: fmt.Sprintf(" (%s)", issue.getURL()),
}, nil
},
}
}

81 changes: 81 additions & 0 deletions internal/namespaces/feedback/custom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package feedback

import (
"os/exec"
"runtime"
"testing"

"github.com/alecthomas/assert"
"github.com/hashicorp/go-version"
"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_FeedbackBug(t *testing.T) {
t.Run("simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw feedback bug",
BuildInfo: core.BuildInfo{
Version: version.Must(version.NewSemver("v0.0.0")),
BuildDate: "unknown",
GoVersion: "runtime.Version()",
GitBranch: "unknown",
GitCommit: "unknown",
GoArch: "runtime.GOARCH",
GoOS: "runtime.GOOS",
},
OverrideExec: func(ctx *core.ExecFuncCtx, cmd *exec.Cmd) (exitCode int, err error) {
var observed string
switch runtime.GOOS {
case "windows":
// 0: "rundll32", 1: "url.dll,FileProtocolHandler" 2: url
observed = cmd.Args[2]
default:
observed = cmd.Args[1]
}
assert.Equal(t,
"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0Aversion+++++0.0.0%0Abuild-date++unknown%0Ago-version++runtime.Version%28%29%0Agit-branch++unknown%0Agit-commit++unknown%0Ago-arch+++++runtime.GOARCH%0Ago-os+++++++runtime.GOOS%0A&issueTemplate=bug_report.md&labels=bug",
observed)

return 0, nil
},
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))
}

func Test_FeedbackFeature(t *testing.T) {
t.Run("simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw feedback feature",
BuildInfo: core.BuildInfo{
Version: version.Must(version.NewSemver("v0.0.0")),
BuildDate: "unknown",
GoVersion: "runtime.Version()",
GitBranch: "unknown",
GitCommit: "unknown",
GoArch: "runtime.GOARCH",
GoOS: "runtime.GOOS",
},
OverrideExec: func(ctx *core.ExecFuncCtx, cmd *exec.Cmd) (exitCode int, err error) {
var observed string
switch runtime.GOOS {
case "windows":
// 0: "rundll32", 1: "url.dll,FileProtocolHandler" 2: url
observed = cmd.Args[2]
default:
observed = cmd.Args[1]
}
assert.Equal(t,
"https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0Aversion+++++0.0.0%0Abuild-date++unknown%0Ago-version++runtime.Version%28%29%0Agit-branch++unknown%0Agit-commit++unknown%0Ago-arch+++++runtime.GOARCH%0Ago-os+++++++runtime.GOOS%0A&issueTemplate=feature_request.md&labels=enhancement",
observed)

return 0, nil
},
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))
}
121 changes: 121 additions & 0 deletions internal/namespaces/feedback/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package feedback

import (
"bytes"
"context"
"fmt"
"html/template"
"log"
"net/url"
"os/exec"
"runtime"

"github.com/scaleway/scaleway-cli/internal/core"
)

const githubURL = "https://github.com/scaleway/scaleway-cli/issues/new"

type issueTemplate string

const (
bug = issueTemplate("bug")
feature = issueTemplate("feature")
)

type issue struct {
IssueTemplate issueTemplate
BuildInfoStr string
remyleone marked this conversation as resolved.
Show resolved Hide resolved
}

const bugBodyTemplate = `
## Description:

## How to reproduce:

### Command attempted

### Expected Behavior

### Actual Behavior

## More info

## Version

{{ .BuildInfoStr }}
`

const featureBodyTemplate = `
## Description

## How this functionality would be exposed

## References

## Version

{{ .BuildInfoStr }}
`

func (i issue) getURL() string {
u, err := url.Parse(githubURL)
if err != nil {
log.Fatal(err)
}

params := url.Values{}
switch i.IssueTemplate {
case feature:
params.Add("labels", "enhancement")
params.Add("issueTemplate", "feature_request.md")
renderedBody, _ := i.renderTemplate(featureBodyTemplate)
params.Add("body", renderedBody)
case bug:
params.Add("labels", "bug")
params.Add("issueTemplate", "bug_report.md")
renderedBody, _ := i.renderTemplate(bugBodyTemplate)
params.Add("body", renderedBody)
}

u.RawQuery = params.Encode()

return u.String()
}

func (i issue) openInBrowser(ctx context.Context) error {
var err error
var openCmd *exec.Cmd

switch runtime.GOOS {
case "linux":
openCmd = exec.Command("xdg-open", i.getURL())
case "windows":
openCmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", i.getURL())
case "darwin":
openCmd = exec.Command("open", i.getURL())
default:
err = fmt.Errorf("unsupported platform")
}

exitCode, err := core.ExecCmd(ctx, openCmd)
if exitCode != 0 {
return &core.CliError{Empty: true, Code: exitCode}
}

return err
}

func (i issue) renderTemplate(bodyTemplate string) (string, error) {
tmpl, err := template.New("configuration").Parse(bodyTemplate)
if err != nil {
return "", err
}

var buf bytes.Buffer
err = tmpl.Execute(&buf, i)
if err != nil {
return "", err
}

return buf.String(), nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
✅ Successfully opened the page. (https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%3A%0A%0A%23%23+How+to+reproduce%3A%0A%0A%23%23%23+Command+attempted%0A%0A%23%23%23+Expected+Behavior%0A%0A%23%23%23+Actual+Behavior%0A%0A%23%23+More+info%0A%0A%23%23+Version%0A%0Aversion+++++0.0.0%0Abuild-date++unknown%0Ago-version++runtime.Version%28%29%0Agit-branch++unknown%0Agit-commit++unknown%0Ago-arch+++++runtime.GOARCH%0Ago-os+++++++runtime.GOOS%0A&issueTemplate=bug_report.md&labels=bug)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
✅ Successfully opened the page. (https://github.com/scaleway/scaleway-cli/issues/new?body=%0A%23%23+Description%0A%0A%23%23+How+this+functionality+would+be+exposed%0A%0A%23%23+References%0A%0A%23%23+Version%0A%0Aversion+++++0.0.0%0Abuild-date++unknown%0Ago-version++runtime.Version%28%29%0Agit-branch++unknown%0Agit-commit++unknown%0Ago-arch+++++runtime.GOARCH%0Ago-os+++++++runtime.GOOS%0A&issueTemplate=feature_request.md&labels=enhancement)
2 changes: 2 additions & 0 deletions internal/namespaces/get_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
autocompleteNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/autocomplete"
baremetal "github.com/scaleway/scaleway-cli/internal/namespaces/baremetal/v1alpha1"
configNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/config"
"github.com/scaleway/scaleway-cli/internal/namespaces/feedback"
initNamespace "github.com/scaleway/scaleway-cli/internal/namespaces/init"
"github.com/scaleway/scaleway-cli/internal/namespaces/instance/v1"
k8s "github.com/scaleway/scaleway-cli/internal/namespaces/k8s/v1"
Expand Down Expand Up @@ -33,5 +34,6 @@ func GetCommands() *core.Commands {
commands.Merge(object.GetCommands())
commands.Merge(versionNamespace.GetCommands())
commands.Merge(registry.GetCommands())
commands.Merge(feedback.GetCommands())
return commands
}