github.com/dnephin/dobi@v0.15.0/tasks/image/record.go (about)

     1  package image
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"time"
     9  
    10  	"github.com/dnephin/dobi/config"
    11  	"github.com/dnephin/dobi/tasks/context"
    12  	yaml "gopkg.in/yaml.v2"
    13  )
    14  
    15  const (
    16  	imageRecordDir = ".dobi/images"
    17  	all            = -1
    18  )
    19  
    20  type imageModifiedRecord struct {
    21  	ImageID  string
    22  	LastPull *time.Time  `yaml:",omitempty"`
    23  	Info     os.FileInfo `yaml:",omitempty"`
    24  }
    25  
    26  func updateImageRecord(path string, record imageModifiedRecord) error {
    27  	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
    28  		return err
    29  	}
    30  
    31  	bytes, err := yaml.Marshal(record)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	return ioutil.WriteFile(path, bytes, 0644)
    36  }
    37  
    38  // TODO: verify error message are sufficient
    39  func getImageRecord(filepath string) (imageModifiedRecord, error) {
    40  	record := imageModifiedRecord{}
    41  	var err error
    42  
    43  	record.Info, err = os.Stat(filepath)
    44  	if err != nil {
    45  		return record, err
    46  	}
    47  
    48  	recordBytes, err := ioutil.ReadFile(filepath)
    49  	if err != nil {
    50  		return record, err
    51  	}
    52  
    53  	return record, yaml.Unmarshal(recordBytes, &record)
    54  }
    55  
    56  func recordPath(ctx *context.ExecuteContext, conf *config.ImageConfig) string {
    57  	return recordPathForTag(ctx.WorkingDir, GetImageName(ctx, conf))
    58  }
    59  
    60  func recordPathForTag(workdir string, tag string) string {
    61  	imageName := strings.Replace(tag, "/", " ", all)
    62  	imageName = strings.Replace(imageName, ":", " ", all)
    63  	return filepath.Join(workdir, imageRecordDir, imageName)
    64  }