Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay committed Apr 2, 2020
1 parent a815a0b commit b456dac
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 70 deletions.
8 changes: 3 additions & 5 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import (

type createCmd struct {
out io.Writer
starterkit string
home vega.Home
starterkit string
dest string
repo string
}

func newCreateCmd(out io.Writer) *cobra.Command {
cCmd := &createCmd{
out: out,
}
cCmd := &createCmd{out: out}

const createDesc = "create starterkit"

Expand All @@ -43,7 +41,7 @@ func newCreateCmd(out io.Writer) *cobra.Command {
cCmd.home = vega.Home(homePath())

flags := createCmd.Flags()
flags.StringVarP(&cCmd.starterkit, "starterkit", "s", "", "name of the Vega starterkit to scaffold the app")
flags.StringVarP(&cCmd.starterkit, "starterkit", "s", "", "name of the vega starterkit to scaffold the app")
flags.StringVarP(&cCmd.repo, "repo", "r", "default", "name of the starterkit repo")
cobra.MarkFlagRequired(flags, "starterkit")
return createCmd
Expand Down
1 change: 0 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
if len(args) != 0 {
return errors.New("Command does not accept arguments")
}
fmt.Println("Vega Home: ", homePath())
init.home = vega.Home(homePath())
return init.execute()
},
Expand Down
6 changes: 4 additions & 2 deletions cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
)

func newRepoCmd(out io.Writer) *cobra.Command {
const repoDesc = "manage starterkits repositories"
const repoDesc = "manage repositories of starterkit"

repoCmd := &cobra.Command{
Use: "repo",
Short: repoDesc,
Long: repoDesc,
}

repoCmd.AddCommand(newAddCmd(out))
repoCmd.AddCommand(newRepositoryList(out))
repoCmd.AddCommand(newRepositoryCmdList(out))

return repoCmd
}
5 changes: 2 additions & 3 deletions cmd/repo_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
)

type repoAddCmd struct {
URL string
dst string
out io.Writer
home vega.Home
name string
URL string
}

func newAddCmd(out io.Writer) *cobra.Command {
Expand Down Expand Up @@ -45,6 +44,6 @@ func (rAddCmd *repoAddCmd) execute() {
Home: rAddCmd.home,
}

fmt.Fprintln(rAddCmd.out, "Adding new repo:", rAddCmd.URL)
fmt.Fprintf(rAddCmd.out, "Adding new repo %s from %s", rAddCmd.name, rAddCmd.URL)
starterKitsRepo.Add()
}
16 changes: 8 additions & 8 deletions cmd/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ type repositoryListCmd struct {
home vega.Home
}

