github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/view_helpers.go (about)

     1  package service
     2  
     3  import (
     4  	"crypto/md5"
     5  	"fmt"
     6  	"io"
     7  	"os/exec"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  const (
    13  	WebRootPath  = "service"
    14  	Templates    = "templates"
    15  	Static       = "static"
    16  	DefaultSkip  = 0
    17  	DefaultLimit = 10
    18  )
    19  
    20  type OtherPageData map[string]interface{}
    21  
    22  // DirectoryChecksum compute an MD5 of a directory's contents. If a file in the directory changes,
    23  // the hash will be different.
    24  func DirectoryChecksum(home string) (string, error) {
    25  	// Compute md5 of ui statics directory
    26  	staticsDirectoryDetailsCmd := exec.Command("ls", "-lR", home)
    27  	staticsDirectoryDetails, err := staticsDirectoryDetailsCmd.Output()
    28  	if err != nil {
    29  		return "", errors.WithStack(err)
    30  	}
    31  
    32  	hash := md5.New()
    33  	if _, err = io.WriteString(hash, string(staticsDirectoryDetails)); err != nil {
    34  		return "", errors.WithStack(err)
    35  
    36  	}
    37  	// fmt.Sprintf("%x") is magic for getting an actual checksum out of crypto/md5
    38  	staticsMD5 := fmt.Sprintf("%x", hash.Sum(nil))
    39  
    40  	return staticsMD5, nil
    41  }