Skip to content

Commit

Permalink
feat: add new cli feature (update, exclude powerpacks) and persist co…
Browse files Browse the repository at this point in the history
…nfig during init
  • Loading branch information
merlindorin committed Nov 19, 2024
1 parent 15a4686 commit a691489
Show file tree
Hide file tree
Showing 38 changed files with 797 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ linters-settings:
gocognit:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 30
min-complexity: 40

gocritic:
# Settings passed to gocritic.
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ gomod:
# https://goreleaser.com/customization/checksum

checksum:
name_template: '{{ .ProjectName }}-{{ .Version }}-checksums.txt'
name_template: '{{ .ProjectName }}_{{ .Version }}-checksums.txt'

# create a source tarball
# https://goreleaser.com/customization/source/
Expand Down
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
filename: "mock_{{.InterfaceName}}.go"
dir: "{{.InterfaceDir}}"
mockname: "Mock{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
inpackage: True
all: True
packages:
github.com/merlindorin/tk:
config:
recursive: True
4 changes: 4 additions & 0 deletions .tk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ignore_readme: false
ignore_aqua: false
ignore_taskfile: false
excludes: []
6 changes: 5 additions & 1 deletion .tk/default/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ tasks:
Examples:
- `task` will run all default tasks if you use default as prefix
- `task {{.TASK}}` will explicitly run all default tasks
Note:
You can add any extra to all default commands by using "--". For example `task -- -f` to force all update.
vars:
PATTERN: '{{.PATTERN | default .DEFAULT_DEFAULT_PATTERN}}'

# this one-line is pretty scary
cmd: task -a --json | jq -c '[ (.tasks[] | select( .name | test({{.PATTERN | quote}})) | select( .name | index({{.TASK | quote}}) | not) ) | .name] | .[]' | xargs -n 1 -L1 -I'{}' sh -c 'task {} || exit 255'
cmds:
- cmd: task -a --json | jq -c '[ (.tasks[] | select( .name | test({{.PATTERN | quote}})) | select( .name | index({{.TASK | quote}}) | not) ) | .name] | .[]' | xargs -n 1 -L1 -I'{}' sh -c 'task {} {{.CLI_ARGS}} || exit 255'
10 changes: 7 additions & 3 deletions .tk/git/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ tasks:
Examples:
- `task {{.TASK}}` generates the `.gitignore` file
cmds:
- echo "${GITIGNORE_CONTENT}" > '{{.ROOT_DIR}}/.gitignore'
- echo "${GITIGNORE_CONTENT}" > '{{.GITIGNORE_FILE}}'
vars:
GITIGNORE_EXTRA: '{{.GITIGNORE_EXTRA | default .DEFAULT_GIT_GITIGNORE_EXTRA}}'
GITIGNORE_FILE: '{{.ROOT_DIR}}/.gitignore'
status:
- test -f '{{ .GITIGNORE_FILE }}'
env:
GITIGNORE_CONTENT: |
# THIS FILE HAS BEEN GENERATED BY THE COMMAND `{{.TASK}}`; DO NOT EDIT;
Expand Down Expand Up @@ -399,7 +402,6 @@ tasks:
install:
desc: Install git pre-commit hook
run: once
summary: |
Install GIT pre-commit hook.
This hook will stash everything that is untracked and execute all task
Expand All @@ -418,10 +420,12 @@ tasks:
cmds:
- if [ -f {{.PRE_COMMIT_FILE}} ]; then mv {{.PRE_COMMIT_FILE}} {{.PRE_COMMIT_FILE}}.old;fi;
- echo "${PRE_COMMIT}" > {{.PRE_COMMIT_FILE}}
- chmod u+x {{.ROOT_DIR}}/.git/hooks/pre-commit
- chmod u+x {{.PRE_COMMIT_FILE}}
vars:
PATTERN: '{{.PATTERN | default .DEFAULT_GIT_PRE_COMMIT_PATTERN}}'
PRE_COMMIT_FILE: '{{.ROOT_DIR}}/.git/hooks/pre-commit'
status:
- test -f '{{ .PRE_COMMIT_FILE }}'
env:
PRE_COMMIT: |
#!/bin/bash
Expand Down
14 changes: 9 additions & 5 deletions .tk/golangci/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

