Skip to content

Commit

Permalink
Functionality to Install Vega Starterkits to Existing Projects
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay committed Jul 13, 2020
1 parent e180516 commit d6637aa
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Several options to install:
| `vega init` | Initializes vega | | |
| `vega starterkit list` | List all available starterkits | | drupal8<br>nodejs |
| `vega create [path] --starterkit <name>` | Creates the starter kit at provided directory | \--starterkit <name><br>\--repo <repo> | |
| `vega install [path]` | Install a starterkit to existing project | \--repo <repo> | |
| `vega repo add <repo-name> <url>` | Add another starterkit repo, Can choose local folder as well | | |
| `vega repo list` | Lists all the repo available | \--repo <repo> | |
| `vega hooks install [path]` | Installs git hooks to specified path | | |
Expand Down
84 changes: 84 additions & 0 deletions cmd/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cmd

import (
"fmt"
"io"
"path/filepath"

"github.com/spf13/cobra"
survey "github.com/AlecAivazis/survey/v2"

vega "github.com/srijanone/vega/pkg/core"
)

type installCmd struct {
out io.Writer
home vega.Home
repo string // StarterKit Repo
path string // Project path where starter kit is going to install
}

func newInstallCmd(out io.Writer) *cobra.Command {
iCmd := installCmd{out: out}

const installDesc = "install vega to existing repository"

installCobraCmd := &cobra.Command{
Use: "install",
Short: installDesc,
Long: installDesc,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
// If no arguments are passed, then choose current directory as path
iCmd.path = "."
} else {
iCmd.path = args[0]
}
iCmd.home = vega.Home(homePath())

iCmd.execute()
},
}

flags := installCobraCmd.Flags()
flags.StringVarP(&iCmd.repo, "repo", "r", "default", "name of the starterkit repo")

return installCobraCmd
}

func (iCmd *installCmd) execute() {
fmt.Fprintf(iCmd.out, "Installing vega to %s\n", iCmd.path)

repoPath := filepath.Join(iCmd.home.StarterKits(), iCmd.repo)
starterkitRepo := vega.StarterKitRepo{
Name: iCmd.repo,
Path: repoPath,
}
starterkits, err := starterkitRepo.StarterKitList()
if err != nil {
fmt.Fprintln(iCmd.out, "No starterkit found")
}

starterkit := ""
prompt := &survey.Select{
Message: "Select starterkit which you want to install:",
}
for _, starterkit := range starterkits {
prompt.Options = append(prompt.Options, starterkit.Name)
}
survey.AskOne(prompt, &starterkit)
fmt.Fprintf(iCmd.out,"You have selected " + starterkit + "\n")

sk := vega.StarterKit{
Name: starterkit,
Path: filepath.ToSlash(filepath.Join(repoPath, starterkit)),
}

err = sk.Install(iCmd.path)
if err != nil {
fmt.Fprintf(iCmd.out, "Error in Installing : %v\n", err)
} else {
fmt.Fprintln(iCmd.out, "Starterkit installed successfully")
}
}

1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func newRootCmd(out io.Writer, in io.Reader) *cobra.Command {
cmd.AddCommand(newInitCmd(out, in))
cmd.AddCommand(newHomeCmd(out))
cmd.AddCommand(newCreateCmd(out))
cmd.AddCommand(newInstallCmd(out))
cmd.AddCommand(newVersionCmd(out))
cmd.AddCommand(newStarterKitCmd(out))
cmd.AddCommand(newUpCmd(out))
Expand Down
48 changes: 48 additions & 0 deletions pkg/core/starterkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package vega

import (
"fmt"
"path/filepath"

"github.com/otiai10/copy"

common "github.com/srijanone/vega/pkg/common"
)

// StarterKit represents a starterkit repository.
Expand All @@ -17,9 +20,54 @@ type StarterKits []StarterKit
// DockerfileName: Default dockerfile name used in starterkits
const DockerfileName string = "Dockerfile"

// Create creates a new project at dest from given starter kit
func (sk *StarterKit) Create(dest string) error {
// TODO: Merge gracefully in future
fmt.Printf("Creating starterkit %s ... \n", sk.Name)
err := copy.Copy(sk.Path, dest)
return err
}

// Install installs a starterkit at existing project
func (sk *StarterKit) Install(dest string) error {
// TODO: This filesToCopy is dependent of type of starterkit
// files for Drupal starterkit
filesToCopy := []string{
// Docker Related
"Dockerfile",
".dockerignore",
"docker-compose.yml",

// Tilt Related
"Tiltfile",

// Others
".env",
}

for _, file := range filesToCopy {
srcFile := filepath.ToSlash(filepath.Join(sk.Path, file))
destFile := filepath.ToSlash(filepath.Join(dest, file))
err := common.CopyFile(srcFile, destFile)
if err != nil {
return err
}
}

// Directory To Copy
dirToCopy := []string{
// Debug Related
".vscode",
}

for _, dir := range dirToCopy {
srcDir := filepath.ToSlash(filepath.Join(sk.Path, dir))
destDir := filepath.ToSlash(filepath.Join(dest, dir))
err := copy.Copy(srcDir, destDir)
if err != nil {
return err
}
}

return nil
}

0 comments on commit d6637aa

Please sign in to comment.