Compare commits

..

1 Commits

Author SHA1 Message Date
amery f7da9519fa zones: allow iterators to terminate
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 20:12:05 +00:00
4 changed files with 13 additions and 44 deletions
-27
View File
@@ -1,27 +0,0 @@
package main
import (
"os"
"github.com/spf13/cobra"
"git.jpi.io/amery/jpictl/pkg/zones"
)
// Command
var envCmd = &cobra.Command{
Use: "env",
Short: "generates environment variables for shell scripts",
RunE: func(_ *cobra.Command, _ []string) error {
m, err := zones.New(cfg.Base, cfg.Domain)
if err != nil {
return err
}
return m.WriteEnv(os.Stdout)
},
}
func init() {
rootCmd.AddCommand(envCmd)
}
-9
View File
@@ -1,9 +0,0 @@
// Package zones abstracts the cluster zones
package zones
import "io"
// WriteEnv generates environment variables for shell scripts
func (*Zones) WriteEnv(io.Writer) error {
return nil
}
+3 -4
View File
@@ -31,10 +31,9 @@ func (m *Zones) scan() error {
func (m *Zones) scanMachines() error { func (m *Zones) scanMachines() error {
var err error var err error
m.ForEachMachine(func(p *Machine) { m.ForEachMachine(func(p *Machine) bool {
if err == nil { err = p.scan()
err = p.scan() return err != nil
}
}) })
return err return err
} }
+10 -4
View File
@@ -32,18 +32,24 @@ type Zones struct {
} }
// ForEachMachine calls a function for each Machine in the cluster // ForEachMachine calls a function for each Machine in the cluster
func (m *Zones) ForEachMachine(fn func(*Machine)) { func (m *Zones) ForEachMachine(fn func(*Machine) bool) {
for _, z := range m.Zones { for _, z := range m.Zones {
for _, p := range z.Machines { for _, p := range z.Machines {
fn(p) if fn(p) {
// terminate
return
}
} }
} }
} }
// ForEachZone calls a function for each Zone in the cluster // ForEachZone calls a function for each Zone in the cluster
func (m *Zones) ForEachZone(fn func(*Zone)) { func (m *Zones) ForEachZone(fn func(*Zone) bool) {
for _, p := range m.Zones { for _, p := range m.Zones {
fn(p) if fn(p) {
// terminate
return
}
} }
} }