sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/githubeventserver/options.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package githubeventserver
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  // Options holds the endpoint and port information that can be used
    26  // to create a new github event server
    27  type Options struct {
    28  	// Metrics will be used to expose prometheus metrics from the
    29  	// github event server operations.
    30  	Metrics *Metrics
    31  
    32  	// endpoint is the main url path that the github event server will be served.
    33  	endpoint string
    34  	// port will be used to start an http server to listen to.
    35  	port int
    36  }
    37  
    38  // DefaultAndValidate validates the option's values and defaults them if they are empty.
    39  func (o *Options) DefaultAndValidate() error {
    40  	if !strings.HasPrefix(o.endpoint, "/") {
    41  		return fmt.Errorf("endpoint %s is not a valid url path", o.endpoint)
    42  	}
    43  
    44  	if o.Metrics == nil {
    45  		o.Metrics = NewMetrics()
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // Bind binds the flags into the given flagset.
    52  func (o *Options) Bind(fs *flag.FlagSet) {
    53  	fs.StringVar(&o.endpoint, "endpoint", "/hook", "The endpoint path where the http server will listen to")
    54  	fs.IntVar(&o.port, "port", 8888, "Port to listen on.")
    55  }