bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/host/host.go (about)

     1  // Package host contains functionality for dealing with the machine which Bosun is running on.
     2  package host
     3  
     4  import (
     5  	"fmt"
     6  
     7  	"bosun.org/name"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  type host struct {
    12  	nameProcessor name.Processor
    13  	providedName  string
    14  	workingName   string
    15  }
    16  
    17  // Host is an interface which defines operations which can be performed against the machine which Bosun is running on.
    18  //
    19  // GetName returns the name of the host.
    20  //
    21  // SetName allows for the current host name to be overridden.
    22  //
    23  // GetNameProcessor returns the name.Processor that is associated with the host.
    24  //
    25  // SetNameProcessor allows for the name.Processor that is associated with the host to be overridden.
    26  type Host interface {
    27  	GetName() string
    28  	SetName(name string) error
    29  	GetNameProcessor() name.Processor
    30  	SetNameProcessor(np name.Processor) error
    31  }
    32  
    33  // NewHost constructs a new Host object
    34  func NewHost(name string, np name.Processor) (Host, error) {
    35  	if name == "" {
    36  		return nil, errors.New("No name provided")
    37  	}
    38  
    39  	if np == nil {
    40  		return nil, errors.New("No name processor provided")
    41  	}
    42  
    43  	host := &host{nameProcessor: np}
    44  	if err := host.SetName(name); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return host, nil
    49  }
    50  
    51  // GetNameProcessor returns the name.Processor that is associated with the host.
    52  func (h *host) GetNameProcessor() name.Processor {
    53  	return h.nameProcessor
    54  }
    55  
    56  // SetNameProcessor allows for the name.Processor that is associated with the host to be overridden.
    57  func (h *host) SetNameProcessor(np name.Processor) error {
    58  	h.nameProcessor = np
    59  	return h.SetName(h.providedName)
    60  }
    61  
    62  // GetName returns the name of the host.
    63  func (h *host) GetName() string {
    64  	return h.workingName
    65  }
    66  
    67  // SetName allows for the current host name to be overridden.
    68  func (h *host) SetName(name string) error {
    69  	if !h.nameProcessor.IsValid(name) {
    70  		return errors.New(fmt.Sprintf("Invalid hostname provided: '%s'", name))
    71  	}
    72  
    73  	providedName := name
    74  
    75  	if h.nameProcessor != nil {
    76  		var err error
    77  		name, err = h.nameProcessor.FormatName(name)
    78  		if err != nil {
    79  			return errors.Wrap(err, "Failed to set name")
    80  		}
    81  	}
    82  
    83  	// record the provided name so that we can reformat it later if the name.Processor changes
    84  	h.providedName = providedName
    85  	h.workingName = name
    86  
    87  	return nil
    88  }