github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/controller/controller_buildnumbers.go (about)

     1  package controller
     2  
     3  import (
     4  	"github.com/olli-ai/jx/v2/pkg/buildnum"
     5  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
     8  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  const (
    13  	command    = "buildnumbers"
    14  	optionPort = "port"
    15  	optionBind = "bind"
    16  )
    17  
    18  // ControllerBuildNumbersOptions holds the options for the build number service.
    19  type ControllerBuildNumbersOptions struct {
    20  	*opts.CommonOptions
    21  	BindAddress string
    22  	Port        int
    23  }
    24  
    25  var (
    26  	serveBuildNumbersLong = templates.LongDesc(`Runs the build number controller that serves sequential build 
    27  		numbers over an HTTP interface.`)
    28  
    29  	serveBuildNumbersExample = templates.Examples("jx " + command)
    30  )
    31  
    32  // NewCmdControllerBuildNumbers builds a new command to serving build numbers over an HTTP interface.
    33  func NewCmdControllerBuildNumbers(commonOpts *opts.CommonOptions) *cobra.Command {
    34  	options := ControllerBuildNumbersOptions{
    35  		CommonOptions: commonOpts,
    36  	}
    37  	cmd := &cobra.Command{
    38  		Use:     command,
    39  		Short:   "Runs the service to generate build numbers.",
    40  		Long:    serveBuildNumbersLong,
    41  		Example: serveBuildNumbersExample,
    42  		Run: func(cmd *cobra.Command, args []string) {
    43  			options.Cmd = cmd
    44  			options.Args = args
    45  			err := options.Run()
    46  			helper.CheckErr(err)
    47  		},
    48  	}
    49  	cmd.Flags().IntVarP(&options.Port, optionPort, "", 8080, "The TCP port to listen on.")
    50  	cmd.Flags().StringVarP(&options.BindAddress, optionBind, "", "",
    51  		"The interface address to bind to (by default, will listen on all interfaces/addresses).")
    52  	return cmd
    53  }
    54  
    55  // Run will execute this command, starting the HTTP build number generation service with the specified options.
    56  func (o *ControllerBuildNumbersOptions) Run() error {
    57  	jxClient, ns, err := o.JXClientAndDevNamespace()
    58  	if err != nil {
    59  		return err
    60  	}
    61  	buildNumGen := buildnum.NewCRDBuildNumGen(jxClient, ns)
    62  
    63  	httpBuildNumServer := buildnum.NewHTTPBuildNumberServer(o.BindAddress, o.Port, buildNumGen)
    64  	return httpBuildNumServer.Start()
    65  }