github.com/sap/cf-mta-plugin@v2.6.3+incompatible/util/builders.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/url"
     7  	"strings"
     8  
     9  	"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
    10  )
    11  
    12  // ProcessBuilder a builder for models.Process instances
    13  type ProcessBuilder struct {
    14  	operation models.Operation
    15  }
    16  
    17  // NewProcessBuilder creates a new process builder
    18  func NewProcessBuilder() *ProcessBuilder {
    19  	return &ProcessBuilder{operation: models.Operation{Parameters: make(map[string]interface{})}}
    20  }
    21  
    22  func (pb *ProcessBuilder) ProcessType(processType string) *ProcessBuilder {
    23  	pb.operation.ProcessType = processType
    24  	return pb
    25  }
    26  
    27  func (pb *ProcessBuilder) Namespace(namespace string) *ProcessBuilder {
    28  	pb.operation.Namespace = namespace
    29  	return pb
    30  }
    31  
    32  // Parameter adds a parameter to the process if it is set
    33  func (pb *ProcessBuilder) Parameter(parameterID string, value string) *ProcessBuilder {
    34  	if value != "" {
    35  		pb.operation.Parameters[parameterID] = value
    36  	}
    37  	return pb
    38  }
    39  
    40  // SetParameterWithoutCheck sets the parameter without checking whether it is null
    41  func (pb *ProcessBuilder) SetParameterWithoutCheck(parameterID string, value string) *ProcessBuilder {
    42  	pb.operation.Parameters[parameterID] = value
    43  	return pb
    44  }
    45  
    46  // Build builds the process
    47  func (pb *ProcessBuilder) Build() *models.Operation {
    48  	return &pb.operation
    49  }
    50  
    51  const hostAndSchemeSeparator = "://"
    52  
    53  type UriBuilder struct {
    54  	host   string
    55  	scheme string
    56  	path   string
    57  }
    58  
    59  func NewUriBuilder() *UriBuilder {
    60  	return &UriBuilder{host: "", scheme: "", path: ""}
    61  }
    62  
    63  func (builder *UriBuilder) SetScheme(scheme string) *UriBuilder {
    64  	builder.scheme = scheme
    65  	return builder
    66  }
    67  
    68  func (builder *UriBuilder) SetHost(host string) *UriBuilder {
    69  	builder.host = host
    70  	return builder
    71  }
    72  
    73  func (builder *UriBuilder) SetPath(path string) *UriBuilder {
    74  	builder.path = path
    75  	return builder
    76  }
    77  
    78  func (builder *UriBuilder) Build() (string, error) {
    79  	if builder.scheme == "" || builder.host == "" {
    80  		return "", fmt.Errorf("The host or scheme could not be empty")
    81  	}
    82  	stringBuilder := bytes.Buffer{}
    83  	stringBuilder.WriteString(builder.scheme)
    84  	stringBuilder.WriteString(hostAndSchemeSeparator)
    85  	stringBuilder.WriteString(builder.host)
    86  	stringBuilder.WriteString(getPath(builder.path))
    87  	builtUrl := stringBuilder.String()
    88  	parsedUrl, err := url.Parse(builtUrl)
    89  	if err != nil {
    90  		return "", err
    91  	}
    92  	return parsedUrl.String(), nil
    93  }
    94  
    95  func getPath(path string) string {
    96  	if strings.HasPrefix(path, "/") {
    97  		return path
    98  	}
    99  
   100  	return "/" + path
   101  }