Compare commits

..

1 Commits

Author SHA1 Message Date
amery fb82a7f358 Merge pull request 'zones: fix PruneWireguardConfig recursion' (#1)
Reviewed-on: #1
2023-08-28 16:47:13 +02:00
2 changed files with 13 additions and 44 deletions
+1 -10
View File
@@ -16,19 +16,10 @@ var envCmd = &cobra.Command{
return err
}
_, err = m.Env(*envExport).WriteTo(os.Stdout)
return err
return m.WriteEnv(os.Stdout)
},
}
// Command Flags
var (
envExport *bool
)
func init() {
rootCmd.AddCommand(envCmd)
envExport = envCmd.PersistentFlags().BoolP("export", "e", false,
"export generated variables")
}
+12 -34
View File
@@ -7,23 +7,8 @@ import (
"strings"
)
// Env is a shell environment factory for this cluster
type Env struct {
ZoneIterator
export bool
}
// Env returns a shell environment factory
func (m *Zones) Env(export bool) *Env {
return &Env{
ZoneIterator: m,
export: export,
}
}
// WriteTo generates environment variables for shell scripts
func (m *Env) WriteTo(w io.Writer) (int64, error) {
// WriteEnv generates environment variables for shell scripts
func (m *Zones) WriteEnv(w io.Writer) error {
var buf bytes.Buffer
m.writeEnvVarFn(&buf, genEnvZones, "ZONES")
@@ -32,10 +17,11 @@ func (m *Env) WriteTo(w io.Writer) (int64, error) {
return false
})
return buf.WriteTo(w)
_, err := buf.WriteTo(w)
return err
}
func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
func (m *Zones) writeEnvZone(w io.Writer, z *Zone) {
zoneID := z.ID
// ZONE{zoneID}
@@ -56,7 +42,7 @@ func (m *Env) writeEnvZone(w io.Writer, z *Zone) {
}
}
func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args ...any) {
func (m *Zones) writeEnvVarFn(w io.Writer, fn func(*Zones) string, name string, args ...any) {
var value string
if fn != nil {
@@ -66,13 +52,7 @@ func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args
m.writeEnvVar(w, value, name, args...)
}
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
var prefix string
if m.export {
prefix = "export "
}
func (*Zones) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
@@ -80,17 +60,15 @@ func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if name != "" {
value = strings.TrimSpace(value)
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
}
}
func genEnvZones(m *Env) string {
var s []string
m.ForEachZone(func(z *Zone) bool {
func genEnvZones(m *Zones) string {
s := make([]string, 0, len(m.Zones))
for _, z := range m.Zones {
s = append(s, fmt.Sprintf("%v", z.ID))
return false
})
}
return strings.Join(s, " ")
}