71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"isle/bootstrap"
|
|
"isle/daemon/network"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
|
)
|
|
|
|
func networkLogger(
|
|
logger *mlog.Logger, creationParams bootstrap.CreationParams,
|
|
) *mlog.Logger {
|
|
return logger.WithNamespace("networks").WithNamespace(creationParams.Domain)
|
|
}
|
|
|
|
func pickNetwork(
|
|
ctx context.Context,
|
|
networkLoader network.Loader,
|
|
networks map[string]joinedNetwork,
|
|
) (
|
|
joinedNetwork, error,
|
|
) {
|
|
if len(networks) == 0 {
|
|
return joinedNetwork{}, ErrNoNetwork
|
|
}
|
|
|
|
networkSearchStr := getNetworkSearchStr(ctx)
|
|
if networkSearchStr == "" {
|
|
if len(networks) > 1 {
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
|
}
|
|
for _, network := range networks {
|
|
return network, nil
|
|
}
|
|
}
|
|
|
|
creationParams, err := networkLoader.Loadable(ctx)
|
|
if err != nil {
|
|
return joinedNetwork{}, fmt.Errorf("getting loadable networks: %w", err)
|
|
}
|
|
|
|
matchingNetworkIDs := make([]string, 0, len(networks))
|
|
for _, creationParam := range creationParams {
|
|
if networkSearchStr == "" || creationParam.Matches(networkSearchStr) {
|
|
matchingNetworkIDs = append(matchingNetworkIDs, creationParam.ID)
|
|
}
|
|
}
|
|
|
|
if len(matchingNetworkIDs) == 0 {
|
|
return joinedNetwork{}, ErrNoMatchingNetworks
|
|
} else if len(matchingNetworkIDs) > 1 {
|
|
return joinedNetwork{}, ErrMultipleMatchingNetworks
|
|
}
|
|
|
|
return networks[matchingNetworkIDs[0]], nil
|
|
}
|
|
|
|
func alreadyJoined(
|
|
networks map[string]joinedNetwork, creationParams bootstrap.CreationParams,
|
|
) bool {
|
|
for _, network := range networks {
|
|
if network.creationParams.Conflicts(creationParams) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|