Unleash/unleash-client-go

View on GitHub
internal/strategies/application_hostname.go

Summary

Maintainability
A
0 mins
Test Coverage
B
84%
package strategies

import (
    "github.com/Unleash/unleash-client-go/v3/context"
    "github.com/Unleash/unleash-client-go/v3/strategy"
    "strings"
)

type applicationHostnameStrategy struct {
    hostname string
}

func NewApplicationHostnameStrategy() *applicationHostnameStrategy {
    hostname, _ := resolveHostname()
    return &applicationHostnameStrategy{
        strings.ToLower(hostname),
    }
}

func (s applicationHostnameStrategy) Name() string {
    return "applicationHostname"
}

func (s applicationHostnameStrategy) IsEnabled(params map[string]interface{}, _ *context.Context) bool {
    value, found := params[strategy.ParamHostNames]
    if !found {
        return false
    }

    hostnames, ok := value.(string)
    if !ok {
        return false
    }

    for _, h := range strings.Split(hostnames, ",") {
        if strings.ToLower(h) == s.hostname {
            return true
        }
    }
    return false
}