github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/registryv1/builds.go (about)

     1  package registryv1
     2  
     3  import (
     4  	"io"
     5  	"strconv"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go/client"
     8  	"github.com/IBM-Cloud/bluemix-go/helpers"
     9  	"github.com/IBM-Cloud/bluemix-go/rest"
    10  )
    11  
    12  type BuildTargetHeader struct {
    13  	AccountID string
    14  }
    15  
    16  //ToMap ...
    17  func (c BuildTargetHeader) ToMap() map[string]string {
    18  	m := make(map[string]string, 1)
    19  	m[accountIDHeader] = c.AccountID
    20  	return m
    21  }
    22  
    23  type ImageBuildRequest struct {
    24  	/*T
    25  	  The full name for the image that you want to build, including the registry URL and namespace.
    26  	*/
    27  	T string
    28  	/*F
    29  	  Specify the location of the Dockerfile relative to the build context. If not specified, the default is 'PATH/Dockerfile', where PATH is the root of the build context.
    30  	*/
    31  	Dockerfile string
    32  	/*Buildargs
    33  	  A JSON key-value structure that contains build arguments. The value of the build arguments are available as environment variables when you specify an `ARG` line which matches the key in your Dockerfile.
    34  	*/
    35  	Buildargs string
    36  	/*Nocache
    37  	  If set to true, cached image layers from previous builds are not used in this build. Use this option if you expect the result of commands that run in the build to change.
    38  	*/
    39  	Nocache bool
    40  	/*Pull
    41  	  If set to true, the base image is pulled even if an image with a matching tag already exists on the build host. The base image is specified by using the FROM keyword in your Dockerfile. Use this option to update the version of the base image on the build host.
    42  	*/
    43  	Pull bool
    44  	/*Quiet
    45  	  If set to true, build output is suppressed unless an error occurs.
    46  	*/
    47  	Quiet bool
    48  	/*Squash
    49  	  If set to true, the filesystem of the built image is reduced to one layer before it is pushed to the registry. Use this option if the number of layers in your image is close to the maximum for your storage driver.
    50  	*/
    51  	Squash bool
    52  }
    53  
    54  func DefaultImageBuildRequest() *ImageBuildRequest {
    55  	return &ImageBuildRequest{
    56  		T:          "",
    57  		Dockerfile: "",
    58  		Buildargs:  "",
    59  		Nocache:    false,
    60  		Pull:       false,
    61  		Quiet:      false,
    62  		Squash:     false,
    63  	}
    64  }
    65  
    66  // Errordetail
    67  type Errordetail struct {
    68  	Message string `json:"message,omitempty"`
    69  }
    70  
    71  // Progressdetail
    72  type Progressdetail struct {
    73  	Current int `json:"current,omitempty"`
    74  	Total   int `json:"total,omitempty"`
    75  }
    76  
    77  //ImageBuildResponse
    78  type ImageBuildResponse struct {
    79  	ID             string                 `json:"id,omitempty"`
    80  	Stream         string                 `json:"stream,omitempty"`
    81  	Status         string                 `json:"status,omitempty"`
    82  	ProgressDetail Progressdetail         `json:"progressDetail,omitempty"`
    83  	Error          string                 `json:"error,omitempty"`
    84  	ErrorDetail    Errordetail            `json:"errorDetail,omitempty"`
    85  	Aux            map[string]interface{} `json:"aux"`
    86  }
    87  
    88  // Callback function for build response stream
    89  type ImageBuildResponseCallback func(respV ImageBuildResponse) bool
    90  
    91  //Subnets interface
    92  type Builds interface {
    93  	ImageBuild(params ImageBuildRequest, buildContext io.Reader, target BuildTargetHeader, out io.Writer) error
    94  	ImageBuildCallback(params ImageBuildRequest, buildContext io.Reader, target BuildTargetHeader, callback ImageBuildResponseCallback) error
    95  }
    96  
    97  type builds struct {
    98  	client *client.Client
    99  }
   100  
   101  func newBuildAPI(c *client.Client) Builds {
   102  	return &builds{
   103  		client: c,
   104  	}
   105  }
   106  
   107  //Create ...
   108  func (r *builds) ImageBuildCallback(params ImageBuildRequest, buildContext io.Reader, target BuildTargetHeader, callback ImageBuildResponseCallback) error {
   109  	req := rest.PostRequest(helpers.GetFullURL(*r.client.Config.Endpoint, "/api/v1/builds")).
   110  		Query("t", params.T).
   111  		Query("dockerfile", params.Dockerfile).
   112  		Query("buildarg", params.Buildargs).
   113  		Query("nocache", strconv.FormatBool(params.Nocache)).
   114  		Query("pull", strconv.FormatBool(params.Pull)).
   115  		Query("quiet", strconv.FormatBool(params.Quiet)).
   116  		Query("squash", strconv.FormatBool(params.Squash)).
   117  		Body(buildContext)
   118  
   119  	for key, value := range target.ToMap() {
   120  		req.Set(key, value)
   121  	}
   122  
   123  	_, err := r.client.SendRequest(req, callback)
   124  	return err
   125  }
   126  
   127  //Create ...
   128  func (r *builds) ImageBuild(params ImageBuildRequest, buildContext io.Reader, target BuildTargetHeader, out io.Writer) error {
   129  	req := rest.PostRequest(helpers.GetFullURL(*r.client.Config.Endpoint, "/api/v1/builds")).
   130  		Query("t", params.T).
   131  		Query("dockerfile", params.Dockerfile).
   132  		Query("buildarg", params.Buildargs).
   133  		Query("nocache", strconv.FormatBool(params.Nocache)).
   134  		Query("pull", strconv.FormatBool(params.Pull)).
   135  		Query("quiet", strconv.FormatBool(params.Quiet)).
   136  		Query("squash", strconv.FormatBool(params.Squash)).
   137  		Body(buildContext)
   138  
   139  	for key, value := range target.ToMap() {
   140  		req.Set(key, value)
   141  	}
   142  
   143  	_, err := r.client.SendRequest(req, out)
   144  	return err
   145  }