Skip to content

Commit

Permalink
Add headerLevel option (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed May 15, 2020
1 parent 82a1498 commit 8cb0482
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 24 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ jobs:
- checkout
- restore_cache:
keys:
- v1-golangci-lint-{{ checksum "go.sum" }}
- v2-golangci-lint-{{ checksum "go.sum" }}
- run:
name: lint
command: make lint
- save_cache:
key: v1-golangci-lint-{{ checksum "go.sum" }}
key: v2-golangci-lint-{{ checksum "go.sum" }}
paths:
- *gocache
- build/golangci-lint

test:
executor: container
Expand Down
7 changes: 7 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ run:
linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
Expand All @@ -21,6 +22,7 @@ linters:
- gofmt
- goimports
- golint
- gomodguard
- goprintffuncname
- gosec
- gosimple
Expand All @@ -31,20 +33,25 @@ linters:
- maligned
- misspell
- nakedret
- nolintlint
- prealloc
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- testpackage
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
# - funlen
# - godot
# - goerr113
# - gomnd
# - nestif
# - wsl
linters-settings:
errcheck:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $(GO_MODIFF_STATIC):

$(GOLANGCI_LINT):
export \
VERSION=v1.24.0 \
VERSION=v1.27.0 \
URL=https://raw.githubusercontent.com/golangci/golangci-lint \
BINDIR=$(BUILD_PATH) && \
curl -sfL $$URL/$$VERSION/install.sh | sh -s $$VERSION
Expand Down
18 changes: 13 additions & 5 deletions cmd/go-modiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (
)

const (
repositoryArg = "repository"
fromArg = "from"
toArg = "to"
linkArg = "link"
repositoryArg = "repository"
fromArg = "from"
toArg = "to"
linkArg = "link"
headerLevelArg = "header-level"
)

func main() {
const debugFlag = "debug"

app := ccli.NewApp()
app.Name = "go-modiff"
app.Version = "1.0.3"
app.Version = "1.1.0"
app.Authors = []*cli.Author{
{Name: "Sascha Grunert", Email: "mail@saschagrunert.de"},
}
Expand Down Expand Up @@ -57,6 +58,12 @@ func main() {
Aliases: []string{"l"},
Usage: "add diff links to the markdown output",
},
&cli.UintFlag{
Name: headerLevelArg,
Aliases: []string{"i"},
Value: 1,
Usage: "add a higher markdown header level depth",
},
&cli.BoolFlag{
Name: debugFlag,
Aliases: []string{"d"},
Expand Down Expand Up @@ -101,6 +108,7 @@ func main() {
c.String(fromArg),
c.String(toArg),
c.Bool(linkArg),
c.Uint(headerLevelArg),
)
res, err := modiff.Run(config)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions completions/go-modiff.fish
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l repository -s r
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l from -s f -r -d 'the start of the comparison, any valid git rev'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l to -s t -r -d 'the end of the comparison, any valid git rev'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l link -s l -d 'add diff links to the markdown output'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l header-level -s i -r -d 'add a higher markdown header level depth'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l debug -s d -d 'enable debug output'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l help -s h -d 'show help'
complete -c go-modiff -n '__fish_go-modiff_no_subcommand' -f -l version -s v -d 'print the version'
Expand Down
4 changes: 4 additions & 0 deletions docs/go-modiff.8
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go\-modiff
.nf
[\-\-debug|\-d]
[\-\-from|\-f]=[value]
[\-\-header\-level|\-i]=[value]
[\-\-help|\-h]
[\-\-link|\-l]
[\-\-repository|\-r]=[value]
Expand Down Expand Up @@ -50,6 +51,9 @@ go\-modiff [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
.PP
\fB\-\-from, \-f\fP="": the start of the comparison, any valid git rev (default: master)

.PP
\fB\-\-header\-level, \-i\fP="": add a higher markdown header level depth (default: 1)

.PP
\fB\-\-help, \-h\fP: show help

Expand Down
3 changes: 3 additions & 0 deletions docs/go-modiff.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go-modiff
```
[--debug|-d]
[--from|-f]=[value]
[--header-level|-i]=[value]
[--help|-h]
[--link|-l]
[--repository|-r]=[value]
Expand All @@ -34,6 +35,8 @@ go-modiff [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]

**--from, -f**="": the start of the comparison, any valid git rev (default: master)

**--header-level, -i**="": add a higher markdown header level depth (default: 1)

**--help, -h**: show help

**--link, -l**: add diff links to the markdown output
Expand Down
25 changes: 15 additions & 10 deletions pkg/modiff/modiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ type modules = map[string]versions

// Config is the structure passed to `Run`
type Config struct {
repository string
from string
to string
link bool
repository string
from string
to string
link bool
headerLevel uint
}

// NewConfig creates a new configuration
func NewConfig(repository, from, to string, link bool) *Config {
return &Config{repository, from, to, link}
func NewConfig(repository, from, to string, link bool, headerLevel uint) *Config {
return &Config{repository, from, to, link, headerLevel}
}

// Run starts go modiff and returns the markdown string
Expand Down Expand Up @@ -74,7 +75,7 @@ func Run(config *Config) (string, error) {
if err != nil {
return "", err
}
return diffModules(mods, config.link), nil
return diffModules(mods, config.link, config.headerLevel), nil
}

func toURL(name string) string {
Expand All @@ -95,7 +96,7 @@ func logErr(msg interface{}) (string, error) {
return "", err
}

func diffModules(mods modules, addLinks bool) string {
func diffModules(mods modules, addLinks bool, headerLevel uint) string {
var added, removed, changed []string
for name, mod := range mods {
txt := fmt.Sprintf("- %s: ", name)
Expand Down Expand Up @@ -135,9 +136,13 @@ func diffModules(mods modules, addLinks bool) string {

// Pretty print
builder := &strings.Builder{}
builder.WriteString("# Dependencies\n")
builder.WriteString(fmt.Sprintf(
"%s Dependencies\n", strings.Repeat("#", int(headerLevel)),
))
forEach := func(section string, input []string) {
builder.WriteString(fmt.Sprintf("\n## %s\n", section))
builder.WriteString(fmt.Sprintf("\n%s %s\n",
strings.Repeat("#", int(headerLevel)+1), section,
))
if len(input) > 0 {
for _, mod := range input {
builder.WriteString(fmt.Sprintf("%s\n", mod))
Expand Down
10 changes: 5 additions & 5 deletions pkg/modiff/modiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ _Nothing has changed._

It("should succeed", func() {
// Given
config := modiff.NewConfig(repo, from, to, false)
config := modiff.NewConfig(repo, from, to, false, 1)

// When
res, err := modiff.Run(config)
Expand All @@ -64,7 +64,7 @@ _Nothing has changed._

It("should succeed with links", func() {
// Given
config := modiff.NewConfig(repo, from, to, true)
config := modiff.NewConfig(repo, from, to, true, 1)

// When
res, err := modiff.Run(config)
Expand All @@ -86,7 +86,7 @@ _Nothing has changed._

It("should fail if 'repository' not given", func() {
// Given
config := modiff.NewConfig("", from, to, true)
config := modiff.NewConfig("", from, to, true, 1)

// When
res, err := modiff.Run(config)
Expand All @@ -98,7 +98,7 @@ _Nothing has changed._

It("should fail if 'from' equals 'to'", func() {
// Given
config := modiff.NewConfig(repo, "", "", true)
config := modiff.NewConfig(repo, "", "", true, 1)

// When
res, err := modiff.Run(config)
Expand All @@ -110,7 +110,7 @@ _Nothing has changed._

It("should fail if repository is not clone-able", func() {
// Given
config := modiff.NewConfig("invalid", from, "", true)
config := modiff.NewConfig("invalid", from, "", true, 1)

// When
res, err := modiff.Run(config)
Expand Down

0 comments on commit 8cb0482

Please sign in to comment.