Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][dcli] Init again #117 #127

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
93701f5
[dcli] Init context and command
at15 Mar 31, 2020
c976d78
[util] Use goimports when formatting generated go code
at15 Apr 2, 2020
f37b3bd
[dcli] Add buildinfo and default vars for ldflags
at15 Apr 3, 2020
1a29436
[fsutil] Add ignore pattern that accepts all
at15 Apr 7, 2020
1222f6c
[dcli] Init Command interface and default impl
at15 Apr 7, 2020
ebd3423
[hashutil] Add fnv32a for pm
at15 May 30, 2020
2d5761f
[httpclient] Add PUT
at15 Jun 9, 2020
cb020cd
[dcli] Add command dispatch w/o flag and arguments
at15 Jun 29, 2020
dce6d07
[fsutil] Add CreateFileAndPath
at15 Jul 26, 2020
caee3c8
[genutil] Add SnakeToCamel
at15 Aug 5, 2020
7d51fb0
[doc] Add developer log and init empty milestones
at15 Aug 6, 2020
da1d6e4
[doc][milestone] Add linter
at15 Aug 7, 2020
6dedbfc
[gommon] Add gommon format #118
at15 Aug 7, 2020
07ef59e
[linter][import] Add goimports flags
at15 Aug 7, 2020
a9d58a9
[linter] Add print diff to gommon format
at15 Aug 8, 2020
acacb6c
[linter][import] gommon format can replace goimports
at15 Aug 8, 2020
8c4f879
[dcli] Add ErrHelpOnlyCommand and common error handler
at15 Aug 8, 2020
da834e6
[linter][import] Add example of using LocalPrefix
at15 Aug 9, 2020
a241ea1
[linter][import] Split print diff logic out
at15 Aug 9, 2020
8d56ab1
[dcli] Support longer than 1 sub command
at15 Aug 9, 2020
8ddf048
[linter][import] Add lint and format rule w/o impl
at15 Aug 11, 2020
f985a5c
[generator] Add RenderGoCode
at15 Aug 12, 2020
19c3129
[stringutil] Add Set
at15 Aug 24, 2020
16e6b14
[tconfig] Init package and Add log
at15 Aug 27, 2020
2402b3d
[tconfig] Support customize bool eval function
at15 Aug 27, 2020
dc5b3f8
[hack] Update go-dev to 1.15.3
at15 Oct 25, 2020
fef6316
[hack] Disable cgo
at15 Oct 25, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
- docker

go:
- "1.13"
- "1.15"
- "1.14"
- tip

Expand All @@ -16,6 +16,7 @@ install:
- go get -u golang.org/x/tools/cmd/goimports

script:
- make install-only
- make install
- make test
- make test-race
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# The builder-image go-dev can be found in hack/go-dev
# Versions can be found on https://hub.docker.com/r/dyweb/go-dev/tags
FROM dyweb/go-dev:1.13.6 as builder
FROM dyweb/go-dev:1.15.3 as builder

LABEL maintainer="[email protected]"

Expand All @@ -27,4 +27,4 @@ LABEL github="github.com/dyweb/gommon"
WORKDIR /usr/bin
COPY --from=builder /go/bin/gommon .
ENTRYPOINT ["gommon"]
CMD ["help"]
CMD ["help"]
38 changes: 25 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# based on https://gist.github.com/azatoth/1030091
# TODO(at15): it is also possible to generate it automatically using awk etc.
define GOMMON_MAKEFILE_HELP_MSG
Make commands for gommon

help show help

Dev:
Dev
-----------------------------------------
install install binaries under ./cmd to $$GOPATH/bin
fmt goimports
test unit test
test run unit test
generate generate code using gommon
loc lines of code (cloc required, brew install cloc)

Build:
Build
-----------------------------------------
install install all binaries under ./cmd to $$GOPATH/bin
build compile all binary to ./build for current platform
build-linux compile all linux binary to ./build with -linux suffix
build-mac compile all mac binary to ./build with -mac suffix
build-win compile all windows binary to ./build with -win suffix
build-release compile binary for all platforms and generate tarball to ./build

Docker:
Docker
-----------------------------------------
docker-build build runner image w/ all binaries using mulitstage build
docker-push push runner image to docker registry

Expand All @@ -31,25 +35,38 @@ export GOMMON_MAKEFILE_HELP_MSG
help:
@echo "$$GOMMON_MAKEFILE_HELP_MSG"