func newRepositoryList(out io.Writer) *cobra.Command {
func newRepositoryCmdList(out io.Writer) *cobra.Command {
rListCmd := repositoryListCmd{out: out}

const listCmdDesc = "list all the starterkit repositories available locally"
repoListCmd := repositoryListCmd{
out: out,
}

listCmd := &cobra.Command{
Use: "list",
Short: listCmdDesc,
Long: listCmdDesc,
Run: func(cmd *cobra.Command, args []string) {
repoListCmd.execute()
rListCmd.execute()
},
}

repoListCmd.home = vega.Home(homePath())
rListCmd.home = vega.Home(homePath())

return listCmd
}
Expand All @@ -37,9 +37,9 @@ func (cmd *repositoryListCmd) execute() error {
if err != nil {
return err
}
fmt.Fprintln(cmd.out, "Availabe Repositories:")
fmt.Fprintln(cmd.out, "Available Repositories:")
for _, repo := range repositories {
fmt.Fprintln(cmd.out, repo.Name)
fmt.Fprintf(cmd.out, " %20s (%s)\n", repo.Name, repo.Path)
}
return nil
}
2 changes: 1 addition & 1 deletion cmd/starterkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func newStarterKitCmd(out io.Writer) *cobra.Command {
const starterkitDesc = "manage starterkits used for initializing project"
const starterkitDesc = "manage starterkits"

starterkitCmd := &cobra.Command{
Use: "starterkit",
Expand Down
2 changes: 1 addition & 1 deletion cmd/starterkit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (cmd *starterkitListCmd) execute() error {
Name: cmd.repo,
Path: path,
}
starterkits, err := starterkitRepo.List()
starterkits, err := starterkitRepo.StarterKitList()
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ type versionCmd struct {
}

func newVersionCmd(out io.Writer) *cobra.Command {
vCmd := &versionCmd{
out: out,
}
vCmd := &versionCmd{out: out}

const versionDesc = "print version"

Expand Down
15 changes: 0 additions & 15 deletions pkg/common/common_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,3 @@ func EnsureDir(dir string) error {

return nil
}

func EnsureFile(filename string) error {
info, err := os.Stat(filename)
if err != nil {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Could not create %s: %s", filename, err)
}
defer file.Close()
} else if info.IsDir() {
return fmt.Errorf("%s must not be a directory", filename)
}

return nil
}
4 changes: 0 additions & 4 deletions pkg/core/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func (home Home) StarterKits() string {
return home.Path("starterkits")
}

func (home Home) Repos() string {
return home.Path("repos")
}

func (home Home) Logs() string {
return home.Path("logs")
}
32 changes: 14 additions & 18 deletions pkg/core/starterkit_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ type StarterKitRepo struct {
Path string // local absolute path to repo
Home Home
URL string
Dir string // starterkit directory name at source/remote
Dir string // starterkits directory name at source/remote
}

type StarterKitRepos []StarterKitRepo

// RepoList list of all the local Repositories
func RepoList(path string) ([]StarterKitRepo, error) {
var repositories StarterKitRepos

var repositories []StarterKitRepo
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
Expand All @@ -42,14 +44,14 @@ func RepoList(path string) ([]StarterKitRepo, error) {
return repositories, nil
}

// List Gets the list of all StarterKits for given repo.
func (repo *StarterKitRepo) List() (StarterKits, error) {
// StarterKitList Gets the list of all StarterKits for given repo.
func (repo *StarterKitRepo) StarterKitList() (StarterKits, error) {
var starterkits StarterKits

switch fileInfo, err := os.Stat(repo.Path); {
case err != nil:
if os.IsNotExist(err) {
return nil, errors.New(fmt.Sprintf("starterkit local repo %s not found", repo.Path))
return nil, errors.New("No repo found, please type `vega init`")
}
case !fileInfo.IsDir():
return nil, errors.New(fmt.Sprintf("%s is not a starterkit repo", repo.Path))
Expand All @@ -71,11 +73,11 @@ func (repo *StarterKitRepo) List() (StarterKits, error) {
return starterkits, nil
}

//Find return starterkits matching with the given name
// Find returns the starterkits matching with the given name
func (repo *StarterKitRepo) Find(name string) ([]StarterKit, error) {
var starterkits StarterKits

starterkitList, err := repo.List()
starterkitList, err := repo.StarterKitList()
if err != nil {
return nil, err
}
Expand All @@ -93,11 +95,11 @@ func (repo *StarterKitRepo) Find(name string) ([]StarterKit, error) {
return starterkits, nil
}

//Add add staterkits repo to vega and download all the starterkits
// Add adds repo and starterkits to vega
func (repo *StarterKitRepo) Add() {
d := downloader.Downloader{}
if repo.Dir == "" {
repo.Dir = "starterkits"
repo.Dir = repo.Home.StarterKits()
}
sourceRepo := fmt.Sprintf("%s//%s", repo.URL, repo.Dir)
fmt.Println("Downloading starterkits...")
Expand All @@ -107,16 +109,10 @@ func (repo *StarterKitRepo) Add() {
d.Download(sourceRepo, repo.Path)
}

//Delete starterkit repo
// Delete deletes the starterkit repo
func (repo StarterKitRepo) Delete() {
d := downloader.Downloader{}
if repo.Dir == "" {
repo.Dir = "starterkits"
}
sourceRepo := fmt.Sprintf("%s//%s", repo.URL, repo.Dir)
fmt.Println("Downloading starterkits...")
if repo.Path == "" {
repo.Path = filepath.Join(repo.Home.StarterKits(), repo.Name)
repo.Dir = repo.Home.StarterKits()
}
d.Download(sourceRepo, repo.Path)
// TODO
}
7 changes: 0 additions & 7 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,3 @@ func (d *Downloader) Download(src string, dest string) {

wg.Wait()
}

// For testing now, will be removed
//func main() {
// fmt.Println("Downloading")
// d := &Downloader{}
// d.Download("git@github.com:Azure/draft.git//packs", "packs")
//}
4 changes: 2 additions & 2 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package version
import "fmt"

type Version struct {
Version string `json:"version"`
GitCommit string `json:"git-commit"`
Version string
GitCommit string
}

func (v *Version) String() string {
Expand Down

0 comments on commit b456dac

Please sign in to comment.