changeset 0:c0a01cfbf234

init project
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Wed, 08 Apr 2020 07:09:03 +0900
parents
children 76695bcbe426
files LICENSE README.md cmd.go cmd/growsync/main.go go.mod go.sum growsync.go version.go
diffstat 8 files changed, 175 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LICENSE	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,22 @@
+Copyright (c) 2020 AnaTofuZ
+
+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.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,32 @@
+growsync
+=======
+
+[![Test Status](https://github.com/AnaTofuZ/growsync/workflows/test/badge.svg?branch=master)][actions]
+[![Coverage Status](https://coveralls.io/repos/AnaTofuZ/growsync/badge.svg?branch=master)][coveralls]
+[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license]
+[![GoDoc](https://godoc.org/github.com/AnaTofuZ/growsync?status.svg)][godoc]
+
+[actions]: https://github.com/AnaTofuZ/growsync/actions?workflow=test
+[coveralls]: https://coveralls.io/r/AnaTofuZ/growsync?branch=master
+[license]: https://github.com/AnaTofuZ/growsync/blob/master/LICENSE
+[godoc]: https://godoc.org/github.com/AnaTofuZ/growsync
+
+growsync short description
+
+## Synopsis
+
+```go
+// simple usage here
+```
+
+## Description
+
+## Installation
+
+```console
+% go get github.com/AnaTofuZ/growsync
+```
+
+## Author
+
+[AnaTofuZ](https://github.com/AnaTofuZ)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd.go	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,12 @@
+package growsync
+
+import (
+	"context"
+	"io"
+)
+
+type cmd interface {
+	name() string
+	description() string
+	run(context.Context, []string, io.Writer, io.Writer) error
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmd/growsync/main.go	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,23 @@
+package main
+
+import (
+	"context"
+	"flag"
+	"log"
+	"os"
+
+	"www.cr.ie.u-ryukyu.ac.jp/hg/Members/anatofuz/growsync"
+)
+
+func main() {
+	log.SetFlags(0)
+	err := growsync.Run(context.Background(), os.Args[1:], os.Stdout, os.Stderr)
+	if err != nil && err != flag.ErrHelp {
+		log.Println(err)
+		exitCode := 1
+		if ecoder, ok := err.(interface{ ExitCode() int }); ok {
+			exitCode = ecoder.ExitCode()
+		}
+		os.Exit(exitCode)
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/go.mod	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,5 @@
+module www.cr.ie.u-ryukyu.ac.jp/hg/Members/anatofuz/growsync
+
+go 1.13
+
+require github.com/pkg/errors v0.9.1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/go.sum	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,2 @@
+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/growsync.go	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,74 @@
+package growsync
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"io"
+	"log"
+
+	"github.com/pkg/errors"
+)
+
+const cmdName = "growsync"
+
+var (
+	subCommands       = []cmd{}
+	dispatch          = make(map[string]cmd, len(subCommands))
+	maxSubcommandName int
+)
+
+func init() {
+	for _, r := range subCommands {
+		n := r.name()
+		l := len(n)
+		if l > maxSubcommandName {
+			maxSubcommandName = l
+		}
+		dispatch[n] = r
+	}
+}
+
+// Run the lectable
+func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) error {
+	log.SetOutput(errStream)
+	nameAndVer := fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision)
+	fs := flag.NewFlagSet(nameAndVer, flag.ContinueOnError)
+	fs.SetOutput(errStream)
+	fs.Usage = func() {
+		fmt.Fprintf(fs.Output(), "Usage of %s:\n", nameAndVer)
+		fs.PrintDefaults()
+		fmt.Fprintf(fs.Output(), "\nCommands:\n")
+		formatCommands(fs.Output())
+	}
+	ver := fs.Bool("version", false, "display version")
+	if err := fs.Parse(argv); err != nil {
+		return err
+	}
+	if *ver {
+		return printVersion(outStream)
+	}
+
+	argv = fs.Args()
+	if len(argv) < 1 {
+		fs.Usage()
+		return errors.New("no subcommand specified")
+	}
+	rnr, ok := dispatch[argv[0]]
+	if !ok {
+		return errors.Errorf("unknown subcommand: %s", argv[0])
+	}
+	return rnr.run(context.Background(), argv[1:], outStream, errStream)
+}
+
+func printVersion(out io.Writer) error {
+	_, err := fmt.Fprintf(out, "%s v%s (rev:%s)\n", cmdName, version, revision)
+	return err
+}
+
+func formatCommands(out io.Writer) {
+	format := fmt.Sprintf("    %%-%ds  %%s\n", maxSubcommandName)
+	for _, r := range subCommands {
+		fmt.Fprintf(out, format, r.name(), r.description())
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/version.go	Wed Apr 08 07:09:03 2020 +0900
@@ -0,0 +1,5 @@
+package growsync
+
+const version = "0.0.0"
+
+var revision = "HEAD"