GO = GO111MODULE=on go
GO = GO111MODULE=on CGO_ENABLED=0 go
# -- build vars ---
PKGS =./errors/... ./generator/... ./httpclient/... ./log/... ./noodle/... ./util/...
PKGST =./cmd ./errors ./generator ./httpclient ./log ./noodle ./util
PKGST =./cmd ./dcli ./errors ./generator ./httpclient ./linter ./log ./noodle ./util ./tconfig
PKGS = $(addsuffix ...,$(PKGST))
VERSION = 0.0.13
BUILD_COMMIT := $(shell git rev-parse HEAD)
BUILD_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
BUILD_TIME := $(shell date +%Y-%m-%dT%H:%M:%S%z)
CURRENT_USER = $(USER)
FLAGS = -X main.version=$(VERSION) -X main.commit=$(BUILD_COMMIT) -X main.buildTime=$(BUILD_TIME) -X main.buildUser=$(CURRENT_USER)
DOCKER_REPO = dyweb/gommon
DCLI_PKG = github.com/dyweb/gommon/dcli.
DCLI_LDFLAGS = -X $(DCLI_PKG)buildVersion=$(VERSION) -X $(DCLI_PKG)buildCommit=$(BUILD_COMMIT) -X $(DCLI_PKG)buildBranch=$(BUILD_BRANCH) -X $(DCLI_PKG)buildTime=$(BUILD_TIME) -X $(DCLI_PKG)buildUser=$(CURRENT_USER)
# -- build vars ---

.PHONY: install
install: fmt test
install: fmt test install-only

install-only:
cd ./cmd/gommon && $(GO) install -ldflags "$(FLAGS)" .
mv $(GOPATH)/bin/gommonbin $(GOPATH)/bin/gommon

.PHONY: install2
install2:
cd ./cmd/gommon2 && $(GO) install -ldflags "$(DCLI_LDFLAGS)" .

.PHONY: fmt
fmt:
gommon format -d -l -w $(PKGST)

# gommon format is a drop in replacement for goimports
deprecated-fmt:
goimports -d -l -w $(PKGST)

# --- build ---
Expand Down Expand Up @@ -136,11 +153,6 @@ docker-build:
docker-push:
docker push $(DOCKER_REPO):$(VERSION)

docker-test:
docker-compose -f hack/docker-compose.yml run --rm golang1.12
# TODO: not sure why the latest one is not using ...
# docker-compose -f hack/docker-compose.yml run --rm golanglatest

#.PHONY: docker-remove-all-containers
#docker-remove-all-containers:
# docker rm $(shell docker ps -a -q)
Expand Down
3 changes: 3 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Roadmap

NOTE: it is being moved to [milestones](doc/milestones)

## Up coming

### 0.0.14

- [ ] dcli
- [ ] wait package, similar to the polling package in k8s
- [ ] have retry as alias and provides backoff
- [ ] allow use wait for container
Expand Down
6 changes: 4 additions & 2 deletions cmd/gommon/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
go 1.13
go 1.14

require (
github.com/dyweb/gommon v0.0.13
github.com/spf13/cobra v0.0.6
golang.org/x/tools v0.0.0-20200401192744-099440627f01
)

replace github.com/dyweb/gommon v0.0.13 => ../..
replace github.com/dyweb/gommon => ../..

// TODO: might name is gom
// NOTE: rename it to gommonbin to aviod ambiguous import
// can't load package: package github.com/dyweb/gommon/cmd/gommon: ambiguous import: found github.com/dyweb/gommon/cmd/gommon in multiple modules:
// github.com/dyweb/gommon/cmd/gommon (/home/at15/w/src/github.com/dyweb/gommon/cmd/gommon)
Expand Down
17 changes: 17 additions & 0 deletions cmd/gommon/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,52 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200401192744-099440627f01 h1:ysQJ/fU6laLOZJseIeOqXl6Mo+lw5z6b7QHnmUKjW+k=
golang.org/x/tools v0.0.0-20200401192744-099440627f01/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
58 changes: 57 additions & 1 deletion cmd/gommon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strings"

"github.com/dyweb/gommon/linter"
"github.com/spf13/cobra"

"github.com/dyweb/gommon/errors"
Expand All @@ -33,7 +34,6 @@ var (
)

