github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/namespace/namespace.go (about)

     1  package namespace
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/tickoalcantara12/micro/v3/service/registry"
     8  	"github.com/tickoalcantara12/micro/v3/util/config"
     9  )
    10  
    11  const separator = ","
    12  
    13  // List the namespaces for an environment
    14  func List(env string) ([]string, error) {
    15  	if len(env) == 0 {
    16  		return nil, errors.New("Missing env value")
    17  	}
    18  
    19  	values, err := config.Get(config.Path("namespaces", env, "all"))
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	if len(values) == 0 {
    24  		return []string{registry.DefaultDomain}, nil
    25  	}
    26  
    27  	namespaces := strings.Split(values, separator)
    28  	return append([]string{registry.DefaultDomain}, namespaces...), nil
    29  }
    30  
    31  // Add a namespace to an environment
    32  func Add(namespace, env string) error {
    33  	if len(env) == 0 {
    34  		return errors.New("Missing env value")
    35  	}
    36  	if len(namespace) == 0 {
    37  		return errors.New("Missing namespace value")
    38  	}
    39  
    40  	existing, err := List(env)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	for _, ns := range existing {
    45  		if ns == namespace {
    46  			// the namespace already exists
    47  			return nil
    48  		}
    49  	}
    50  
    51  	values, _ := config.Get(config.Path("namespaces", env, "all"))
    52  	if len(values) > 0 {
    53  		values = strings.Join([]string{values, namespace}, separator)
    54  	} else {
    55  		values = namespace
    56  	}
    57  
    58  	return config.Set(config.Path("namespaces", env, "all"), values)
    59  }
    60  
    61  // Remove a namespace from an environment
    62  func Remove(namespace, env string) error {
    63  	if len(env) == 0 {
    64  		return errors.New("Missing env value")
    65  	}
    66  	if len(namespace) == 0 {
    67  		return errors.New("Missing namespace value")
    68  	}
    69  	if namespace == registry.DefaultDomain {
    70  		return errors.New("Cannot remove the default namespace")
    71  	}
    72  
    73  	current, err := Get(env)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	if current == namespace {
    78  		err = Set(registry.DefaultDomain, env)
    79  		if err != nil {
    80  			return err
    81  		}
    82  	}
    83  
    84  	existing, err := List(env)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	var namespaces []string
    90  	var found bool
    91  	for _, ns := range existing {
    92  		if ns == namespace {
    93  			found = true
    94  			continue
    95  		}
    96  		if ns == registry.DefaultDomain {
    97  			continue
    98  		}
    99  		namespaces = append(namespaces, ns)
   100  	}
   101  
   102  	if !found {
   103  		return errors.New("Namespace does not exists")
   104  	}
   105  
   106  	values := strings.Join(namespaces, separator)
   107  	return config.Set(config.Path("namespaces", env, "all"), values)
   108  }
   109  
   110  // Set the current namespace for an environment
   111  func Set(namespace, env string) error {
   112  	if len(env) == 0 {
   113  		return errors.New("Missing env value")
   114  	}
   115  	if len(namespace) == 0 {
   116  		return errors.New("Missing namespace value")
   117  	}
   118  
   119  	existing, err := List(env)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	var found bool
   125  	for _, ns := range existing {
   126  		if ns != namespace {
   127  			continue
   128  		}
   129  		found = true
   130  		break
   131  	}
   132  
   133  	if !found {
   134  		return errors.New("Namespace does not exists")
   135  	}
   136  
   137  	return config.Set(config.Path("namespaces", env, "current"), namespace)
   138  }
   139  
   140  // Get the current namespace for an environment
   141  func Get(env string) (string, error) {
   142  	if len(env) == 0 {
   143  		return "", errors.New("Missing env value")
   144  	}
   145  
   146  	if ns, err := config.Get(config.Path("namespaces", env, "current")); err != nil {
   147  		return "", err
   148  	} else if len(ns) > 0 {
   149  		return ns, nil
   150  	}
   151  
   152  	return registry.DefaultDomain, nil
   153  }