Skip to content

Commit

Permalink
Merge pull request #374 from gusandrioli/check-cheatsheet-is-up-to-date
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield authored Oct 11, 2022
2 parents 26858c3 + 574e0b6 commit 38959a0
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 85 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ jobs:
- name: Build darwin binary
run: |
GOOS=darwin go build
check-codebase:
runs-on: ubuntu-latest
env:
GOFLAGS: -mod=vendor
GOARCH: amd64
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.18.x
- name: Cache build
uses: actions/cache@v1
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{runner.os}}-go-${{hashFiles('**/go.sum')}}-build
restore-keys: |
${{runner.os}}-go-
- name: Check Cheatsheet
run: |
go run scripts/cheatsheet/main.go check
- name: Check Vendor Directory
# ensure our vendor directory matches up with our go modules
run: |
go mod vendor && git diff --exit-code || (echo "Unexpected change to vendor directory. Run 'go mod vendor' locally and commit the changes" && exit 1)
lint:
runs-on: ubuntu-latest
env:
Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_de.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root._
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root._

# Lazydocker menü

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root._
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root._

# Lazydocker menu

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_nl.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root._
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root._

# Lazydocker menu

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_pl.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root._
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root._

# Lazydocker menu

Expand Down
2 changes: 1 addition & 1 deletion docs/keybindings/Keybindings_tr.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/generate_cheatsheet.go` from the project root._
_This file is auto-generated. To update, make the changes in the pkg/i18n directory and then run `go run scripts/cheatsheet/main.go generate` from the project root._

# Lazydocker menü

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ require (
github.com/docker/docker v20.10.15+incompatible
github.com/fatih/color v1.7.0
github.com/go-errors/errors v1.4.2
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/gookit/color v1.5.0
github.com/imdario/mergo v0.3.8
github.com/integrii/flaggy v1.4.0
github.com/jesseduffield/asciigraph v0.0.0-20190605104717-6d88e39309ee
github.com/jesseduffield/gocui v0.3.1-0.20220417002912-bce22fd599f6
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10
github.com/jesseduffield/lazycore v0.0.0-20221009164442-17c8b878c316
github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93
github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56
github.com/mattn/go-runewidth v0.0.13
github.com/mcuadros/go-lookup v0.0.0-20171110082742-5650f26be767
github.com/mgutz/str v1.2.0
github.com/pmezard/go-difflib v1.0.0
github.com/samber/lo v1.31.0
github.com/sirupsen/logrus v1.4.2
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad
Expand Down Expand Up @@ -48,7 +48,6 @@ require (
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxI
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4=
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -54,8 +52,8 @@ github.com/jesseduffield/gocui v0.3.1-0.20220417002912-bce22fd599f6 h1:Fmay0Lz21
github.com/jesseduffield/gocui v0.3.1-0.20220417002912-bce22fd599f6/go.mod h1:znJuCDnF2Ph40YZSlBwdX/4GEofnIoWLGdT4mK5zRAU=
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0=
github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo=
github.com/jesseduffield/lazycore v0.0.0-20221009164442-17c8b878c316 h1:UzTg0utG+cLS94Qjb/ZFDzfMeZDFXErSbNgJOyj70EA=
github.com/jesseduffield/lazycore v0.0.0-20221009164442-17c8b878c316/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ=
github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93 h1:zY7ymCjXC7fZeJVXDpiicYx6c2YFgvsdZSOdch2f7gU=
github.com/jesseduffield/lazycore v0.0.0-20221010211550-2c30efd18b93/go.mod h1:qxN4mHOAyeIDLP7IK7defgPClM/z1Kze8VVQiaEjzsQ=
github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56 h1:33wSxJWU/f2TAozHYtJ8zqBxEnEVYM+22moLoiAkxvg=
github.com/jesseduffield/yaml v0.0.0-20190702115811-b900b7e08b56/go.mod h1:FZJBwOhE+RXz8EVZfY+xnbCw2cVOwxlK3/aIi581z/s=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
Expand Down
15 changes: 10 additions & 5 deletions scripts/generate_cheatsheet.go → pkg/cheatsheet/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// The content of this generated file is a keybindings cheatsheet.
//
// To generate cheatsheet in english run:
// LANG=en go run scripts/generate_cheatsheet.go
// LANG=en go run scripts/cheatsheet/main.go generate

package main
package cheatsheet

import (
"fmt"
Expand All @@ -20,15 +20,19 @@ import (
)

const (
generateCheatsheetCmd = "go run scripts/generate_cheatsheet.go"
generateCheatsheetCmd = "go run scripts/cheatsheet/main.go generate"
)

type bindingSection struct {
title string
bindings []*gui.Binding
}

func main() {
func Generate() {
generateAtDir(GetKeybindingsDir())
}

func generateAtDir(dir string) {
mConfig, err := config.NewAppConfig("lazydocker", "", "", "", "", true, nil, "")
if err != nil {
panic(err)
Expand All @@ -37,7 +41,8 @@ func main() {
for lang := range i18n.GetTranslationSets() {
os.Setenv("LC_ALL", lang)
mApp, _ := app.NewApp(mConfig)
file, err := os.Create("./docs/keybindings/Keybindings_" + lang + ".md")

file, err := os.Create(dir + "/Keybindings_" + lang + ".md")
if err != nil {
panic(err)
}
Expand Down
87 changes: 87 additions & 0 deletions pkg/cheatsheet/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cheatsheet

import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"

"github.com/jesseduffield/lazycore/pkg/utils"
"github.com/pmezard/go-difflib/difflib"
)

func Check() {
dir := GetKeybindingsDir()
tmpDir := filepath.Join(os.TempDir(), "lazydocker_cheatsheet")

err := os.RemoveAll(tmpDir)
if err != nil {
log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
}
defer os.RemoveAll(tmpDir)

if err = os.Mkdir(tmpDir, 0o700); err != nil {
log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
}

generateAtDir(tmpDir)

actualContent := obtainContent(dir)
expectedContent := obtainContent(tmpDir)

if expectedContent == "" {
log.Fatal("empty expected content")
}

if actualContent != expectedContent {
if err := difflib.WriteUnifiedDiff(os.Stdout, difflib.UnifiedDiff{
A: difflib.SplitLines(expectedContent),
B: difflib.SplitLines(actualContent),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 1,
}); err != nil {
log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
}
fmt.Printf(
"\nCheatsheets are out of date. Please run `%s` at the project root and commit the changes. "+
"If you run the script and no keybindings files are updated as a result, try rebasing onto master"+
"and trying again.\n",
generateCheatsheetCmd,
)
os.Exit(1)
}

fmt.Println("\nCheatsheets are up to date")
}

func GetKeybindingsDir() string {
return utils.GetLazyRootDirectory() + "/docs/keybindings"
}

func obtainContent(dir string) string {
re := regexp.MustCompile(`Keybindings_\w+\.md$`)

content := ""
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if re.MatchString(path) {
bytes, err := os.ReadFile(path)
if err != nil {
log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
}
content += fmt.Sprintf("\n%s\n\n", filepath.Base(path))
content += string(bytes)
}

return nil
})
if err != nil {
log.Fatalf("Error occurred while checking if cheatsheets are up to date: %v", err)
}

return content
}
27 changes: 27 additions & 0 deletions scripts/cheatsheet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"log"
"os"

"github.com/jesseduffield/lazydocker/pkg/cheatsheet"
)

func main() {
if len(os.Args) < 2 {
log.Fatal("Please provide a command: one of 'generate', 'check'")
}

command := os.Args[1]

switch command {
case "generate":
cheatsheet.Generate()
fmt.Printf("\nGenerated cheatsheets in %s\n", cheatsheet.GetKeybindingsDir())
case "check":
cheatsheet.Check()
default:
log.Fatal("\nUnknown command. Expected one of 'generate', 'check'")
}
}
20 changes: 0 additions & 20 deletions vendor/github.com/golang-collections/collections/LICENSE

This file was deleted.

44 changes: 0 additions & 44 deletions vendor/github.com/golang-collections/collections/stack/stack.go

This file was deleted.

35 changes: 35 additions & 0 deletions vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go

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

Loading

0 comments on commit 38959a0

Please sign in to comment.