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

     1  package projectLib
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons"
    10  	loginLib "github.com/taubyte/tau-cli/lib/login"
    11  	"github.com/taubyte/tau-cli/singletons/config"
    12  )
    13  
    14  func (h *repositoryHandler) Clone(tauProject config.Project, embedToken bool) (ProjectRepository, error) {
    15  	cwd, err := os.Getwd()
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	if len(tauProject.Location) == 0 {
    21  		tauProject.Location = path.Join(cwd, tauProject.Name)
    22  	} else if !filepath.IsAbs(tauProject.Location) {
    23  		tauProject.Location = path.Join(cwd, tauProject.Location)
    24  	}
    25  
    26  	// Check if user has already defined project name in given location
    27  	if !strings.HasSuffix(strings.ToLower(tauProject.Location), strings.ToLower(tauProject.Name)) {
    28  		tauProject.Location = path.Join(tauProject.Location, tauProject.Name)
    29  	}
    30  
    31  	profile, err := loginLib.GetSelectedProfile()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	if len(tauProject.DefaultProfile) == 0 {
    36  		tauProject.DefaultProfile = profile.Name()
    37  	}
    38  
    39  	// check if the project is configured, if not delete it from config and continue
    40  	testProject, err := config.Projects().Get(h.projectName)
    41  	if err == nil {
    42  		_, configErr := os.Stat(testProject.ConfigLoc())
    43  		_, codeErr := os.Stat(testProject.CodeLoc())
    44  		if configErr != nil || codeErr != nil {
    45  			err = config.Projects().Delete(h.projectName)
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  		} else {
    50  			return nil, singletonsI18n.ProjectAlreadyCloned(h.projectName, testProject.Location)
    51  		}
    52  	}
    53  
    54  	err = h.openOrCloneProject(profile, tauProject, embedToken)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	err = config.Projects().Set(h.projectName, tauProject)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return h, nil
    65  }