github.com/searchspring/haus@v0.1.8-0.20200414161854-a7ca8bb9ea93/app/repotsar.go (about)

     1  package haus
     2  
     3  import(
     4  	"os"
     5  	"fmt"
     6  	"io/ioutil"
     7  
     8  	"gopkg.in/yaml.v2"
     9  	"github.com/searchspring/repo-tsar/gitutils"
    10  )
    11  
    12  // Signature represents the values to be use in a git signature.
    13  type Signature struct {
    14  	Name string
    15  	Email string
    16  }
    17  
    18  // Repo represents a single git repo definition.
    19  type Repo struct {
    20  	URL string
    21  	Path string
    22  	Branch string
    23  	Link string
    24  
    25  }
    26  
    27  // CloneRepo clones a git repo, hand it a repo name, returns error.
    28  func (r *Repo) CloneRepo(n string) error {
    29  	fmt.Printf("Cloning repo: %s into %s\n",n,r.Path)
    30  	cloneinfo := &gitutils.CloneInfo{
    31  		Reponame: n,
    32  		Path: r.Path,
    33  		URL: r.URL,
    34  		Branch: r.Branch,
    35  	}
    36  	_,err := cloneinfo.CloneRepo()
    37  	if err != nil {
    38  		return err
    39  	}
    40  	return nil
    41  }
    42  
    43  // CreateLink creates symlink defined in Repo.  Returns error.
    44  func (r *Repo) CreateLink() error {
    45  	if r.Link == "" {
    46  		return nil
    47  	} else {
    48  		_,err := os.Lstat(r.Link)
    49  		if err != nil {
    50  			fmt.Printf("Symlinking %s to %#v\n",r.Link,r.Path)
    51  			err = os.Symlink(r.Path,r.Link)
    52  			if err != nil {
    53  				return err
    54  			}
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  // RepoYml represents a collection of repos configs to be turned into YAML.
    61  type RepoYml struct {
    62  	Signature Signature
    63  	Repos map[string]Repo
    64  }
    65  
    66  // AddCfg adds a map[string]Repo to RepoYml.
    67  func (y *RepoYml) AddCfg(r map[string]Repo) {
    68  	for k,v := range r {
    69  		if y.Repos == nil {
    70  			y.Repos = make(map[string]Repo)
    71  		}
    72  		y.Repos[k] = v
    73  	}
    74  }
    75  
    76  // WriteYml writes out Cfgs to yaml file.  Hand it full path
    77  // to yaml file you wish to write out. Returns string of yaml text.
    78  func (y *RepoYml) WriteYml(filename string) (string,error) {
    79  	yaml,err := yaml.Marshal(y)
    80  	if err != nil {
    81  		return "",err
    82  	}
    83  	err = ioutil.WriteFile(filename, yaml, 0644)
    84  	if err != nil {
    85  		return "",err
    86  	}
    87  	return string(yaml[:]),err	
    88  }
    89  
    90