Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/pkg/plugin/common/reposcaffolding/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package reposcaffolding

import "github.com/devstream-io/devstream/pkg/util/github"

func download(org, repo, workpath string) error {
ghOption := &github.Option{
Org: org,
Repo: repo,
NeedAuth: false,
WorkPath: workpath,
}
ghClient, err := github.NewClient(ghOption)
if err != nil {
return err
}

if err = ghClient.DownloadLatestCodeAsZipFile(); err != nil {
return err
}

return nil
}
139 changes: 139 additions & 0 deletions internal/pkg/plugin/common/reposcaffolding/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package reposcaffolding

import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"

"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
)

func walkLocalRepoPath(workpath string, opts *Options, ghClient *github.Client) error {
repoPath := filepath.Join(workpath, fmt.Sprintf("%s-%s", TemplateRepo, MainBranch))
appName := opts.Repo
outputDir := fmt.Sprintf("%s/%s", workpath, opts.Repo)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return err
}

if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Debugf("Walk error: %s.", err)
return err
}

if info.IsDir() {
log.Debugf("Walk: found dir: %s.", path)
oldDir, err := replaceAppNameInPathStr(path, appName)
if err != nil {
return err
}
oldDirSections := strings.Split(oldDir, "/")
newDir := fmt.Sprintf("%s/%s", outputDir, strings.Join(oldDirSections[2:], "/"))
if err := os.MkdirAll(newDir, os.ModePerm); err != nil {
return err
}
log.Debugf("Walk: new output dir created: %s.", newDir)
return nil
}

if strings.Contains(path, ".git/") {
log.Debugf("Walk: ignore file %s.", "./git/xxx")
return nil
}

if strings.HasSuffix(path, "README.md") {
log.Debugf("Walk: ignore file %s.", "README.md")
return nil
}

log.Debugf("Walk: found file: %s.", path)

outputPath := fmt.Sprintf("%s/%s", outputDir, strings.Join(strings.Split(path, "/")[2:], "/"))
if outputPath, err = replaceAppNameInPathStr(outputPath, appName); err != nil {
return err
}
outputPath = strings.TrimSuffix(outputPath, ".tpl")

if strings.Contains(path, "tpl") {
err = render(path, outputPath, opts)
if err != nil {
return err
}
}

return nil
}); err != nil {
return err
}

return nil
}

func render(filePath, output string, opts *Options) error {
var owner = opts.Owner
if opts.Org != "" {
owner = opts.Org
}

config := Config{
AppName: opts.Repo,
ImageRepo: opts.ImageRepo,
Repo: Repo{
Name: opts.Repo,
Owner: owner,
},
}

log.Debugf("Render filePath: %s.", filePath)
log.Debugf("Render config: %v.", config)
log.Debugf("Render output: %s.", output)

textBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
textStr := string(textBytes)

tpl := template.New("github-repo-scaffolding-golang").Delims("[[", "]]")
parsed, err := tpl.Parse(textStr)
if err != nil {
log.Debugf("Template parse file failed: %s.", err)
return err
}

f, err := os.Create(output)
if err != nil {
log.Error("create file: ", err)
return err
}
defer f.Close()
if err = parsed.Execute(f, config); err != nil {
log.Debugf("Template execution failed: %s.", err)
return err
}

return nil
}