vars:
DEFAULT_GOLANGCI_BIN_NAME: '{{ .BIN_NAME | default "golangci-lint" }}'
DEFAULT_GOLANGCI_GITHUBACTION_FILENAME: '{{.ROOT_DIR}}/.github/workflows/golangci.yml'
DEFAULT_GOLANGCI_GITHUBACTION_FILENAME: 'golangci.yml'
DEFAULT_GOLANGCI_CONFIG_FILENAME: '.golangci.yaml'
DEFAULT_GOLANGCI_SOURCES: '{{ .DEFAULT_GO_SOURCES | default "./..." }}'
DEFAULT_GOLANGCI_ARGS: ''
Expand Down Expand Up @@ -90,10 +90,12 @@ tasks:
The generated file should be excluded from commits.
vars:
GITHUBACTION_FILENAME: '{{.GITHUBACTION_FILENAME | default .DEFAULT_GOLANGCI_GITHUBACTION_FILENAME}}'
GITHUBACTION_FILENAME: '{{.ROOT_DIR}}/.github/workflows/{{.GITHUBACTION_FILENAME | default .DEFAULT_GOLANGCI_GITHUBACTION_FILENAME}}'
cmds:
- mkdir -p $(dirname "{{.GITHUBACTION_FILENAME}}")
- echo "${GITHUBACTION_CONTENT}" > "{{.GITHUBACTION_FILENAME}}"
status:
- test -f '{{ .GITHUBACTION_FILENAME }}'
env:
GITHUBACTION_CONTENT: |
# THIS FILE HAS BEEN GENERATED BY THE COMMAND `{{.TASK}}`; DO NOT EDIT;
Expand Down Expand Up @@ -157,9 +159,11 @@ tasks:
The generated file should be excluded from commits.
vars:
CONFIG_FILENAME: '{{.CONFIG_FILENAME | default .DEFAULT_GOLANGCI_CONFIG_FILENAME}}'
CONFIG_FILENAME: '{{.ROOT_DIR}}/{{.CONFIG_FILENAME | default .DEFAULT_GOLANGCI_CONFIG_FILENAME}}'
cmds:
- echo "${CONFIG_CONTENT}" > "{{.ROOT_DIR}}/{{.CONFIG_FILENAME}}"
- echo "${CONFIG_CONTENT}" > "{{.CONFIG_FILENAME}}"
status:
- test -f '{{ .CONFIG_FILENAME }}'
env:
CONFIG_CONTENT: |
# THIS FILE HAS BEEN GENERATED BY THE COMMAND `{{.TASK}}`; DO NOT EDIT;
Expand Down Expand Up @@ -244,7 +248,7 @@ tasks:
gocognit:
# Minimal code complexity to report.
# Default: 30 (but we recommend 10-20)
min-complexity: 30
min-complexity: 40
gocritic:
# Settings passed to gocritic.
Expand Down
40 changes: 28 additions & 12 deletions .tk/goreleaser/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ vars:
tasks:
default:
cmds:
- task: docker
- task: boilerplate
- task: ci
- task: install-script
Expand All @@ -36,6 +37,8 @@ tasks:
BIN_NAME: '{{ .BIN_NAME | default .DEFAULT_GORELEASER_BIN_NAME }}'
cmds:
- echo "${INSTALL_SCRIPT_CONTENT}" > "{{.INSTALL_SCRIPT_FILENAME}}"
status:
- test -f '{{ .INSTALL_SCRIPT_FILENAME }}'
env:
INSTALL_SCRIPT_CONTENT: |
#!/bin/sh
Expand Down Expand Up @@ -556,6 +559,28 @@ tasks:
args: release --clean
env:
GITHUB_TOKEN: ${{ "{{ secrets.GITHUB_TOKEN }}" }}
docker:
desc: Generate dockerfile for GoReleaser
summary: |
Generate Docker for GoReleaser.
vars:
NAME: '{{ .NAME | default .DEFAULT_GORELEASER_NAME }}'
DOCKER_CMD: '{{ .DOCKER_CMD | default .DEFAULT_GORELEASER_DOCKER_CMD }}'
cmds:
- echo "${DOCKERFILE_CONTENT}" > "Dockerfile"

