github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/bobtask/target/target.go (about)

     1  package target
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/benchkram/bob/bobtask/buildinfo"
     7  	"github.com/benchkram/bob/pkg/dockermobyutil"
     8  )
     9  
    10  type Target interface {
    11  	BuildInfo() (*buildinfo.Targets, error)
    12  
    13  	Verify() bool
    14  	VerifyShallow() VerifyResult
    15  	Resolve() error
    16  
    17  	FilesystemEntries() []string
    18  	FilesystemEntriesRaw() []string
    19  	FilesystemEntriesRawPlain() []string
    20  
    21  	WithExpected(*buildinfo.Targets)
    22  	DockerImages() []string
    23  
    24  	// AsInvalidFiles returns all FilesystemEntriesRaw as invalid with the specified reason
    25  	AsInvalidFiles(reason Reason) map[string][]Reason
    26  }
    27  
    28  type T struct {
    29  	// working dir of target
    30  	dir string
    31  
    32  	// expected is the last valid buildinfo of the target used to verify the targets integrity.
    33  	// Loaded from the system and created on a previous run. Can be nil.
    34  	expected *buildinfo.Targets
    35  
    36  	// current is the currenlty created buildInfo during the run.
    37  	// current avoids recomputations.
    38  	// current *buildinfo.Targets
    39  
    40  	// dockerRegistryClient utility functions to handle requests with local docker registry
    41  	dockerRegistryClient dockermobyutil.RegistryClient
    42  
    43  	// dockerImages an array of docker tags
    44  	dockerImages []string
    45  	// filesystemEntries is an array of files/directories,
    46  	// read from the filesystem.
    47  	// resolve(filesystemEntriesRaw) = filesystemEntriesRaw.
    48  	//
    49  	// Usually the first required when IgnoreChildtargets() is called
    50  	// on aggregate level.
    51  	filesystemEntries *[]string
    52  	// filesystemEntriesRaw is an array of files or directories,
    53  	// as defined by the user.
    54  	//
    55  	// Used to verify that targets are created
    56  	// without verifying against expected buildinfo.
    57  	filesystemEntriesRaw []string
    58  }
    59  
    60  func New(opts ...Option) *T {
    61  	t := &T{}
    62  
    63  	for _, opt := range opts {
    64  		if opt == nil {
    65  			continue
    66  		}
    67  		opt(t)
    68  	}
    69  
    70  	return t
    71  }
    72  
    73  // FilesystemEntries in relation to the umrella bobfile
    74  func (t *T) FilesystemEntries() []string {
    75  	if len(*t.filesystemEntries) == 0 {
    76  		return []string{}
    77  	}
    78  	return *t.filesystemEntries
    79  }
    80  
    81  // FilesystemEntriesRaw returns the filesystem entries
    82  // relative to the umbrella bobfile.
    83  func (t *T) FilesystemEntriesRaw() []string {
    84  	var pathsWithDir []string
    85  	for _, v := range t.filesystemEntriesRaw {
    86  		pathsWithDir = append(pathsWithDir, filepath.Join(t.dir, v))
    87  	}
    88  
    89  	return pathsWithDir
    90  }
    91  
    92  func (t *T) FilesystemEntriesRawPlain() []string {
    93  	return append([]string{}, t.filesystemEntriesRaw...)
    94  }
    95  
    96  func (t *T) WithExpected(expected *buildinfo.Targets) {
    97  	t.expected = expected
    98  }
    99  
   100  func (t *T) WithDockerRegistryClient(c dockermobyutil.RegistryClient) {
   101  	t.dockerRegistryClient = c
   102  }
   103  
   104  func (t *T) DockerImages() []string {
   105  	return append([]string{}, t.dockerImages...)
   106  }
   107  
   108  // AsInvalidFiles returns all FilesystemEntriesRaw as invalid with the specified reason
   109  func (t *T) AsInvalidFiles(reason Reason) map[string][]Reason {
   110  	invalidFiles := make(map[string][]Reason)
   111  
   112  	for _, v := range t.FilesystemEntries() {
   113  		invalidFiles[v] = []Reason{reason}
   114  	}
   115  	return invalidFiles
   116  }