Skip to content

Commit

Permalink
Merge pull request #76 from tamada/issue/introduce_dryrun_for_rrhnew
Browse files Browse the repository at this point in the history
ref #75 introduce dry-run option to rrh new command, and #74 fixing invalid permission.
  • Loading branch information
tamada committed Jul 13, 2019
2 parents cbcf8ac + 11dce44 commit 0d37abe
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
8 changes: 3 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
GO=go
NAME := rrh
VERSION := 1.0.3
VERSION := 1.0.4
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'main.version=$(VERSION)'
-X 'main.revision=$(REVISION)'

all: test build

Expand Down Expand Up @@ -44,8 +42,8 @@ format: setup
goimports -w $$(go list ./... | sed 's/github.com\/tamada\/rrh//g' | sed 's/^\///g')

install: test build
$(GO) install $(LDFLAGS)
. ./completions/rrh_completion.bash
$(GO) install
. ./completions/bash/rrh

clean:
$(GO) clean
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![codebeat badge](https://codebeat.co/badges/15e04551-d448-4ad3-be1d-e98b1e586f1a)](https://codebeat.co/projects/github-com-tamada-rrh-master)
[![go report](https://goreportcard.com/badge/github.com/tamada/rrh)](https://goreportcard.com/report/github.com/tamada/rrh)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/tamada/rrh/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-1.0.3-yellowgreen.svg)](https://github.com/tamada/rrh/releases/tag/v1.0.3)
[![Version](https://img.shields.io/badge/Version-1.0.4-yellowgreen.svg)](https://github.com/tamada/rrh/releases/tag/v1.0.4)

# RRH

Expand Down
46 changes: 34 additions & 12 deletions cmd/rrh-new/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ type newOptions struct {
homepage string
parentPath string
privateFlag bool
dryrunMode bool
helpFlag bool
}

func getHelpMessage() string {
return `rrh new [OPTIONS] <[ORGANIZATION/]REPOSITORIES...>
OPTIONS
-d, --description <DESC> specifies short description of the repository.
-D, --dry-run performs on dry run mode.
-g, --group <GROUP> specifies group name.
-H, --homepage <URL> specifies homepage url.
-d, --description <DESC> specifies short description of the repository.
-p, --private create a private repository.
-p, --parent-path <PATH> specifies the destination path (default: '.').
-P, --parent-path <PATH> specifies the destination path (default: '.').
-h, --help print this message.
ARGUMENTS
ORGANIZATION specifies organization, if needed.
Expand All @@ -49,6 +51,7 @@ func buildFlagSet(config *lib.Config) (*flag.FlagSet, *newOptions) {
flags.StringVarP(&opt.homepage, "homepage", "H", "", "specifies homepage url")
flags.StringVarP(&opt.parentPath, "parent-path", "P", ".", "specifies the destination path")
flags.BoolVarP(&opt.privateFlag, "private", "p", false, "create a private repository")
flags.BoolVarP(&opt.dryrunMode, "dry-run", "D", false, "performs on dry run mode")
flags.BoolVarP(&opt.helpFlag, "help", "h", false, "print this message")
return flags, &opt
}
Expand All @@ -70,27 +73,34 @@ func createArgsToHubCommand(projectName string, opts *newOptions) []string {
return args
}

func createProjectPage(repo *repo, opts *newOptions) error {
func createProjectPage(repo *repo, opts *newOptions) (string, error) {
var argsToHub = createArgsToHubCommand(repo.givenString, opts)
var cmdString = "hub " + strings.Join(argsToHub, " ")
if opts.dryrunMode {
return cmdString, nil
}
var cmd = exec.Command("hub", argsToHub...)
cmd.Dir = repo.dest
var _, err = cmd.Output()
if err != nil {
return fmt.Errorf("%s: %s", repo.givenString, err.Error())
return "", fmt.Errorf("%s: %s", repo.givenString, err.Error())
}
return nil
return cmdString, nil
}

func createReadme(dest, projectName string) {
var path = filepath.Join(dest, "README.md")
var file, err = os.OpenFile(path, os.O_CREATE, 644)
var file, err = os.OpenFile(path, os.O_CREATE, 0644)
defer file.Close()
if err == nil {
file.WriteString(fmt.Sprintf("# %s", projectName))
}
}

func makeGitDirectory(config *lib.Config, repo *repo, opts *newOptions) error {
if opts.dryrunMode {
return nil
}
os.MkdirAll(repo.dest, 0755)
var gitDir = filepath.Join(repo.dest, ".git")
var fsys = osfs.New(gitDir)
Expand Down Expand Up @@ -126,7 +136,15 @@ func findDirectoryName(arg string, opts *newOptions) (string, error) {
if availableDir(opts, dest) {
return "", fmt.Errorf("%s/%s: directory exist", opts.parentPath, dest)
}
return dest, nil
return convertToAbsolutePath(dest, opts)
}

func convertToAbsolutePath(dest string, opts *newOptions) (string, error) {
var abs, err = filepath.Abs(filepath.Join(opts.parentPath, dest))
if err != nil {
return "", err
}
return abs, nil
}

func findRepoName(arg string) string {
Expand All @@ -143,10 +161,13 @@ func createRepo(config *lib.Config, arg string, opts *newOptions) (*repo, error)
return nil, err
}
var repoName = findRepoName(arg)
return &repo{givenString: arg, dest: filepath.Join(opts.parentPath, dest), repoName: repoName}, nil
return &repo{givenString: arg, dest: dest, repoName: repoName}, nil
}

func registerToGroup(db *lib.Database, repo *repo, opts *newOptions) error {
if opts.dryrunMode {
return nil
}
var remotes, _ = lib.FindRemotes(repo.dest)
var _, err1 = db.CreateRepository(repo.repoName, repo.dest, opts.description, remotes)
if err1 != nil {
Expand All @@ -163,15 +184,16 @@ func createRepository(db *lib.Database, arg string, opts *newOptions) error {
var repo, err = createRepo(db.Config, arg, opts)
if err == nil {
err = makeGitDirectory(db.Config, repo, opts)
fmt.Printf("1/3 create git directory on %s\n", repo.dest)
fmt.Printf("1/3 create git directory on \"%s\"\n", repo.dest)
}
if err == nil {
err = createProjectPage(repo, opts)
fmt.Printf("2/3 create remote repository of %s\n", repo.repoName)
var cmd string
cmd, err = createProjectPage(repo, opts)
fmt.Printf("2/3 create remote repository of %s by \"%s\"\n", repo.repoName, cmd)
}
if err == nil {
err = registerToGroup(db, repo, opts)
fmt.Printf("3/3 add %s to rrh database with group %s\n", repo.repoName, opts.group)
fmt.Printf("3/3 add repository \"%s\" to group \"%s\"\n", repo.repoName, opts.group)
}
return err
}
Expand Down
18 changes: 17 additions & 1 deletion completions/bash/rrh
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ __rrh_mv() {
fi
}

__rrh_new() {
if [[ "$1" =~ ^\- ]]; then
COMPREPLY=($(compgen -W "-d --description -D --dry-run -g --group -H --homepage -p --private -P --parent-path -h --help" -- "${cur}"))
elif [ "$2" == "-g" ] || [ "$2" == "--group" ] ; then
groups="$(__rrh_groups)"
COMPREPLY=($(compgen -W "$groups" -- "${cur}"))
elif [ "$2" == "-P" ] || [ "$2" == "--parent-path" ] ; then
compopt -o filenames
COMPREPLY=($(compgen -d -- "$1"))
fi
}

__rrh_path() {
if [[ "$1" =~ ^\- ]]; then
COMPREPLY=($(compgen -W "-m --partial-match -p --show-only-path" -- "${cur}"))
Expand Down Expand Up @@ -285,7 +297,7 @@ __rrh_completions()
subcom=${COMP_WORDS[$subcomIndex]}
fi
# echo "cur: $cur, prev: $prev, cword: $cword, subcom: $subcom, index: $subcomIndex"
opts="add clone config export fetch fetch-all group help import list mv prune repository rm status version"
opts="add clone config export fetch fetch-all group help import list mv new prune repository rm status version"

case "${subcom}" in
add)
Expand Down Expand Up @@ -332,6 +344,10 @@ __rrh_completions()
__rrh_mv "$cur" "$prev" "$cword" "$subcom" $subcomIndex
return 0
;;
new)
__rrh_new "$cur" "$prev" "$cword" "$subcom" $subcomIndex
return 0
;;
prune)
return 0
;;
Expand Down
2 changes: 1 addition & 1 deletion docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: RRH
[![codebeat badge](https://codebeat.co/badges/15e04551-d448-4ad3-be1d-e98b1e586f1a)](https://codebeat.co/projects/github-com-tamada-rrh-master)
[![go report](https://goreportcard.com/badge/github.com/tamada/rrh)](https://goreportcard.com/report/github.com/tamada/rrh)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/tamada/rrh/blob/master/LICENSE)
[![Version](https://img.shields.io/badge/Version-1.0.3-yellowgreen.svg)](https://github.com/tamada/rrh/releases/tag/v1.0.3)
[![Version](https://img.shields.io/badge/Version-1.0.4-yellowgreen.svg)](https://github.com/tamada/rrh/releases/tag/v1.0.4)

RRH is a simple git repository manager.

Expand Down
2 changes: 1 addition & 1 deletion internal/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func ExampleVersionCommand_Run() {
var command, _ = VersionCommandFactory()
command.Run([]string{})
// Output:
// rrh version 1.0.3
// rrh version 1.0.4
}
2 changes: 1 addition & 1 deletion lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
/*
VERSION shows the version of RRH.
*/
const VERSION = "1.0.3"
const VERSION = "1.0.4"

/*
The environment variable names.
Expand Down

0 comments on commit 0d37abe

Please sign in to comment.