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

Add bug command #921

Merged
merged 2 commits into from Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/ISSUE_TEMPLATE
@@ -0,0 +1,13 @@
### What version of Traefik are you using (`traefik version`)?


### What is your environment & configuration (arguments, toml...)?


### What did you do?


### What did you expect to see?


### What did you see instead?
110 changes: 110 additions & 0 deletions cmd/bug.go
@@ -0,0 +1,110 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"github.com/containous/flaeg"
"github.com/mvdan/xurls"
"net/url"
"os/exec"
"regexp"
"runtime"
"text/template"
)

var (
bugtracker = "https://github.com/containous/traefik/issues/new"
bugTemplate = `### What version of Traefik are you using?
` + "```" + `
{{.Version}}
` + "```" + `

### What is your environment & configuration (arguments, toml...)?
` + "```" + `
{{.Configuration}}
` + "```" + `

### What did you do?


### What did you expect to see?


### What did you see instead?
`
)

// NewBugCmd builds a new Bug command
func NewBugCmd(traefikConfiguration interface{}, traefikPointersConfiguration interface{}) *flaeg.Command {

//version Command init
return &flaeg.Command{
Name: "bug",
Description: `Report an issue on Traefik bugtracker`,
Config: traefikConfiguration,
DefaultPointersConfig: traefikPointersConfiguration,
Run: func() error {
var version bytes.Buffer
if err := getVersionPrint(&version); err != nil {
return err
}

tmpl, err := template.New("").Parse(bugTemplate)
if err != nil {
return err
}

configJSON, err := json.MarshalIndent(traefikConfiguration, "", " ")
if err != nil {
return err
}

v := struct {
Version string
Configuration string
}{
Version: version.String(),
Configuration: anonymize(string(configJSON)),
}

var bug bytes.Buffer
if err := tmpl.Execute(&bug, v); err != nil {
return err
}

body := bug.String()
url := bugtracker + "?body=" + url.QueryEscape(body)
if err := openBrowser(url); err != nil {
fmt.Print("Please file a new issue at " + bugtracker + " using this template:\n\n")
fmt.Print(body)
}

return nil
},
Metadata: map[string]string{
"parseAllSources": "true",
},
}
}

func openBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could print a link to click on that would "pre-populate" stuff for it though right ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😝

}
return err
}

func anonymize(input string) string {
replace := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
mailExp := regexp.MustCompile(`\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3}"`)
return xurls.Relaxed.ReplaceAllString(mailExp.ReplaceAllString(input, replace), replace)
}
62 changes: 62 additions & 0 deletions cmd/version.go
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"github.com/containous/flaeg"
"github.com/containous/traefik/version"
"io"
"os"
"runtime"
"text/template"
)

var versionTemplate = `Version: {{.Version}}
Codename: {{.Codename}}
Go version: {{.GoVersion}}
Built: {{.BuildTime}}
OS/Arch: {{.Os}}/{{.Arch}}`

// NewVersionCmd builds a new Version command
func NewVersionCmd() *flaeg.Command {

//version Command init
return &flaeg.Command{
Name: "version",
Description: `Print version`,
Config: struct{}{},
DefaultPointersConfig: struct{}{},
Run: func() error {
if err := getVersionPrint(os.Stdout); err != nil {
return err
}
fmt.Printf("\n")
return nil

},
}
}

func getVersionPrint(wr io.Writer) error {
tmpl, err := template.New("").Parse(versionTemplate)
if err != nil {
return err
}

v := struct {
Version string
Codename string
GoVersion string
BuildTime string
Os string
Arch string
}{
Version: version.Version,
Codename: version.Codename,
GoVersion: runtime.Version(),
BuildTime: version.BuildDate,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
}

return tmpl.Execute(wr, v)
}
47 changes: 25 additions & 22 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion glide.yaml
Expand Up @@ -112,4 +112,5 @@ import:
subpackages:
- daemon
- package: github.com/google/go-github
- package: github.com/hashicorp/go-version
- package: github.com/hashicorp/go-version
- package: github.com/mvdan/xurls
9 changes: 8 additions & 1 deletion stats.go → middlewares/stats.go
@@ -1,4 +1,4 @@
package main
package middlewares

import (
"net/http"
Expand All @@ -16,6 +16,13 @@ type StatsRecorder struct {
recentErrors []*statsError
}

// NewStatsRecorder returns a new StatsRecorder
func NewStatsRecorder(numRecentErrors int) *StatsRecorder {
return &StatsRecorder{
numRecentErrors: numRecentErrors,
}
}

// Stats includes all of the stats gathered by the recorder.
type Stats struct {
RecentErrors []*statsError `json:"recent_errors"`
Expand Down