Skip to content

Commit

Permalink
Implement switching logic on Source variants
Browse files Browse the repository at this point in the history
Continue implementing the necessary switches. This code compiles.

Signed-off-by: Peter Engelbert <[email protected]>
  • Loading branch information
pmengelbert committed Dec 11, 2023
1 parent 175e3fe commit a9f697b
Showing 1 changed file with 26 additions and 52 deletions.
78 changes: 26 additions & 52 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import (
"io"
"strings"

"github.com/Azure/dalec"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/dockerui"
sourcetypes "github.com/moby/buildkit/source/types"
"github.com/moby/buildkit/util/gitutil"
)

Expand Down Expand Up @@ -185,7 +183,7 @@ func source2LLBGetter(s *Spec, src Source, name string, forMount bool) LLBGetter
st = llb.Local(dockerui.DefaultLocalNameContext, withConstraints(opts))
} else {
src2 := Source{
Build: &dalec.SourceBuild{Ref: build.Ref},
Build: &SourceBuild{Ref: build.Ref},
Path: src.Path,
Includes: src.Includes,
Excludes: src.Excludes,
Expand Down Expand Up @@ -237,23 +235,16 @@ func WithCreateDestPath() llb.CopyOption {
}

func SourceIsDir(src Source) (bool, error) {
scheme, _, err := SplitSourceRef(src.Ref)
if err != nil {
return false, err
}
switch scheme {
case sourcetypes.DockerImageScheme,
sourcetypes.GitScheme,
sourceTypeBuild,
sourceTypeContext:
switch {
case src.DockerImage != nil,
src.Git != nil,
src.Build != nil,
src.Context != nil:
return true, nil
case sourcetypes.HTTPScheme, sourcetypes.HTTPSScheme:
if isGitRef(src.Ref) {
return true, nil
}
case src.HTTPS != nil:
return false, nil
default:
return false, fmt.Errorf("unsupported source type: %s", scheme)
return false, fmt.Errorf("unsupported source type")
}
}

Expand All @@ -267,20 +258,16 @@ func isGitRef(ref string) bool {
// so that others can reproduce the build.
func (s Source) Doc() (io.Reader, error) {
b := bytes.NewBuffer(nil)
scheme, ref, err := SplitSourceRef(s.Ref)
if err != nil {
return nil, err
}

switch scheme {
case sourceTypeSource:
fmt.Fprintln(b, "Generated from another source named:", ref)
case sourceTypeContext:
switch {
case s.Source != nil:
fmt.Fprintln(b, "Generated from another source named:", s.Source.Ref)
case s.Context != nil:
fmt.Fprintln(b, "Generated from a local docker build context and is unreproducible.")
case sourceTypeBuild:
case s.Build != nil:
build := s.Build
fmt.Fprintln(b, "Generated from a docker build:")
fmt.Fprintln(b, " Docker Build Target:", s.Build.Target)
fmt.Fprintln(b, " Docker Build Ref:", ref)
fmt.Fprintln(b, " Docker Build Ref:", build.Ref)

if len(s.Build.Args) > 0 {
sorted := SortMapKeys(s.Build.Args)
Expand All @@ -307,44 +294,31 @@ func (s Source) Doc() (io.Reader, error) {
}
fmt.Fprintln(b, " Dockerfile path in context:", p)
}
case sourcetypes.HTTPScheme, sourcetypes.HTTPSScheme:
ref, err := gitutil.ParseGitRef(ref)
if err == nil {
// git ref
fmt.Fprintln(b, "Generated from a git repository:")
fmt.Fprintln(b, " Remote:", scheme+"://"+ref.Remote)
fmt.Fprintln(b, " Ref:", ref.Commit)
if ref.SubDir != "" {
fmt.Fprintln(b, " Subdir:", ref.SubDir)
}
if s.Path != "" {
fmt.Fprintln(b, " Extraced path:", s.Path)
}
} else {
fmt.Fprintln(b, "Generated from a http(s) source:")
fmt.Fprintln(b, " URL:", ref)
}
case sourcetypes.GitScheme:
ref, err := gitutil.ParseGitRef(ref)
case s.HTTPS != nil:
fmt.Fprintln(b, "Generated from a http(s) source:")
fmt.Fprintln(b, " URL:", s.HTTPS.Ref)
case s.Git != nil:
git := s.Git
ref, err := gitutil.ParseGitRef(git.Ref)
if err != nil {
return nil, err
}
fmt.Fprintln(b, "Generated from a git repository:")
fmt.Fprintln(b, " Remote:", scheme+"://"+ref.Remote)
fmt.Fprintln(b, " Ref:", ref.Commit)
if s.Path != "" {
fmt.Fprintln(b, " Extraced path:", s.Path)
}
case sourcetypes.DockerImageScheme:
case s.DockerImage != nil:
img := s.DockerImage
if s.Cmd == nil {
fmt.Fprintln(b, "Generated from a docker image:")
fmt.Fprintln(b, " Image:", ref)
fmt.Fprintln(b, " Image:", img.Ref)
if s.Path != "" {
fmt.Fprintln(b, " Extraced path:", s.Path)
}
} else {
fmt.Fprintln(b, "Generated from running a command(s) in a docker image:")
fmt.Fprintln(b, " Image:", ref)
fmt.Fprintln(b, " Image:", img.Ref)
if s.Path != "" {
fmt.Fprintln(b, " Extraced path:", s.Path)
}
Expand Down Expand Up @@ -393,7 +367,7 @@ func (s Source) Doc() (io.Reader, error) {
default:
// This should be unrecable.
// We could panic here, but ultimately this is just a doc string and parsing user generated content.
fmt.Fprintln(b, "Generated from an unknown source type:", s.Ref)
fmt.Fprintln(b, "Generated from an unknown source type")
}

return b, nil
Expand Down

0 comments on commit a9f697b

Please sign in to comment.