status:
- test -f 'Dockerfile'
env:
DOCKERFILE_CONTENT: |
FROM scratch
COPY {{ .NAME }} /usr/bin/{{ .NAME }}
ENTRYPOINT [ "/usr/bin/{{ .NAME }}" ]
CMD {{ .DOCKER_CMD }}
boilerplate:
desc: Generate goreleaser configuration
summary: |
Expand All @@ -564,9 +589,6 @@ tasks:
Any modification can be overwriting in the future.
The generated file should be excluded from commits.
status:
- 'test -f "{{.CONFIG_FILENAME}}"'
vars:
NAME: '{{ .NAME | default .DEFAULT_GORELEASER_NAME }}'
DESCRIPTION: '{{ .DESCRIPTION | default .DEFAULT_GORELEASER_DESCRIPTION }}'
Expand All @@ -578,16 +600,10 @@ tasks:
SOURCE: '{{ .SOURCE | default .DEFAULT_GORELEASER_SOURCE }}'
cmds:
- echo "${CONFIG_CONTENT}" > "{{.CONFIG_FILENAME}}"
- echo "${DOCKERFILE_CONTENT}" > "Dockerfile"
env:
DOCKERFILE_CONTENT: |
FROM scratch
COPY {{ .NAME }} /usr/bin/{{ .NAME }}
ENTRYPOINT [ "/usr/bin/{{ .NAME }}" ]

CMD {{ .DOCKER_CMD }}
status:
- test -f '{{ .CONFIG_FILENAME }}'
env:
CONFIG_CONTENT: |
# THIS FILE HAS BEEN GENERATED BY THE COMMAND `{{.TASK}}`; DO NOT EDIT;
# See also: .github/workflows/release.yml
Expand Down
4 changes: 4 additions & 0 deletions .tk/license/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,9 @@ tasks:
FILENAME: '{{.LICENSE_FILENAME | default .DEFAULT_LICENSE_FILENAME}}'
YEAR: '{{.LICENSE_YEAR | default .DEFAULT_LICENSE_YEAR}}'
PROJECT: '{{.LICENSE_PROJECT | default .DEFAULT_LICENSE_PROJECT}}'

status:
- test -f '{{ .FILENAME }}'

cmds:
- aqua exec license -n "{{.NAME}}" -y "{{.YEAR}}" {{if .PROJECT }}-p "{{.PROJECT}}"{{end}} -o {{.FILENAME}} {{.TYPE}}
6 changes: 3 additions & 3 deletions .tk/markdownlint/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ tasks:
cmd:
task: lint



pre-commit:
cmd:
task: default
Expand Down Expand Up @@ -70,9 +68,11 @@ tasks:
The generated file should be excluded from commits.
status:
- 'test -f "{{.CONFIG_FILENAME}}"'
- test -f "{{.CONFIG_FILENAME}}"

vars:
CONFIG_FILENAME: '{{.CONFIG_FILENAME | default .DEFAULT_MARKDOWNLINT_CONFIG_FILENAME}}'

cmds:
- echo "${CONFIG_CONTENT}" > "{{.CONFIG_FILENAME}}"
env:
Expand Down
3 changes: 3 additions & 0 deletions .tk/trufflehog/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ tasks:
cmds:
- mkdir -p $(dirname "{{.GITHUBACTION_FILENAME}}")
- echo "${GITHUBACTION_CONTENT}" > "{{.GITHUBACTION_FILENAME}}"