func replaceAppNameInPathStr(filePath, appName string) (string, error) {
if !strings.Contains(filePath, AppNamePlaceHolder) {
return filePath, nil
}

reg, err := regexp.Compile(AppNamePlaceHolder)
if err != nil {
return "", err
}
newFilePath := reg.ReplaceAllString(filePath, appName)

log.Debugf("Replace file path place holder. Before: %s, after: %s.", filePath, newFilePath)

return newFilePath, nil
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package golang
package reposcaffolding

import "fmt"

// validate validates the options provided by the core.
func validate(opts *Options) []error {
type Options struct {
Owner string
Org string
Repo string
Branch string
ImageRepo string `mapstructure:"image_repo"`
}

// Validate validates the options provided by the core.
func Validate(opts *Options) []error {
retErrors := make([]error, 0)

// owner/org/repo/branch
Expand Down
56 changes: 56 additions & 0 deletions internal/pkg/plugin/common/reposcaffolding/reposcaffolding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package reposcaffolding

import (
"path/filepath"

"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/zip"
)

const (
TemplateRepo = "dtm-scaffolding-golang"
TemplateOrg = "devstream-io"
TransitBranch = "init-with-devstream"
MainBranch = "main"
AppNamePlaceHolder = "_app_name_"
)

type Config struct {
AppName string
ImageRepo string
Repo Repo
}

type Repo struct {
Name string
Owner string
}

func CreateAndRenderLocalRepo(workpath string, opts *Options) error {
ghOptions := &github.Option{
Owner: opts.Owner,
Org: opts.Org,
Repo: opts.Repo,
NeedAuth: true,
}
ghClient, err := github.NewClient(ghOptions)
if err != nil {
return err
}

if err := download(TemplateOrg, TemplateRepo, workpath); err != nil {
return err
}

if err := zip.UnZip(filepath.Join(workpath, github.DefaultLatestCodeZipfileName), workpath); err != nil {
return err
}

if retErr := walkLocalRepoPath(workpath, opts, ghClient); retErr != nil {
log.Debugf("Failed to walk local repo-path: %s.", retErr)
return retErr
}

return nil
}
59 changes: 7 additions & 52 deletions internal/pkg/plugin/reposcaffolding/github/golang/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/github"
rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/zip"
)

// Create installs github-repo-scaffolding-golang with provided options.
func Create(options map[string]interface{}) (map[string]interface{}, error) {
var opts Options
var opts rs.Options
if err := mapstructure.Decode(options, &opts); err != nil {
return nil, err
}

if errs := validate(&opts); len(errs) != 0 {
if errs := rs.Validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
Expand All @@ -28,65 +27,21 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {

return install(&opts)
}

func install(opts *Options) (map[string]interface{}, error) {
// Clear workpath before return
func install(opts *rs.Options) (map[string]interface{}, error) {
defer func() {
if err := os.RemoveAll(DefaultWorkPath); err != nil {
log.Errorf("Failed to clear workpath %s: %s.", DefaultWorkPath, err)
}
}()

if err := download(); err != nil {
return nil, err
}

if err := zip.UnZip(filepath.Join(DefaultWorkPath, github.DefaultLatestCodeZipfileName), DefaultWorkPath); err != nil {
err := rs.CreateAndRenderLocalRepo(DefaultWorkPath, opts)
if err != nil {
return nil, err
}

if err := push(opts); err != nil {
if err := pushToRemote(filepath.Join(DefaultWorkPath, opts.Repo), opts); err != nil {
return nil, err
}

return buildState(opts), nil
}

func download() error {
ghOption := &github.Option{
Owner: DefaultTemplateOwner,
Repo: DefaultTemplateRepo,
NeedAuth: false,
WorkPath: DefaultWorkPath,
}
ghClient, err := github.NewClient(ghOption)
if err != nil {
return err
}

if err = ghClient.DownloadLatestCodeAsZipFile(); err != nil {
return err
}

return nil
}

func push(opts *Options) error {
ghOptions := &github.Option{
Owner: opts.Owner,
Org: opts.Org,
Repo: opts.Repo,
NeedAuth: true,
}
ghClient, err := github.NewClient(ghOptions)
if err != nil {
return err
}

err = InitRepoLocalAndPushToRemote(filepath.Join(DefaultWorkPath, DefaultTemplateRepo+"-main"), opts, ghClient)
if err != nil {
return err
}

return nil
}
7 changes: 4 additions & 3 deletions internal/pkg/plugin/reposcaffolding/github/golang/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (

"github.com/mitchellh/mapstructure"

rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding"
"github.com/devstream-io/devstream/pkg/util/github"
"github.com/devstream-io/devstream/pkg/util/log"
)

// Delete uninstalls github-repo-scaffolding-golang with provided options.
func Delete(options map[string]interface{}) (bool, error) {
var opts Options
var opts rs.Options
if err := mapstructure.Decode(options, &opts); err != nil {
return false, err
}

if errs := validate(&opts); len(errs) != 0 {
if errs := rs.Validate(&opts); len(errs) != 0 {
for _, e := range errs {
log.Errorf("Options error: %s.", e)
}
Expand All @@ -26,7 +27,7 @@ func Delete(options map[string]interface{}) (bool, error) {
return uninstall(&opts)
}

func uninstall(opts *Options) (bool, error) {
func uninstall(opts *rs.Options) (bool, error) {
ghOptions := &github.Option{
Owner: opts.Owner,
Org: opts.Org,
Expand Down
Loading