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

     1  package projectLib
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/pterm/pterm"
     9  	"github.com/taubyte/tau-cli/common"
    10  	"github.com/taubyte/tau-cli/env"
    11  	projectI18n "github.com/taubyte/tau-cli/i18n/project"
    12  	singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons"
    13  	"github.com/taubyte/tau-cli/prompts/spinner"
    14  	authClient "github.com/taubyte/tau-cli/singletons/auth_client"
    15  	"github.com/taubyte/tau-cli/singletons/session"
    16  	httpClient "github.com/taubyte/tau/clients/http/auth"
    17  )
    18  
    19  /*
    20  New creates a new project. It creates the config and code repositories
    21  and registers them with the auth server. It then creates a project on
    22  the auth server and sets the current project to the new project.
    23  After that it will clone the project then push the id, description, and
    24  email to the config.yaml file.
    25  
    26  Args:
    27  
    28  	ctx: The cli context
    29  	p: The project to create
    30  	location: The location to clone the project to. If not specified, it
    31  		clones to the current directory.
    32  	embedToken: Whether to embed the auth token in the git config
    33  */
    34  func New(p *Project, location string, embedToken bool) error {
    35  	user, err := env.GetSelectedUser()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	stopGlobe := spinner.Globe()
    41  
    42  	// capture pterm output momentarily so that the globe doesn't go everywhere
    43  	buf := bytes.NewBuffer(nil)
    44  	pterm.SetDefaultOutput(buf)
    45  	defer func() {
    46  		stopGlobe()
    47  
    48  		// Return pterm output to normal
    49  		pterm.SetDefaultOutput(os.Stdout)
    50  
    51  		// Read all from buf and display
    52  		pterm.Println(buf.String())
    53  	}()
    54  
    55  	private := !p.Public
    56  
    57  	client, err := authClient.Load()
    58  	if err != nil {
    59  		return singletonsI18n.LoadingAuthClientFailed(err)
    60  	}
    61  
    62  	// Create config repository
    63  	config_name := fmt.Sprintf(common.ConfigRepoPrefix, p.Name)
    64  	config_id, err := CreateRepository(client, config_name, p.Description, private)
    65  	if err != nil {
    66  		return projectI18n.ConfigRepoCreateFailed(err)
    67  	}
    68  
    69  	// Create code repository
    70  	code_name := fmt.Sprintf(common.CodeRepoPrefix, p.Name)
    71  	code_id, err := CreateRepository(client, code_name, p.Description, private)
    72  	if err != nil {
    73  		return projectI18n.CodeRepoCreateFailed(err)
    74  	}
    75  
    76  	// Register config repository
    77  	err = client.RegisterRepository(config_id)
    78  	if err != nil {
    79  		return projectI18n.ConfigRepoRegisterFailed(err)
    80  	}
    81  
    82  	// Register code repository
    83  	err = client.RegisterRepository(code_id)
    84  	if err != nil {
    85  		return projectI18n.CodeRepoRegisterFailed(err)
    86  	}
    87  
    88  	// Create project
    89  	clientProject := &httpClient.Project{
    90  		Name: p.Name,
    91  	}
    92  	err = clientProject.Create(client, config_id, code_id)
    93  	if err != nil {
    94  		return projectI18n.CreatingProjectFailed(err)
    95  	}
    96  
    97  	// Select created project
    98  	err = session.Set().SelectedProject(clientProject.Name)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	return cloneProjectAndPushConfig(clientProject, location, p.Description, user, embedToken)
   104  }