Skip to content

Commit

Permalink
Merge pull request #13 from Songmu/new
Browse files Browse the repository at this point in the history
implement new subcommand
  • Loading branch information
Songmu committed Feb 17, 2019
2 parents 8dab99e + e1aa7fb commit dfbcb74
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 20 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ devel-deps: deps
github.com/mattn/goveralls \
github.com/Songmu/godzil/cmd/godzil \
github.com/Songmu/goxz/cmd/goxz \
github.com/tcnksm/ghr
github.com/tcnksm/ghr \
github.com/jessevdk/go-assets-builder

test: deps
go test
Expand All @@ -43,4 +44,9 @@ upload:

release: bump crossbuild upload

.PHONY: test deps devel-deps lint cover build bump crossbuild upload release
assets:
@echo '// Code generated by make assets. DO NOT EDIT.' > new_assets_gen.go
@go-assets-builder -p godzil -v templates $$(git ls-files testdata/assets) \
>> new_assets_gen.go

.PHONY: test deps devel-deps lint cover build bump crossbuild upload release assets
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/Songmu/ghch v0.5.0
github.com/Songmu/prompter v0.0.0-20181014095714-d227c68538bd
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/motemen/gobump v0.0.0-20190215161939-66ee926e4810
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/fhs/go-netrc v1.0.0 h1:jbXXfpcwkeNHq5lXXXQO9DTWD7wUqWxgnyPKUe5H4I0=
github.com/fhs/go-netrc v1.0.0/go.mod h1:tGgE+SHFQhgo1jg+hG6/uCxBJv5Pnq7pTMjvaEWrOu8=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jingweno/go-sawyer v0.0.0-20140729165055-1999ae5763d6 h1:2RMaSBb+6kdtJtT7SZfx4OtcjEuRQpAZ9UFU9OiT1BQ=
Expand Down
1 change: 1 addition & 0 deletions godzil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Run the godzil
func Run(argv []string, outStream, errStream io.Writer) error {
log.SetOutput(errStream)
log.SetPrefix("[godzil] ")
fs := flag.NewFlagSet(
fmt.Sprintf("godzil (v%s rev:%s)", version, revision), flag.ContinueOnError)
fs.SetOutput(errStream)
Expand Down
106 changes: 88 additions & 18 deletions new.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package godzil

import (
"bytes"
"flag"
"io"
"log"
"os"
"path/filepath"
"strings"
"text/template"
"time"

"golang.org/x/xerrors"
)

type new struct {
author, email, pkg string
Author, PackagePath string
GitHubHost, Owner, Package string
Year int

outStream, errStream io.Writer
}
Expand All @@ -19,32 +28,93 @@ func (ne *new) run(argv []string, outStream, errStream io.Writer) error {

fs := flag.NewFlagSet("godzil new", flag.ContinueOnError)
fs.SetOutput(errStream)
fs.StringVar(&ne.author, "author", "", "author name")
fs.StringVar(&ne.email, "email", "", "author email")
fs.StringVar(&ne.Author, "Author", "", "Author name")
if err := fs.Parse(argv); err != nil {
return err
}
ne.pkg = fs.Arg(0)
if ne.pkg == "" {
ne.PackagePath = fs.Arg(0)
if ne.PackagePath == "" {
return xerrors.New("no package specified")
}
if ne.Author == "" {
ne.Author, _, _ = git("config", "user.name")
}
return ne.do()
}

if ne.author == "" {
ne.author, _, _ = git("config", "user.name")
func (ne *new) parsePackage() error {
m := strings.Split(ne.PackagePath, "/")
if len(m) < 3 {
return xerrors.Errorf("invalid pacakge path %q. please specify full package path",
ne.PackagePath)
}
if ne.email == "" {
ne.email, _, _ = git("config", "user.email")
ne.GitHubHost = m[0]
ne.Owner = m[1]
ne.Package = m[len(m)-1]
if ne.Author == "" {
ne.Author = ne.Owner
}
ne.Year = time.Now().Year()
return nil
}

// XXX How to detect github username?
// XXX How to detect project location? (how about ghq.root or $GOPATH/src?)
// config? ~/.config/godzil/godzil.yaml
func (ne *new) do() error {
if err := ne.parsePackage(); err != nil {
return err
}
projDir := ne.Package
if _, err := os.Stat(projDir); err == nil {
return xerrors.Errorf("directory %q already exists", projDir)
}
// create project directory and templating
for _, f := range templates.Files {
const prefix = "/testdata/assets/basic"
if f.IsDir() {
continue
}
if !strings.HasPrefix(f.Path, prefix) {
continue
}
targetPathTmpl := strings.Replace(f.Path, prefix, projDir, 1)
buf := &bytes.Buffer{}
if err := template.Must(template.New(f.Path).Parse(targetPathTmpl)).Execute(buf, ne); err != nil {
return xerrors.Errorf("failed to scaffold while resolving targetPath %q: %w",
targetPathTmpl, err)
}
targetPath := buf.String()
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
return xerrors.Errorf("failed to scaffold while MkdirAll of %q: %w",
targetPath, err)
}
err := func() (rerr error) {
log.Printf("Writing %s\n", targetPath)
targetF, err := os.Create(targetPath)
if err != nil {
return err
}
defer func() {
e := targetF.Close()
if rerr == nil && e != nil {
rerr = e
}
}()
return template.Must(template.New(f.Path+".tmpl").Parse(string(f.Data))).
Execute(targetF, ne)
}()
if err != nil {
return xerrors.Errorf("failed to scaffold while templating %q: %w",
targetPath, err)
}
}
log.Println("% git init && git add .")
c := &cmd{outStream: ne.outStream, errStream: ne.errStream}
c.git("-C", projDir, "init")
c.git("-C", projDir, "add", ".")

// TOOD:
// 1. create project repository ({{.Root}}/github.com/{{.User}}/{{.Pkg}})
// 2. scaffold from templates
// 3. git init && git add
if c.err != nil {
return c.err
}
log.Printf("Finished to create %s in %s\n", ne.PackagePath, projDir)
return nil
// 4. create remote repository?

return xerrors.New("not implemented")
}
97 changes: 97 additions & 0 deletions new_assets_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testdata/assets/basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.*
!.gitignore
!.travis.yml
dist/
8 changes: 8 additions & 0 deletions testdata/assets/basic/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go
go:
- 1.x
script:
- make lint
- make test
after_script:
- make cover
22 changes: 22 additions & 0 deletions testdata/assets/basic/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) {{.Year}} {{.Author}}

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 changes: 46 additions & 0 deletions testdata/assets/basic/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
VERSION = $(shell godzil show-version)
CURRENT_REVISION = $(shell git rev-parse --short HEAD)
BUILD_LDFLAGS = "-s -w -X {{.PackagePath}}.revision=$(CURRENT_REVISION)"
ifdef update
u=-u
endif

export GO111MODULE=on

deps:
go get ${u} -d

devel-deps: deps
GO111MODULE=off go get ${u} \
golang.org/x/lint/golint \
github.com/mattn/goveralls \
github.com/Songmu/godzil/cmd/godzil \
github.com/Songmu/goxz/cmd/goxz \
github.com/tcnksm/ghr

test: deps
go test

lint: devel-deps
go vet
golint -set_exit_status

cover: devel-deps
goveralls

build: deps
go build -ldflags=$(BUILD_LDFLAGS) ./cmd/{{.Package}}

bump: devel-deps
godzil release

crossbuild:
goxz -pv=v$(VERSION) -build-ldflags=$(BUILD_LDFLAGS) \
-os=linux,darwin -d=./dist/v$(VERSION) ./cmd/*

upload:
ghr v$(VERSION) dist/v$(VERSION)

release: bump crossbuild upload

.PHONY: test deps devel-deps lint cover build bump crossbuild upload release
32 changes: 32 additions & 0 deletions testdata/assets/basic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{.Package}}
=======

[![Build Status](https://travis-ci.org/{{.Owner}}/{{.Package}}.png?branch=master)][travis]
[![Coverage Status](https://coveralls.io/repos/{{.Owner}}/{{.Package}}/badge.png?branch=master)][coveralls]
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license]
[![GoDoc](https://godoc.org/{{.PackagePath}}?status.svg)][godoc]

[travis]: https://travis-ci.org/{{.Owner}}/{{.Package}}
[coveralls]: https://coveralls.io/r/{{.Owner}}/{{.Package}}?branch=master
[license]: https://{{.GitHubHost}}/{{.Owner}}/{{.Package}}/blob/master/LICENSE
[godoc]: https://godoc.org/{{.PackagePath}}

{{.Package}} short description

## Synopsis

```go
// simple usage here
```

## Description

## Installation

```console
% go get {{.PackagePath}}
```

## Author

[{{.Author}}](https://{{.GitHubHost}}/{{.Author}})
Loading

0 comments on commit dfbcb74

Please sign in to comment.