func main() {
// TODO: most code here are copied from go.ice's cli package, dependency management might break if we import go.ice which also import gommon
rootCmd := &cobra.Command{
Use: "gommon",
Short: "gommon helpers",
Expand Down Expand Up @@ -73,13 +73,15 @@ func main() {
versionCmd,
genCmd(),
addBuildIgnoreCmd(),
formatCmd(),
)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

// triggers generator for logger, go template and embedding asset
func genCmd() *cobra.Command {
gen := cobra.Command{
Use: "generate",
Expand Down Expand Up @@ -141,6 +143,7 @@ func genCmd() *cobra.Command {
return &gen
}

// add // +build ignore to files before moving them to legacy folder
func addBuildIgnoreCmd() *cobra.Command {
cmd := cobra.Command{
Use: "add-build-ignore",
Expand Down Expand Up @@ -194,6 +197,59 @@ func addBuildIgnoreCmd() *cobra.Command {
return &cmd
}

// gommon format
func formatCmd() *cobra.Command {
var flags linter.GoimportFlags
processFile := func(path string, info os.FileInfo, err error) error {
if err == nil && fsutil.IsGoFile(info) {
return linter.CheckAndFormatImportToStdout(path, flags)
}
// Skip directory and stop on walk error
return err
}

run := func(paths []string) error {
for _, p := range paths {
switch dir, err := os.Stat(p); {
case err != nil:
return err
case dir.IsDir():
// TODO: walk w/ ignore like generator
if err := filepath.Walk(p, processFile); err != nil {
return err
}
default:
if err := processFile(p, dir, nil); err != nil {
return err
}
}
}
return nil
}

cmd := cobra.Command{
Use: "format",
Short: "Format go code like goimports with custom rules",
Run: func(cmd *cobra.Command, args []string) {
paths := args
if len(paths) == 0 {
log.Fatal("format stdin is not implemented")
return
}
if err := run(paths); err != nil {
log.Fatal(err)
}
},
}
cmd.Flags().BoolVarP(&flags.List, "list", "l", false, "list files whose formatting differs from goimports")
cmd.Flags().BoolVarP(&flags.Write, "write", "w", false, "write result to (source) file instead of stdout, i.e. in place update")
cmd.Flags().BoolVarP(&flags.Diff, "diff", "d", false, "display diffs instead of rewriting files")
cmd.Flags().BoolVarP(&flags.AllErrors, "errors", "e", false, "report all errors (not just the first 10 on different lines)")
cmd.Flags().StringVar(&flags.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
cmd.Flags().BoolVar(&flags.FormatOnly, "format-only", false, "if true, don't fix imports and only format. In this mode, goimports is effectively gofmt, with the addition that imports are grouped into sections.")
return &cmd
}

func init() {
dlog.SetHandler(cli.New(os.Stderr, true))
}
23 changes: 23 additions & 0 deletions cmd/gommon2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// gommon2 is a test binary for using dcli, it will be renamed to gommon once we have most functionality in spf13/cobra
package main

import (
"context"

"github.com/dyweb/gommon/dcli"
dlog "github.com/dyweb/gommon/log"
)

var logReg = dlog.NewRegistry()
var log = logReg.NewLogger()

func main() {
root := &dcli.Cmd{
Name: "gommon2",
Run: func(ctx context.Context) error {
log.Info("gommon2 does nothing")
return nil
},
}
dcli.RunApplication(root)
}
13 changes: 10 additions & 3 deletions dcli/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
# dcli

dcli is a light weight cli builder.
dcli is a lightweight cli builder.

## Usage

```go

```

## Issues

- [Create a new package #117](https://github.com/dyweb/gommon/issues/117)

## Reference and Alternatives

- [spf13/cobra](https://github.com/spf13/cobra)
- [spf13/cobra](https://github.com/spf13/cobra) Imports etcd and consul ...
- [peterbourgon/ff](https://github.com/peterbourgon/ff) Flag first package for configuration
- [urfave/cli](https://github.com/urfave/cli)
- [mitchellh/cli](https://github.com/mitchellh/cli) Used by consul, terraform etc.
- [mitchellh/cli](https://github.com/mitchellh/cli) Used by consul, terraform etc.
- [alecthomas/kong](https://github.com/alecthomas/kong) Define command using struct and struct tag
Loading