github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/roachprod/install/nodes.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package install
    12  
    13  import (
    14  	"fmt"
    15  	"sort"
    16  	"strconv"
    17  	"strings"
    18  )
    19  
    20  func allNodes(total int) []int {
    21  	r := make([]int, total)
    22  	for i := range r {
    23  		r[i] = i + 1
    24  	}
    25  	return r
    26  }
    27  
    28  // ListNodes TODO(peter): document
    29  func ListNodes(s string, total int) ([]int, error) {
    30  	if s == "all" {
    31  		return allNodes(total), nil
    32  	}
    33  
    34  	m := map[int]bool{}
    35  	for _, p := range strings.Split(s, ",") {
    36  		parts := strings.Split(p, "-")
    37  		switch len(parts) {
    38  		case 1:
    39  			i, err := strconv.Atoi(parts[0])
    40  			if err != nil {
    41  				return nil, err
    42  			}
    43  			m[i] = true
    44  		case 2:
    45  			from, err := strconv.Atoi(parts[0])
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  			to, err := strconv.Atoi(parts[1])
    50  			if err != nil {
    51  				return nil, err
    52  			}
    53  			for i := from; i <= to; i++ {
    54  				m[i] = true
    55  			}
    56  		default:
    57  			return nil, fmt.Errorf("unable to parse nodes specification: %s", p)
    58  		}
    59  	}
    60  
    61  	r := make([]int, 0, len(m))
    62  	for i := range m {
    63  		r = append(r, i)
    64  	}
    65  	sort.Ints(r)
    66  	return r, nil
    67  }