Skip to content

Commit

Permalink
Merge pull request #444 from rawlingsj/import-golang
Browse files Browse the repository at this point in the history
feat(import): support creating golang quickstarts
  • Loading branch information
rawlingsj committed Mar 20, 2018
2 parents c847a15 + 6ae4b1b commit be43cac
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 89 deletions.
4 changes: 2 additions & 2 deletions pkg/draft/draft.go
Expand Up @@ -19,8 +19,8 @@ import (

// doPackDetection performs pack detection across all the packs available in $(draft home)/packs in
// alphabetical order, returning the pack dirpath and any errors that occurred during the pack detection.
func DoPackDetection(home draftpath.Home, out io.Writer) (string, error) {
langs, err := linguist.ProcessDir(".")
func DoPackDetection(home draftpath.Home, out io.Writer, dir string) (string, error) {
langs, err := linguist.ProcessDir(dir)
if err != nil {
return "", fmt.Errorf("there was an error detecting the language: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/gits/provider.go
Expand Up @@ -6,9 +6,10 @@ import (
"strings"
"time"

"strconv"

"github.com/jenkins-x/jx/pkg/auth"
"gopkg.in/AlecAivazis/survey.v1"
"strconv"
)

type GitProvider interface {
Expand Down
36 changes: 20 additions & 16 deletions pkg/jx/cmd/create_quickstart.go
Expand Up @@ -49,6 +49,7 @@ type CreateQuickstartOptions struct {

GitHubOrganisations []string
Filter quickstarts.QuickstartFilter
GitProvider gits.GitProvider
}

// NewCmdCreateQuickstart creates a command object for the "create" command
Expand Down Expand Up @@ -92,6 +93,22 @@ func NewCmdCreateQuickstart(f cmdutil.Factory, out io.Writer, errOut io.Writer)

// Run implements the generic Create command
func (o *CreateQuickstartOptions) Run() error {
authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gits.GitHubHost)

userAuth, err := config.PickServerUserAuth(server, "git user name", o.BatchMode)
if err != nil {
return err
}
o.GitProvider, err = gits.CreateProvider(server, userAuth)
if err != nil {
return err
}

model, err := o.LoadQuickstarts()
if err != nil {
return fmt.Errorf("Failed to load quickstarts: %s", err)
Expand All @@ -118,6 +135,8 @@ func (o *CreateQuickstartOptions) Run() error {

o.Printf("Created project at %s\n\n", util.ColorInfo(genDir))

o.CreateProjectOptions.ImportOptions.GitProvider = o.GitProvider
o.Organisation = userAuth.Username
return o.ImportCreatedProject(genDir)
}

Expand Down Expand Up @@ -189,26 +208,11 @@ func findFirstDirectory(dir string) (string, error) {
func (o *CreateQuickstartOptions) LoadQuickstarts() (*quickstarts.QuickstartModel, error) {
model := quickstarts.NewQuickstartModel()

authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return model, err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gits.GitHubHost)

userAuth, err := config.PickServerUserAuth(server, "git user name", o.BatchMode)
if err != nil {
return model, err
}
provider, err := gits.CreateProvider(server, userAuth)
if err != nil {
return model, err
}
groups := o.GitHubOrganisations
if util.StringArrayIndex(groups, JenkinsXQuickstartsOrganisation) < 0 {
groups = append(groups, JenkinsXQuickstartsOrganisation)
}

model.LoadGithubQuickstarts(provider, groups)
model.LoadGithubQuickstarts(o.GitProvider, groups)
return model, nil
}
114 changes: 51 additions & 63 deletions pkg/jx/cmd/import.go
Expand Up @@ -8,11 +8,10 @@ import (
"path/filepath"
"strings"

"runtime"

"github.com/Azure/draft/pkg/draft/draftpath"
"github.com/jenkins-x/draft-repo/pkg/draft/pack"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/auth"
jxdraft "github.com/jenkins-x/jx/pkg/draft"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jenkins"
Expand All @@ -28,7 +27,10 @@ import (
)

const (
PlaceHolder = "REPLACE_ME"
PlaceHolderAppName = "REPLACE_ME_APP_NAME"
PlaceHolderGitProvider = "REPLACE_ME_GIT_PROVIDER"
PlaceHolderOrg = "REPLACE_ME_ORG"

DefaultWritePermissions = 0760

defaultGitIgnoreFile = `
Expand Down Expand Up @@ -173,6 +175,27 @@ func (o *ImportOptions) Run() error {
o.currentNamespace = ns
o.kubeClient = client

var userAuth *auth.UserAuth
if o.GitProvider == nil {
authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gits.GitHubHost)
userAuth, err = config.PickServerUserAuth(server, "git user name", o.BatchMode)
if err != nil {
return err
}
o.GitProvider, err = gits.CreateProvider(server, userAuth)
if err != nil {
return err
}
}
if o.Organisation == "" {
o.Organisation = userAuth.Username
}

if o.GitHub {
return o.ImportProjectsFromGitHub()
}
Expand Down Expand Up @@ -251,30 +274,8 @@ func (o *ImportOptions) Run() error {
}

func (o *ImportOptions) ImportProjectsFromGitHub() error {
authConfigSvc, err := o.Factory.CreateGitAuthConfigService()
if err != nil {
return err
}
config := authConfigSvc.Config()
server := config.GetOrCreateServer(gits.GitHubHost)
userAuth, err := config.PickServerUserAuth(server, "git user name", o.BatchMode)
if err != nil {
return err
}
provider, err := gits.CreateProvider(server, userAuth)
if err != nil {
return err
}

username := userAuth.Username
org := o.Organisation
if org == "" {
org, err = gits.PickOrganisation(provider, username)
if err != nil {
return err
}
}
repos, err := gits.PickRepositories(provider, org, "Which repositories do you want to import", o.SelectAll, o.SelectFilter)
repos, err := gits.PickRepositories(o.GitProvider, o.Organisation, "Which repositories do you want to import", o.SelectAll, o.SelectFilter)
if err != nil {
return err
}
Expand All @@ -285,10 +286,10 @@ func (o *ImportOptions) ImportProjectsFromGitHub() error {
CommonOptions: o.CommonOptions,
Dir: o.Dir,
RepoURL: r.CloneURL,
Organisation: org,
Organisation: o.Organisation,
Repository: r.Name,
Jenkins: o.Jenkins,
GitProvider: provider,
GitProvider: o.GitProvider,
DisableJenkinsfileCheck: o.DisableJenkinsfileCheck,
DisableDraft: o.DisableDraft,
}
Expand Down Expand Up @@ -322,13 +323,12 @@ func (o *ImportOptions) DraftCreate() error {
lpack = filepath.Join(draftHome.Packs(), "github.com/jenkins-x/draft-packs/packs/java")
} else {
// pack detection time
lpack, err = jxdraft.DoPackDetection(draftHome, o.Out)
lpack, err = jxdraft.DoPackDetection(draftHome, o.Out, dir)

if err != nil {
return err
}
}

chartsDir := filepath.Join(dir, "charts")
exists, err = util.FileExists(chartsDir)
if exists {
Expand All @@ -353,7 +353,7 @@ func (o *ImportOptions) DraftCreate() error {
}

//walk through every file in the given dir and update the placeholders
err = o.replacePlaceholders(dir, o.AppName)
err = o.replacePlaceholders()
if err != nil {
return err
}
Expand All @@ -369,29 +369,13 @@ func (o *ImportOptions) DraftCreate() error {
return nil
}

func homePath() string {
return os.ExpandEnv(defaultDraftHome())
}

func defaultDraftHome() string {
if home := os.Getenv("DRAFT_HOME"); home != "" {
return home
}

homeEnvPath := os.Getenv("HOME")
if homeEnvPath == "" && runtime.GOOS == "windows" {
homeEnvPath = os.Getenv("USERPROFILE")
}

return filepath.Join(homeEnvPath, ".draft")
}

func (o *ImportOptions) CreateNewRemoteRepository() error {
f := o.Factory
authConfigSvc, err := f.CreateGitAuthConfigService()
if err != nil {
return err
}

dir := o.Dir
_, defaultRepoName := filepath.Split(dir)

Expand Down Expand Up @@ -425,11 +409,11 @@ func (o *ImportOptions) CreateNewRemoteRepository() error {
func (o *ImportOptions) CloneRepository() error {
url := o.RepoURL
if url == "" {
return fmt.Errorf("No git repository URL defined!")
return fmt.Errorf("no git repository URL defined!")
}
gitInfo, err := gits.ParseGitURL(url)
if err != nil {
return fmt.Errorf("Failed to parse git URL %s due to: %s", url, err)
return fmt.Errorf("failed to parse git URL %s due to: %s", url, err)
}
if gitInfo.Host == gits.GitHubHost && strings.HasPrefix(gitInfo.Scheme, "http") {
if !strings.HasSuffix(url, ".git") {
Expand Down Expand Up @@ -465,7 +449,7 @@ func (o *ImportOptions) DiscoverGit() error {

dir := o.Dir
if dir == "" {
return fmt.Errorf("No directory specified!")
return fmt.Errorf("no directory specified!")
}

// lets prompt the user to initialise the git repository
Expand All @@ -481,7 +465,7 @@ func (o *ImportOptions) DiscoverGit() error {
return err
}
if !flag {
return fmt.Errorf("Please initialise git yourself then try again")
return fmt.Errorf("please initialise git yourself then try again")
}
}
err := gits.GitInit(dir)
Expand Down Expand Up @@ -541,7 +525,7 @@ func (o *ImportOptions) DefaultGitIgnore() error {
data := []byte(defaultGitIgnoreFile)
err = ioutil.WriteFile(name, data, DefaultWritePermissions)
if err != nil {
return fmt.Errorf("Failed to write %s due to %s", name, err)
return fmt.Errorf("failed to write %s due to %s", name, err)
}
}
return nil
Expand All @@ -552,17 +536,17 @@ func (o *ImportOptions) DefaultGitIgnore() error {
func (o *ImportOptions) DiscoverRemoteGitURL() error {
gitConf := o.GitConfDir
if gitConf == "" {
return fmt.Errorf("No GitConfDir defined!")
return fmt.Errorf("no GitConfDir defined!")
}
cfg := gitcfg.NewConfig()
data, err := ioutil.ReadFile(gitConf)
if err != nil {
return fmt.Errorf("Failed to load %s due to %s", gitConf, err)
return fmt.Errorf("failed to load %s due to %s", gitConf, err)
}

err = cfg.Unmarshal(data)
if err != nil {
return fmt.Errorf("Failed to unmarshal %s due to %s", gitConf, err)
return fmt.Errorf("failed to unmarshal %s due to %s", gitConf, err)
}
remotes := cfg.Remotes
if len(remotes) == 0 {
Expand All @@ -586,11 +570,11 @@ func (o *ImportOptions) DiscoverRemoteGitURL() error {

func (o *ImportOptions) DoImport() error {
if o.Jenkins == nil {
jenkins, err := o.Factory.CreateJenkinsClient()
jclient, err := o.Factory.CreateJenkinsClient()
if err != nil {
return err
}
o.Jenkins = jenkins
o.Jenkins = jclient
}
gitURL := o.RepoURL
gitProvider := o.GitProvider
Expand All @@ -613,9 +597,11 @@ func (o *ImportOptions) DoImport() error {
return jenkins.ImportProject(o.Out, o.Jenkins, gitURL, o.Dir, jenkinsfile, o.BranchPattern, o.Credentials, false, gitProvider, authConfigSvc)
}

func (o *ImportOptions) replacePlaceholders(dir, value string) error {
log.Infof("replacing placeholders in direcory %s\n", dir)
if err := filepath.Walk(dir, func(f string, fi os.FileInfo, err error) error {
func (o *ImportOptions) replacePlaceholders() error {
log.Infof("replacing placeholders in direcory %s\n", o.Dir)
log.Infof("app name: %s, git server: %s, org: %s\n", o.AppName, o.GitRepositoryOptions.ServerURL, o.Organisation)

if err := filepath.Walk(o.Dir, func(f string, fi os.FileInfo, err error) error {
if fi.Name() == ".git" {
return filepath.SkipDir
}
Expand All @@ -629,8 +615,10 @@ func (o *ImportOptions) replacePlaceholders(dir, value string) error {
lines := strings.Split(string(input), "\n")

for i, line := range lines {
lines[i] = strings.Replace(line, PlaceHolder, value, -1)

line = strings.Replace(line, PlaceHolderAppName, o.AppName, -1)
line = strings.Replace(line, PlaceHolderGitProvider, o.GitRepositoryOptions.ServerURL, -1)
line = strings.Replace(line, PlaceHolderOrg, o.Organisation, -1)
lines[i] = line
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(f, []byte(output), 0644)
Expand Down
12 changes: 8 additions & 4 deletions pkg/jx/cmd/import_test.go
Expand Up @@ -25,20 +25,24 @@ func TestReplacePlaceholders(t *testing.T) {

assert.NoError(t, err)
o := ImportOptions{}
o.replacePlaceholders(f, "foo")
o.Dir = f
o.AppName = "bar"
o.GitRepositoryOptions.ServerURL = "github.com"
o.Organisation = "foo"
o.replacePlaceholders()

// root file
testFile, err := util.LoadBytes(f, "file.txt")
assert.Equal(t, string(testFile), "foo", "replaced placeholder")
assert.Equal(t, "/home/jenkins/go/src/github.com/foo/bar", string(testFile), "replaced placeholder")

// dir1
testDir1 := path.Join(f, "dir1")
testFile, err = util.LoadBytes(testDir1, "file.txt")
assert.Equal(t, string(testFile), "foo", "replaced placeholder")
assert.Equal(t, "/home/jenkins/go/src/github.com/foo/bar", string(testFile), "replaced placeholder")

// dir2
testDir2 := path.Join(f, "dir2")
testFile, err = util.LoadBytes(testDir2, "file.txt")
assert.Equal(t, string(testFile), "foo", "replaced placeholder")
assert.Equal(t, "/home/jenkins/go/src/github.com/foo/bar", string(testFile), "replaced placeholder")

}
2 changes: 1 addition & 1 deletion pkg/jx/cmd/test_data/replace_placeholders/dir1/file.txt
@@ -1 +1 @@
REPLACE_ME
/home/jenkins/go/src/REPLACE_ME_GIT_PROVIDER/REPLACE_ME_ORG/REPLACE_ME_APP_NAME
2 changes: 1 addition & 1 deletion pkg/jx/cmd/test_data/replace_placeholders/dir2/file.txt
@@ -1 +1 @@
REPLACE_ME
/home/jenkins/go/src/REPLACE_ME_GIT_PROVIDER/REPLACE_ME_ORG/REPLACE_ME_APP_NAME
2 changes: 1 addition & 1 deletion pkg/jx/cmd/test_data/replace_placeholders/file.txt
@@ -1 +1 @@
REPLACE_ME
/home/jenkins/go/src/REPLACE_ME_GIT_PROVIDER/REPLACE_ME_ORG/REPLACE_ME_APP_NAME
1 change: 1 addition & 0 deletions pkg/jx/cmd/util/factory.go
Expand Up @@ -77,6 +77,7 @@ func (f *factory) SetBatch(batch bool) {

// CreateJenkinsClient creates a new jenkins client
func (f *factory) CreateJenkinsClient() (*gojenkins.Jenkins, error) {

svc, err := f.CreateJenkinsAuthConfigService()
if err != nil {
return nil, err
Expand Down

0 comments on commit be43cac

Please sign in to comment.