Skip to content

Commit 4a897c4

Browse files
committed
Print PRINT_CONFIG_ONLY in markdown so it can be used in github issues #507
1 parent 9af45d3 commit 4a897c4

File tree

3 files changed

+76
-24
lines changed

3 files changed

+76
-24
lines changed

cmd/print-config.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"os"
7+
"sort"
8+
"strings"
9+
"text/template"
10+
11+
"github.com/bakito/adguardhome-sync/pkg/types"
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
//go:embed print-config.md
16+
var printConfigTemplate string
17+
18+
func printConfig(cfg *types.Config, usedCfgFile string, cfgContent string) error {
19+
config, err := yaml.Marshal(cfg)
20+
if err != nil {
21+
logger.Error(err)
22+
return err
23+
}
24+
25+
t, err := template.New("printConfigTemplate").Parse(printConfigTemplate)
26+
if err != nil {
27+
return err
28+
}
29+
30+
env := os.Environ()
31+
sort.Strings(env)
32+
33+
var buf bytes.Buffer
34+
35+
if err = t.Execute(&buf, map[string]interface{}{
36+
"AggregatedConfig": string(config),
37+
"ConfigFilePath": usedCfgFile,
38+
"ConfigFileContent": cfgContent,
39+
"EnvironmentVariables": strings.Join(env, "\n"),
40+
}); err != nil {
41+
return err
42+
}
43+
44+
logger.Infof(
45+
"Printing adguardhome-sync aggregated config (THE APPLICATION WILL NOT START IN THIS MODE):\n%s",
46+
buf.String(),
47+
)
48+
49+
return nil
50+
}

cmd/print-config.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords and IPs when using this in an issue ) -->
2+
3+
### AdGuardHome sync aggregated config
4+
5+
```yaml
6+
{{ .AggregatedConfig }}
7+
```
8+
{{- if .ConfigFilePath }}
9+
### AdGuardHome sync unmodified config file
10+
11+
Config file path: {{ .ConfigFilePath }}
12+
13+
```yaml
14+
{{ .ConfigFileContent }}
15+
```
16+
{{- end }}
17+
18+
### Environment Variables
19+
20+
```ini
21+
{{ .EnvironmentVariables }}
22+
```
23+
24+
<!-- END OF GITHUB ISSUE CONTENT -->

cmd/run.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package cmd
22

33
import (
4-
"os"
5-
"sort"
6-
"strings"
7-
84
"github.com/bakito/adguardhome-sync/pkg/config"
95
"github.com/bakito/adguardhome-sync/pkg/log"
106
"github.com/bakito/adguardhome-sync/pkg/sync"
117
"github.com/spf13/cobra"
12-
"gopkg.in/yaml.v3"
138
)
149

1510
// runCmd represents the run command
@@ -31,28 +26,11 @@ var doCmd = &cobra.Command{
3126
}
3227

3328
if cfg.PrintConfigOnly {
34-
config, err := yaml.Marshal(cfg)
35-
if err != nil {
29+
if err := printConfig(cfg, usedCfgFile, cfgContent); err != nil {
30+
3631
logger.Error(err)
3732
return err
3833
}
39-
logger.Infof(
40-
"Printing adguardhome-sync aggregated config (THE APPLICATION WILL NOT START IN THIS MODE):\n"+
41-
"# adguardhome-sync aggregated config\n%s",
42-
string(config),
43-
)
44-
45-
if cfgContent != "" {
46-
logger.Infof(
47-
"Printing adguardhome-sync config file:\n# adguardhome-sync config file: %s\n%s",
48-
usedCfgFile,
49-
cfgContent,
50-
)
51-
}
52-
53-
env := os.Environ()
54-
sort.Strings(env)
55-
logger.Infof("Printing adguardhome-sync environment variables: \n%s", strings.Join(env, "\n"))
5634

5735
return nil
5836
}

0 commit comments

Comments
 (0)