status:
- test -f "{{.GITHUBACTION_FILENAME}}"
env:
GITHUBACTION_CONTENT: |
# THIS FILE HAS BEEN GENERATED BY THE COMMAND `{{.TASK}}`; DO NOT EDIT;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Test Status](https://github.com/merlindorin/tk/actions/workflows/goreleaser.yml/badge.svg)](https://github.com/merlindorin/tk/actions/workflows/goreleaser.yml)
[![Test Status](https://github.com/merlindorin/tk/actions/workflows/trufflehog.yml/badge.svg)](https://github.com/merlindorin/tk/actions/workflows/trufflehog.yml)

Welcome to `tk`, a command-line interface (CLI) designed to streamline the installation of tools for your projects.
> Welcome to `tk`, a command-line interface (CLI) designed to streamline the installation of tools for your projects.
Managed by [merlindorin](https://github.com/merlindorin), this project offers simplicity and efficiency by using
powerpacks that bundle necessary configurations for Taskfile and Aquafiles.

Expand Down
19 changes: 7 additions & 12 deletions cmd/tk/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ package commands

import (
"fmt"
"time"

"github.com/merlindorin/go-shared/pkg/cmd"
ps "github.com/merlindorin/tk/pkg/powerpacks"

"github.com/merlindorin/tk/powerpacks"

"github.com/vanyda-official/go-shared/pkg/cmd"
)

type InitCmd struct {
Name string `arg:"" optional:"" help:"project name"`
Description string `help:"project description"`
Target string `help:"target where to init tk" default:"."`

Exclude []string `help:"exclude powerpacks" optional:""`

DisableEnvrc bool `default:"false" help:"disable envrc"`
DisableTaskfiles bool `default:"false" help:"disable taskfiles"`
DisableAqua bool `default:"false" help:"disable aqua"`
DisableReadme bool

Force bool `default:"false" help:"overwrite existing files if already exist"`

Timeout time.Duration `default:"5s" help:"timeout for discovering (ns, ms, s & m)"`
}

func (i *InitCmd) Run(_ *cmd.Commons) error {
p, err := powerpacks.BuildPowerpackManager()
manager, err := powerpacks.BuildPowerpackManager()
if err != nil {
return fmt.Errorf("failed to create powerpacks: %w", err)
}
Expand All @@ -35,9 +29,10 @@ func (i *InitCmd) Run(_ *cmd.Commons) error {
IgnoreAqua: i.DisableAqua,
IgnoreTaskfile: i.DisableTaskfiles,
IgnoreReadme: i.DisableReadme,
Excludes: i.Exclude,
}

if er := p.Write(".", opts); er != nil {
if er := manager.Write(".", opts); er != nil {
return fmt.Errorf("failed to write powerpacks: %w", er)
}

Expand Down
1 change: 0 additions & 1 deletion cmd/tk/commands/templates/Taskfile.yaml.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ includes:

dotenv:
- .env
- PROJECT
- .env.default
43 changes: 43 additions & 0 deletions cmd/tk/commands/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package commands

import (
"bytes"
"fmt"
"os"
"path/filepath"

ps "github.com/merlindorin/tk/pkg/powerpacks"
"github.com/merlindorin/tk/powerpacks"
"gopkg.in/yaml.v3"

"github.com/merlindorin/go-shared/pkg/cmd"
)

type UpdateCmd struct {
Target string `help:"target where to init tk" default:"."`
}

func (i *UpdateCmd) Run(_ *cmd.Commons) error {
manager, err := powerpacks.BuildPowerpackManager()
if err != nil {
return fmt.Errorf("failed to create powerpacks: %w", err)
}

f, err := os.ReadFile(filepath.Join(i.Target, ".tk.yaml"))
if err != nil {
return fmt.Errorf("failed to read .tk.yaml: %w", err)
}

opts := ps.WriteOption{}

err = yaml.NewDecoder(bytes.NewBuffer(f)).Decode(&opts)
if err != nil {
return fmt.Errorf("failed to parse .tk.yaml: %w", err)
}

if er := manager.Write(".", opts); er != nil {
return fmt.Errorf("failed to updates powerpacks: %w", er)
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/tk/global/tk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global

type Taskfiles struct {
type TK struct {
}
Loading

0 comments on commit a691489

Please sign in to comment.