Files
pulumi-docker-build/provider/internal/dockerfile.go
dependabot[bot] 1febe659ca Bump the docker group with 5 updates (#113)
Bumps the docker group with 5 updates:

| Package | From | To |
| --- | --- | --- |
| [github.com/docker/buildx](https://github.com/docker/buildx) |
`0.13.1` | `0.15.1` |
| [github.com/docker/cli](https://github.com/docker/cli) |
`26.0.0-rc1+incompatible` | `26.1.4+incompatible` |
|
[github.com/docker/distribution](https://github.com/docker/distribution)
| `2.8.2+incompatible` | `2.8.3+incompatible` |
| [github.com/docker/docker](https://github.com/docker/docker) |
`26.0.0-rc1+incompatible` | `26.1.4+incompatible` |
| [github.com/moby/buildkit](https://github.com/moby/buildkit) |
`0.13.0` | `0.14.1` |

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Bryce Lampe <bryce@pulumi.com>
2024-06-26 11:07:02 -07:00

109 lines
2.7 KiB
Go

// Copyright 2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"errors"
"io"
"os"
"path/filepath"
"strings"
buildx "github.com/docker/buildx/build"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/pulumi/pulumi-go-provider/infer"
)
// Dockerfile references a local, remote, or inline Dockerfile.
type Dockerfile struct {
Location string `pulumi:"location,optional"`
Inline string `pulumi:"inline,optional"`
}
// Annotate sets docstrings on Dockerfile.
func (d *Dockerfile) Annotate(a infer.Annotator) {
a.Describe(&d.Location, dedent(`
Location of the Dockerfile to use.
Can be a relative or absolute path to a local file, or a remote URL.
Defaults to "${context.location}/Dockerfile" if context is on-disk.
Conflicts with "inline".
`))
a.Describe(&d.Inline, dedent(`
Raw Dockerfile contents.
Conflicts with "location".
Equivalent to invoking Docker with "-f -".
`))
}
func (d *Dockerfile) validate(preview bool, c *Context) error {
if d.Location != "" && d.Inline != "" {
return newCheckFailure(
errors.New(`only specify "file" or "inline", not both`),
"dockerfile",
)
}
if d.Location != "" {
if buildx.IsRemoteURL(d.Location) {
return nil
}
abs, err := filepath.Abs(d.Location)
if err != nil {
return err
}
f, err := os.Open(filepath.Clean(abs))
if err != nil {
return newCheckFailure(err, "dockerfile.location")
}
if err := parseDockerfile(f); err != nil {
return newCheckFailure(err, "dockerfile.location")
}
return nil
}
if d.Inline != "" {
err := parseDockerfile(strings.NewReader(d.Inline))
if err != nil {
return newCheckFailure(err, "dockerfile.inline")
}
return nil
}
if !preview && c != nil && !buildx.IsRemoteURL(c.Location) {
return newCheckFailure(errors.New("missing 'location' or 'inline'"), "dockerfile")
}
return nil
}
func parseDockerfile(r io.Reader) error {
parsed, err := parser.Parse(r)
if err != nil {
return newCheckFailure(err, "dockerfile")
}
_, _, err = instructions.Parse(parsed.AST, nil)
if err != nil {
return err
}
return nil
}