github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/lib/repository/clone.go (about)

     1  package repositoryLib
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path"
     7  	"strings"
     8  
     9  	git "github.com/taubyte/go-simple-git"
    10  	libraryI18n "github.com/taubyte/tau-cli/i18n/library"
    11  	websiteI18n "github.com/taubyte/tau-cli/i18n/website"
    12  	loginLib "github.com/taubyte/tau-cli/lib/login"
    13  	"github.com/taubyte/tau-cli/singletons/config"
    14  	"github.com/taubyte/tau-cli/states"
    15  )
    16  
    17  func (info *Info) HasBeenCloned(project config.Project, provider string) bool {
    18  	var dir string
    19  	switch info.Type {
    20  	case WebsiteRepositoryType:
    21  		dir = project.WebsiteLoc()
    22  	case LibraryRepositoryType:
    23  		dir = project.LibraryLoc()
    24  	default:
    25  		return false
    26  
    27  	}
    28  
    29  	repositoryPath := path.Join(dir, strings.Split(info.FullName, "/")[1])
    30  
    31  	_, err := os.Stat(repositoryPath)
    32  	return err == nil
    33  }
    34  
    35  func (info *Info) Clone(project config.Project, url, branch string, embedded bool) (*git.Repository, error) {
    36  	if !info.DoClone {
    37  		return nil, errors.New("cloning when info.Clone is false")
    38  	}
    39  
    40  	repositoryPath, err := info.path(project)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	_, err = os.Stat(repositoryPath)
    46  	if err == nil {
    47  		if info.Type == WebsiteRepositoryType {
    48  			websiteI18n.Help().WebsiteAlreadyCloned(repositoryPath)
    49  			return nil, websiteI18n.ErrorAlreadyCloned
    50  		}
    51  
    52  		libraryI18n.Help().LibraryAlreadyCloned(repositoryPath)
    53  		return nil, libraryI18n.ErrorAlreadyCloned
    54  	}
    55  
    56  	profile, err := loginLib.GetSelectedProfile()
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	var tokenOption git.Option
    62  	if embedded {
    63  		tokenOption = git.EmbeddedToken(profile.Token)
    64  	} else {
    65  		tokenOption = git.Token(profile.Token)
    66  	}
    67  
    68  	repo, err := git.New(states.Context,
    69  		git.Root(repositoryPath),
    70  		git.Author(profile.GitUsername, profile.GitEmail),
    71  		git.URL(url),
    72  		tokenOption,
    73  
    74  		// TODO branch, this breaks things
    75  		// git.Branch(branch),
    76  	)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	return repo, nil
    82  }