From ce94969eeda00a0ac27c65c1d2c45b32e03d1e50 Mon Sep 17 00:00:00 2001 From: Bill Monkman Date: Fri, 19 Jun 2020 11:20:38 -0700 Subject: [PATCH] Integrated repo creation into create command --- cmd/create.go | 16 +++++++++++ internal/context/init.go | 2 -- internal/generate/generate_modules.go | 5 ---- internal/vcs/create-git-repos.go | 40 +++++++++++++++------------ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index a3052f52b..359022e48 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -5,10 +5,13 @@ import ( "path" "strings" + "github.com/commitdev/zero/internal/config/globalconfig" "github.com/commitdev/zero/internal/config/projectconfig" "github.com/commitdev/zero/internal/constants" "github.com/commitdev/zero/internal/generate" + "github.com/commitdev/zero/internal/vcs" "github.com/commitdev/zero/pkg/util/exit" + "github.com/commitdev/zero/pkg/util/flog" "github.com/spf13/cobra" ) @@ -36,4 +39,17 @@ func Create(dir string, createConfigPath string) { projectConfig := projectconfig.LoadConfig(configFilePath) generate.Generate(*projectConfig) + + if projectConfig.ShouldPushRepositories { + flog.Infof(":up_arrow: Done Rendering - committing repositories to version control.") + + globalConfig := globalconfig.GetProjectCredentials(projectConfig.Name) + for _, module := range projectConfig.Modules { + vcs.InitializeRepository(module.Files.Repository, globalConfig.GithubResourceConfig.AccessToken) + } + } else { + flog.Infof(":up_arrow: Done Rendering - you will need to commit the created projects to version control.") + } + + flog.Infof(":check_mark_button: Done - run zero apply to create any required infrastructure or execute any other remote commands to prepare your environments.") } diff --git a/internal/context/init.go b/internal/context/init.go index 4da7a1264..7fa8beecf 100644 --- a/internal/context/init.go +++ b/internal/context/init.go @@ -19,7 +19,6 @@ import ( project "github.com/commitdev/zero/pkg/credentials" "github.com/commitdev/zero/pkg/util/exit" "github.com/commitdev/zero/pkg/util/flog" - "github.com/k0kubun/pp" "github.com/manifoldco/promptui" ) @@ -74,7 +73,6 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig { } } - pp.Println(mappedSources) projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL, mappedSources[moduleName]) } diff --git a/internal/generate/generate_modules.go b/internal/generate/generate_modules.go index 83cd27eed..f688cd557 100644 --- a/internal/generate/generate_modules.go +++ b/internal/generate/generate_modules.go @@ -56,11 +56,6 @@ func Generate(projectConfig projectconfig.ZeroProjectConfig) error { executeTemplates(fileTemplates, templateData, delimiters) } - - flog.Infof(":up_arrow: Done Rendering - committing repositories to version control") - // TODO : Integrate this work - - flog.Infof(":check_mark_button: Done - run zero apply to create any required infrastructure or execute any other remote commands to prepare your environments.") return nil } diff --git a/internal/vcs/create-git-repos.go b/internal/vcs/create-git-repos.go index ba063491a..51105a53d 100644 --- a/internal/vcs/create-git-repos.go +++ b/internal/vcs/create-git-repos.go @@ -3,9 +3,11 @@ package vcs import ( "context" "fmt" - "github.com/machinebox/graphql" "os/exec" "strings" + + "github.com/commitdev/zero/pkg/util/flog" + "github.com/machinebox/graphql" ) // InitializeRepository Creates and initializes a github repository for the given url @@ -53,19 +55,19 @@ func InitializeRepository(repositoryUrl string, githubApiKey string) { return } - fmt.Printf("Repository successfully created and initialized for module: %s\n", repositoryName) + flog.Infof(":check_mark_button: Repository created: %s", repositoryUrl) } // parseRepositoryUrl extracts the owner name and repository name from a repository url. // repositoryUrl is expected to be in the format "github.com/{ownerName}/{repositoryName}" func parseRepositoryUrl(repositoryUrl string) (string, string, error) { if len(repositoryUrl) == 0 { - return "","", fmt.Errorf("invalid repository url. expected format \"github.com/{ownerName}/{repositoryName}\"") + return "", "", fmt.Errorf("invalid repository url. expected format \"github.com/{ownerName}/{repositoryName}\"") } segments := strings.Split(repositoryUrl, "/") if len(segments) != 3 { - return "","", fmt.Errorf("invalid repository url. expected format \"github.com/{ownerName}/{repositoryName}\"") + return "", "", fmt.Errorf("invalid repository url. expected format \"github.com/{ownerName}/{repositoryName}\"") } ownerName := segments[1] @@ -77,10 +79,10 @@ func parseRepositoryUrl(repositoryUrl string) (string, string, error) { const createPersonalRepositoryMutation = `mutation ($repoName: String!, $repoDescription: String!) { createRepository( input: { - name:$repoName, - visibility: PRIVATE, + name:$repoName, + visibility: PRIVATE, description: $repoDescription - }) + }) { clientMutationId } @@ -89,11 +91,11 @@ const createPersonalRepositoryMutation = `mutation ($repoName: String!, $repoDes const createOrganizationRepositoryMutation = `mutation ($repoName: String!, $repoDescription: String!, $ownerId: String!) { createRepository( input: { - name:$repoName, - visibility: PRIVATE, + name:$repoName, + visibility: PRIVATE, description: $repoDescription ownerId: $ownerId - }) + }) { clientMutationId } @@ -183,21 +185,23 @@ func doInitialCommit(ownerName string, repositoryName string) error { } for _, command := range commands { - fmt.Printf(">> %s\n", command.description) + // TODO: Debug-level logging? + // fmt.Printf(">> %s\n", command.description) cmd := exec.Command(command.command, command.args...) cmd.Dir = "./" + repositoryName - out, err := cmd.CombinedOutput() + _, err := cmd.CombinedOutput() if err != nil { fmt.Printf("ERROR: failed to run %s: %s\n", command.description, err.Error()) // this is a partial failure. some commands may have exec'ed successfully. break - } else { - response := string(out) - if len(response) > 0 { - fmt.Println(response) - } - } + } //else { + // TODO: Debug-level logging? + // response := string(out) + // if len(response) > 0 { + // fmt.Println(response) + // } + // } } return nil