feat: print runtime info in config only mode

This commit is contained in:
bakito
2025-03-09 10:19:43 +01:00
parent 7c2018acbc
commit 8ef1eb8c2a
3 changed files with 60 additions and 9 deletions
+38 -3
View File
@@ -4,10 +4,14 @@ import (
"bytes"
_ "embed"
"os"
"runtime"
"sort"
"strings"
"text/template"
"github.com/bakito/adguardhome-sync/pkg/client"
"github.com/bakito/adguardhome-sync/pkg/types"
"github.com/bakito/adguardhome-sync/version"
"gopkg.in/yaml.v3"
)
@@ -15,7 +19,13 @@ import (
var printConfigTemplate string
func (ac *AppConfig) Print() error {
out, err := ac.print(os.Environ())
originVersion := aghVersion(ac.cfg.Origin)
var replicaVersions []string
for _, replica := range ac.cfg.Replicas {
replicaVersions = append(replicaVersions, aghVersion(replica))
}
out, err := ac.print(os.Environ(), originVersion, replicaVersions)
if err != nil {
return err
}
@@ -28,13 +38,32 @@ func (ac *AppConfig) Print() error {
return nil
}
func (ac *AppConfig) print(env []string) (string, error) {
func aghVersion(i types.AdGuardInstance) string {
cl, err := client.New(i)
if err != nil {
return "N/A"
}
stats, err := cl.Status()
if err != nil {
return "N/A"
}
return stats.Version
}
func (ac *AppConfig) print(env []string, originVersion string, replicaVersions []string) (string, error) {
config, err := yaml.Marshal(ac.Get())
if err != nil {
return "", err
}
t, err := template.New("printConfigTemplate").Parse(printConfigTemplate)
funcMap := template.FuncMap{
// The name "inc" is what the function will be called in the template text.
"inc": func(i int) int {
return i + 1
},
}
t, err := template.New("printConfigTemplate").Funcs(funcMap).Parse(printConfigTemplate)
if err != nil {
return "", err
}
@@ -44,10 +73,16 @@ func (ac *AppConfig) print(env []string) (string, error) {
var buf bytes.Buffer
if err = t.Execute(&buf, map[string]interface{}{
"Version": version.Version,
"Build": version.Build,
"OperatingSystem": runtime.GOOS,
"Architecture": runtime.GOARCH,
"AggregatedConfig": string(config),
"ConfigFilePath": ac.filePath,
"ConfigFileContent": ac.content,
"EnvironmentVariables": strings.Join(env, "\n"),
"OriginVersion": originVersion,
"ReplicaVersions": replicaVersions,
}); err != nil {
return "", err
}
+12
View File
@@ -1,5 +1,17 @@
<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords, IPs and other sensitive information when using this in an issue ) -->
### Runtime
AdguardHome-Sync Version: {{ .Version }}
Build: {{ .Build }}
OperatingSystem: {{ .OperatingSystem }}
Architecture: {{ .Architecture }}
OriginVersion: {{ .OriginVersion }}
ReplicaVersions:
{{- range $i,$rep := .ReplicaVersions }}
- Replica {{ inc $i }}: {{ $rep }}
{{- end }}
### AdGuardHome sync aggregated config
```yaml
+10 -6
View File
@@ -1,7 +1,11 @@
package config
import (
"fmt"
"runtime"
"github.com/bakito/adguardhome-sync/pkg/types"
"github.com/bakito/adguardhome-sync/version"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
@@ -27,20 +31,20 @@ origin:
})
Context("print", func() {
It("should print config without file", func() {
out, err := ac.print(env)
out, err := ac.print(env, "v0.0.1", []string{"v0.0.2"})
Ω(err).ShouldNot(HaveOccurred())
Ω(out).Should(Equal(expected1))
Ω(out).Should(Equal(fmt.Sprintf(expected1, version.Version, version.Build, runtime.GOOS, runtime.GOARCH)))
})
It("should print config with file", func() {
ac.filePath = "config.yaml"
out, err := ac.print(env)
out, err := ac.print(env, "v0.0.1", []string{"v0.0.2"})
Ω(err).ShouldNot(HaveOccurred())
Ω(out).Should(Equal(expected2))
Ω(out).Should(Equal(fmt.Sprintf(expected2, version.Version, version.Build, runtime.GOOS, runtime.GOARCH)))
})
})
})
const (
expected1 = "<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords, IPs and other sensitive information when using this in an issue ) -->\n\n### AdGuardHome sync aggregated config\n\n```yaml\norigin:\n url: https://ha.xxxx.net:3000\n webURL: \"\"\n insecureSkipVerify: false\n autoSetup: false\n\n```\n\n### Environment Variables\n\n```ini\nBAR=bar\nFOO=foo\n```\n\n<!-- END OF GITHUB ISSUE CONTENT -->"
expected2 = "<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords, IPs and other sensitive information when using this in an issue ) -->\n\n### AdGuardHome sync aggregated config\n\n```yaml\norigin:\n url: https://ha.xxxx.net:3000\n webURL: \"\"\n insecureSkipVerify: false\n autoSetup: false\n\n```\n### AdGuardHome sync unmodified config file\n\nConfig file path: config.yaml\n\n```yaml\n\norigin:\n url: https://ha.xxxx.net:3000\n\n```\n\n### Environment Variables\n\n```ini\nBAR=bar\nFOO=foo\n```\n\n<!-- END OF GITHUB ISSUE CONTENT -->"
expected1 = "<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords, IPs and other sensitive information when using this in an issue ) -->\n\n### Runtime\n\nAdguardHome-Sync Version: %s\nBuild: %s\nOperatingSystem: %s\nArchitecture: %s\nOriginVersion: v0.0.1\nReplicaVersions:\n- Replica 1: v0.0.2\n\n### AdGuardHome sync aggregated config\n\n```yaml\norigin:\n url: https://ha.xxxx.net:3000\n webURL: \"\"\n insecureSkipVerify: false\n autoSetup: false\n\n```\n\n### Environment Variables\n\n```ini\nBAR=bar\nFOO=foo\n```\n\n<!-- END OF GITHUB ISSUE CONTENT -->"
expected2 = "<!-- PLEASE COPY THE FOLLOWING OUTPUT AS IS INTO THE GITHUB ISSUE (Don't forget to mask your usernames, passwords, IPs and other sensitive information when using this in an issue ) -->\n\n### Runtime\n\nAdguardHome-Sync Version: %s\nBuild: %s\nOperatingSystem: %s\nArchitecture: %s\nOriginVersion: v0.0.1\nReplicaVersions:\n- Replica 1: v0.0.2\n\n### AdGuardHome sync aggregated config\n\n```yaml\norigin:\n url: https://ha.xxxx.net:3000\n webURL: \"\"\n insecureSkipVerify: false\n autoSetup: false\n\n```\n### AdGuardHome sync unmodified config file\n\nConfig file path: config.yaml\n\n```yaml\n\norigin:\n url: https://ha.xxxx.net:3000\n\n```\n\n### Environment Variables\n\n```ini\nBAR=bar\nFOO=foo\n```\n\n<!-- END OF GITHUB ISSUE CONTENT -->"
)