github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/sharedaction/check_target.go (about)

     1  package sharedaction
     2  
     3  // NotLoggedInError represents the scenario when the user is not logged in.
     4  type NotLoggedInError struct {
     5  	BinaryName string
     6  }
     7  
     8  func (NotLoggedInError) Error() string {
     9  	// The error message will be replaced by a translated message, returning the
    10  	// empty string does not add to the translation files.
    11  	return ""
    12  }
    13  
    14  // NoOrganizationTargetedError represents the scenario when an org is not targeted.
    15  type NoOrganizationTargetedError struct {
    16  	BinaryName string
    17  }
    18  
    19  func (NoOrganizationTargetedError) Error() string {
    20  	// The error message will be replaced by a translated message, returning the
    21  	// empty string does not add to the translation files.
    22  	return ""
    23  }
    24  
    25  // NoSpaceTargetedError represents the scenario when a space is not targeted.
    26  type NoSpaceTargetedError struct {
    27  	BinaryName string
    28  }
    29  
    30  func (NoSpaceTargetedError) Error() string {
    31  	// The error message will be replaced by a translated message, returning the
    32  	// empty string does not add to the translation files.
    33  	return ""
    34  }
    35  
    36  // CheckTarget confirms that the user is logged in. Optionally it will also
    37  // check if an organization and space are targeted.
    38  func (Actor) CheckTarget(config Config, targetedOrganizationRequired bool, targetedSpaceRequired bool) error {
    39  	if config.AccessToken() == "" && config.RefreshToken() == "" {
    40  		return NotLoggedInError{
    41  			BinaryName: config.BinaryName(),
    42  		}
    43  	}
    44  
    45  	if targetedOrganizationRequired {
    46  		if !config.HasTargetedOrganization() {
    47  			return NoOrganizationTargetedError{
    48  				BinaryName: config.BinaryName(),
    49  			}
    50  		}
    51  
    52  		if targetedSpaceRequired {
    53  			if !config.HasTargetedSpace() {
    54  				return NoSpaceTargetedError{
    55  					BinaryName: config.BinaryName(),
    56  				}
    57  			}
    58  		}
    59  	}
    60  
    61  	return nil
    62  }