From 97bf4acab1b5d4d44d02ab9fc88fdde723cd5a75 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 30 Sep 2022 21:50:03 +0200 Subject: [PATCH] Add discuss command (#4659) --- cmd/discuss.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/discuss.tpl | 26 +++++++++++++++++ go.mod | 1 + go.sum | 3 ++ 4 files changed, 106 insertions(+) create mode 100644 cmd/discuss.go create mode 100644 cmd/discuss.tpl diff --git a/cmd/discuss.go b/cmd/discuss.go new file mode 100644 index 000000000..1c3bb4f8f --- /dev/null +++ b/cmd/discuss.go @@ -0,0 +1,76 @@ +//go:build !windows + +package cmd + +import ( + "bytes" + _ "embed" + "net/url" + "os" + "path/filepath" + "text/template" + + "github.com/Masterminds/sprig/v3" + "github.com/evcc-io/evcc/server" + "github.com/evcc-io/evcc/util" + "github.com/pkg/browser" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// discussCmd represents the discuss command +var discussCmd = &cobra.Command{ + Use: "discuss", + Short: "Request support at Github Discussions (https://github.com/evcc-io/evcc/discussions/categories/erste-hilfe)", + Run: runDiscuss, +} + +//go:embed discuss.tpl +var discussTmpl string + +func init() { + rootCmd.AddCommand(discussCmd) +} + +func errorString(err error) string { + if err != nil { + return err.Error() + } + return "" +} + +func runDiscuss(cmd *cobra.Command, args []string) { + util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels")) + log.INFO.Printf("evcc %s", server.FormattedVersion()) + + cfgErr := loadConfigFile(&conf) + + file, pathErr := filepath.Abs(cfgFile) + if pathErr != nil { + file = cfgFile + } + + var redacted string + if src, err := os.ReadFile(cfgFile); err == nil { + redacted = redact(string(src)) + } + + out := new(bytes.Buffer) + tmpl := template.Must(template.New("discuss").Funcs(sprig.FuncMap()).Parse(discussTmpl)) + + _ = tmpl.Execute(out, map[string]any{ + "CfgFile": file, + "CfgError": errorString(cfgErr), + "CfgContent": redacted, + "Version": server.FormattedVersion(), + }) + + body := out.String() + uri := "https://github.com/evcc-io/evcc/discussions/new?category=erste-hilfe&body=" + url.QueryEscape(body) + + if err := browser.OpenURL(uri); err != nil { + log.FATAL.Println("Could not open browser.") + log.FATAL.Println("Go to https://github.com/evcc-io/evcc/discussions/new?category=erste-hilfe and post the following:") + log.FATAL.Println(body) + } +} diff --git a/cmd/discuss.tpl b/cmd/discuss.tpl new file mode 100644 index 000000000..e2b2baa33 --- /dev/null +++ b/cmd/discuss.tpl @@ -0,0 +1,26 @@ + + + + +{{ if .CfgError -}} +Fehlermeldung: + +``` +{{ .CfgError }} +``` + +{{ end -}} + +{{ if .CfgContent -}} +
Konfiguration{{ if .CfgFile }} ({{ .CfgFile }}){{ end }} + +```yaml +{{ .CfgContent }} +``` + +
+{{ end -}} + +{{ if .Version -}} +Version: `{{ .Version }}` +{{ end -}} diff --git a/go.mod b/go.mod index 8a2261c52..8d77048cb 100644 --- a/go.mod +++ b/go.mod @@ -64,6 +64,7 @@ require ( github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20220510032225-4f9f17eaec4c github.com/olekukonko/tablewriter v0.0.5 github.com/philippseith/signalr v0.6.1-0.20220829124759-bd5ffb679356 + github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 github.com/prometheus/client_golang v1.13.0 github.com/prometheus/common v0.37.0 github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f diff --git a/go.sum b/go.sum index c793832be..1f66bc206 100644 --- a/go.sum +++ b/go.sum @@ -711,6 +711,8 @@ github.com/philippseith/signalr v0.6.1-0.20220829124759-bd5ffb679356 h1:YTrZVN3A github.com/philippseith/signalr v0.6.1-0.20220829124759-bd5ffb679356/go.mod h1:8tC01OFK/CNtKSZbwLOavvLYxmEMaQXOsY4Ut69QKM4= github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= +github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= +github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -1144,6 +1146,7 @@ golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=