Skip to content

Commit

Permalink
Merge pull request #171 from commitdev/integrate-repo-creation
Browse files Browse the repository at this point in the history
Integrated repo creation into create command
  • Loading branch information
bmonkman committed Jun 19, 2020
2 parents 12c0eb4 + ce94969 commit 74528df
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
16 changes: 16 additions & 0 deletions cmd/create.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.")
}
2 changes: 0 additions & 2 deletions internal/context/init.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -74,7 +73,6 @@ func Init(outDir string) *projectconfig.ZeroProjectConfig {
}

}
pp.Println(mappedSources)
projectConfig.Modules[moduleName] = projectconfig.NewModule(projectModuleParams, repoName, repoURL, mappedSources[moduleName])
}

Expand Down
5 changes: 0 additions & 5 deletions internal/generate/generate_modules.go
Expand Up @@ -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
}

Expand Down
40 changes: 22 additions & 18 deletions internal/vcs/create-git-repos.go
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 74528df

Please sign in to comment.