github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/bobtask/target/target.go (about)

     1  package target
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Benchkram/bob/pkg/dockermobyutil"
     7  )
     8  
     9  type Target interface {
    10  	Hash() (string, error)
    11  	Verify() bool
    12  	Exists() bool
    13  
    14  	WithHash(string) Target
    15  	WithDir(string) Target
    16  }
    17  
    18  type T struct {
    19  	// working dir of target
    20  	dir string
    21  
    22  	// last computed hash of target
    23  	hash string
    24  
    25  	// dockerRegistryClient utility functions to handle requests with local docker registry
    26  	dockerRegistryClient dockermobyutil.RegistryClient
    27  
    28  	Paths []string   `yaml:"Paths"`
    29  	Type  TargetType `yaml:"Type"`
    30  }
    31  
    32  func Make() T {
    33  	return T{
    34  		dockerRegistryClient: dockermobyutil.NewRegistryClient(),
    35  		Paths:                []string{},
    36  		Type:                 Path,
    37  	}
    38  }
    39  
    40  func New() *T {
    41  	return new()
    42  }
    43  
    44  func new() *T {
    45  	return &T{
    46  		dockerRegistryClient: dockermobyutil.NewRegistryClient(),
    47  		Paths:                []string{},
    48  		Type:                 Path,
    49  	}
    50  }
    51  
    52  type TargetType string
    53  
    54  const (
    55  	Path   TargetType = "path"
    56  	Docker TargetType = "docker"
    57  )
    58  
    59  const DefaultType = Path
    60  
    61  func (t *T) clone() *T {
    62  	target := new()
    63  	target.dir = t.dir
    64  	target.Paths = t.Paths
    65  	target.Type = t.Type
    66  	return target
    67  }
    68  
    69  func (t *T) WithDir(dir string) Target {
    70  	target := t.clone()
    71  	target.dir = dir
    72  	return target
    73  }
    74  func (t *T) WithHash(hash string) Target {
    75  	target := t.clone()
    76  	target.hash = hash
    77  	return target
    78  }
    79  
    80  func ParseType(str string) (TargetType, error) {
    81  	switch {
    82  	case str == string(Path):
    83  		return Path, nil
    84  	case str == string(Docker):
    85  		return Docker, nil
    86  	default:
    87  		return DefaultType, fmt.Errorf("Invalid Target type. Only supports 'path' and 'docker-image' as type.")
    88  	}
    89  }