Skip to content

Commit

Permalink
feat: add template subcommand (#24)
Browse files Browse the repository at this point in the history
- Created `template` subcommand to manage reusable Git hook templates.
- Implemented `create`, `copy`, `edit`, and `remove` subcommands for template management.
- Added the ability to configure Git hook templates for multiple use cases.

feat: add config subcommand

- Created `config` subcommand to manage HkUp configuration settings.
- Subcommands include `get` and `set` for managing configuration settings.
- NOTE: This feature is currently hidden and will be available once configuration settings are finalized.

chore: improve install script

- Improved script to handle both installation and update operations when the version is lower than the release version.

chore: update release-please GitHub action

- Moved logic from `scripts/build` to `release-please` action.
- Removed `scripts/build` to simplify release process.

chore: use defaults for Go dependency checking

- Removed settings for day, time, and timezone from Go dependency checking.
- Dependabot now uses the default weekly time settings for Go dependencies.

chore: fix pre-commit git hook

- Removed adding all files after running the Go formatter, fixing the issue where unwanted files were being added to commits.

refactor(cmd): simplify flag handling & init logic

- Renamed flags for consistency: `Lang` to `LangFlg`, `GitDir` to `GitDirFlg`, `WorkTree` to `WorkTreeFlg`.
- Consolidated command registration into `root.go`.
- Simplified the `init()` function and centralized logic.

refactor(logic): update flags and simplify hook commands

- Simplified Add, Remove, and Init logic by centralizing directory checks.
- Renamed flags: `Lang` to `LangFlg`, `GitDir` to `GitDirFlg`, `WorkTree` to `WorkTreeFlg`.
- Improved readability and error handling in file operations.

refactor(git): improve comments and simplify hook logic

- Updated comments for clarity and consistency in the Git package.
- Simplified `GetHook` and `GetLang` functions to improve readability.
- Updated the `supportedLangs` map to explicitly include `sh` and `bash`.

feat(util): add file ops, prompts, and config functions

- Added utility functions for terminal prompts: `YesNoPrompt` and `UserInputPrompt`.
- Expanded file handling functions: `CreateDirectory` and `CopyFile`.
- Introduced functions to handle HkUp configuration paths: `GetConfigDirPath`, `GetConfigFilePath`, `GetTemplateDirPath`.
- Added functions for TOML file manipulation: `GetTOMLValue`, `SetTOMLValue`.
- Implemented `GetEditor` function to determine the default editor based on configuration, git, or environment variables.
  • Loading branch information
iton0 authored Nov 20, 2024
1 parent 1c7753d commit 28c507e
Show file tree
Hide file tree
Showing 39 changed files with 1,319 additions and 237 deletions.
3 changes: 0 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ updates:
directory: "/"
schedule:
interval: "weekly"
day: "friday"
time: "12:00"
timezone: "America/New_York"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
34 changes: 25 additions & 9 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

# Step 2: Run your build script
- name: Run build script
run: |
rm -rf ./bin
chmod +x ./scripts/build
./scripts/build
# Step 2: Set up Go environment
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

# Step 3: Use release-please to create a release
- name: Create release
Expand All @@ -31,12 +30,29 @@ jobs:
with:
release-type: go

# Step 4: Upload Release Artifacts if a release was created
# Step 4: Update the version in the build and perform multi-platform builds
- name: Build for multiple platforms
if: ${{ steps.release.outputs.release_created }}
run: |
VERSION="${{ steps.release.outputs.version }}"
echo "Updating version to ${VERSION}"
mkdir -p bin
# Build for Linux
GOOS=linux GOARCH=amd64 go build -o bin/hkup-linux -ldflags="-s -w -X cmd.version=${VERSION}" .
# Build for Darwin/macOS
GOOS=darwin GOARCH=amd64 go build -o bin/hkup-darwin -ldflags="-s -w -X cmd.version=${VERSION}" .
# Build for Windows (optional, uncomment if needed)
# GOOS=windows GOARCH=amd64 go build -o bin/hkup.exe -ldflags="-s -w -X cmd.version=${VERSION}" .
# Step 5: Upload Release Artifacts if a release was created
- name: Upload Release Artifacts
if: ${{ steps.release.outputs.release_created }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload ${{ steps.release.outputs.tag_name }} \
./bin/*
bin/*
3 changes: 0 additions & 3 deletions .hkup/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
if git diff --cached --name-only | grep -q '\.go$'; then
echo "Formatting Go files..."
gofmt -w .
echo ""

# Add formatted files to the staging area
git add .
else
echo "No Go files changed. Skipping formatting."
echo ""
Expand Down
84 changes: 52 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# HkUp
Your CLI tool with benefits built by [iton0](https://github.com/iton0) in [Go](https://go.dev/)!
> Your CLI tool with benefits built by [iton0](https://github.com/iton0) in [Go](https://go.dev/)!
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/iton0/hkup-cli)](https://github.com/iton0/hkup-cli/releases/latest)
[![godoc](https://godoc.org/github.com/iton0/hkup-cli?status.svg)](http://godoc.org/github.com/iton0/hkup-cli)
Expand All @@ -11,9 +11,16 @@ Your CLI tool with benefits built by [iton0](https://github.com/iton0) in [Go](h
## Introduction
Git hooks automate and implement processes in your workflow, increasing code quality and consistency.

However, many developers avoid git hooks due to a lack of awareness and the perceived complexity of setup, discouraging them from using this feature.
Common use cases include:
- Commit Message Validation
- Environment Configuration
- Formatting
- Linting
- Testing

**HkUp** simplifies the management of git hooks, allowing you to focus on the logic and usage of your hooks instead.
However, many developers avoid Git hooks due to a lack of awareness and the perceived complexity of setup, discouraging them from using this feature.

**HkUp** simplifies the management of Git hooks, allowing you to focus on the logic and usage of your hooks instead.

## Installation
External Dependencies:
Expand All @@ -27,10 +34,9 @@ Run the script below (supports Linux and macOS):
curl -sSL https://raw.githubusercontent.com/iton0/hkup-cli/main/scripts/install | sh
```
> [!Tip]
> To update HkUp, rerun the above script.
> It will replace the current version.
> To update HkUp, simply rerun the script above. It will automatically replace your current version with the latest release.
#### Uninstalling hkup
### Uninstalling HkUp

```sh
# Locates and deletes the HkUp binary
Expand All @@ -40,49 +46,63 @@ sh -c 'rm "$(command -v 'hkup')"'
</details>

## Usage Quickstart
This section provides basic information about core usage. For detailed options run `hkup --help`.
This section provides basic information about core usage. For detailed usage information run `hkup --help`.

#### Initializing hkup
### Initializing hkup
Run the following command in your git repository to initialize HkUp:
```sh
hkup init
```

This command creates a **.hkup** folder and sets the local **core.hooksPath** variable. If the folder already exists, it will simply update the path variable. The path is relative, ensuring that moving your repository won’t affect hook sourcing.
This creates a **.hkup** directory and sets the local **core.hooksPath** variable. If the directory already exists, it will simply update the path variable. The path is relative, ensuring that moving your repository won’t affect hook sourcing.

#### Adding & Removing hooks
### Adding & Removing hooks
Add or remove hooks easily with:
```sh
hkup add <hook-name>

hkup remove <hook-name>
```

#### Info & Docs
There are two commands that will help you with both HkUp and git hooks:

**`hkup list {hook|lang}`**
Outputs list of either available hooks or supported languages.
### Templates
A **template** is a pre-configured, reusable Git hook that simplifies and automates the process of setting up hooks in a Git repository. With **HkUp**, you can create, copy, edit, or remove templates, allowing for consistent and easy application of hooks without needing to rewrite scripts each time.

**`hkup doc <hook-name>`**
Opens your browser with Git documentation for the specified git hook, helping you understand its usage.
The templates are stored in the HkUp config templates directory that can either be found at **$XDG_CONFIG_HOME/hkup/templates** or **$HOME/.config/hkup/templates** depending on your system.

## Future TODOs
- [ ] make an update subcommand
- [ ] store custom git hooks as templates for future use (via add template subcmd)
- Allow users to create, store, and share templates for common hooks. Users can fetch these templates over the network.
- [ ] branch-specific hooks
- [ ] logo maybe?
#### Naming Convention
Template files follow the naming convention:
`<template-name>#<hook-name>`
Where:
- `<template-name>` is the name of the template.
- `<hook-name>` is the specific Git hook (e.g., pre-commit, post-merge).

## Contributing
HkUp welcomes contributions to enhance this CLI application! Before submitting a pull request (PR) for a new feature, please follow these steps:
**Create a template**:
```sh
hkup template create
# OR
hkup template create <hook-name>
```

1. **Create an Issue**:
If you have an idea for a new feature, please create a new issue in the repository using the **feature_request** template. Provide a clear description of the feature and its potential benefits. Please note that issues submitted without using the template may be closed without warning.
**Copy a template** into current working directory:
```sh
hkup template copy <template-name>
```

2. **Wait for Approval**:
Once you submit your issue, I’ll review it and provide feedback. If I approve the feature request, I will let you know that you're free to proceed with your PR.
**Edit a template**:
```sh
hkup template edit <template-name>
```
>[!CAUTION]
> Editing a template will not update its copies.
3. **Submit Your PR**:
After receiving approval, you can create your PR. Be sure to reference the issue in your PR description.
**Remove a template**:
```sh
hkup template remove <template-name>
```

Please note that PRs submitted without prior approval through an issue may be closed without merging. This process helps us manage feature requests effectively and ensures that contributions align with the project’s goals.
## Roadmap to v1.0.0
1. windows support
2. wrapper for git init & clone and gh repo create & clone
3. HkUp logo (may or may not keep this one)
4. better test coverage
5. Allow users to create, store, and share templates. Users can fetch these templates over internet (may need to make another repo for this).
3 changes: 1 addition & 2 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ var (
)

func init() {
addCmd.Flags().StringVar(&logic.Lang, "lang", "", "supported languages for git hooks")
rootCmd.AddCommand(addCmd)
addCmd.Flags().StringVar(&logic.LangFlg, "lang", "", "supported languages for git hooks")
}
17 changes: 17 additions & 0 deletions cmd/config/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
"github.com/iton0/hkup-cli/internal/logic/config"
"github.com/spf13/cobra"
)

var (
getCmd = &cobra.Command{
Use: "get <config-setting>",
Short: "Get a HkUp config setting",
Args: cobra.ExactArgs(1),
RunE: config.Get,
}
)

func init() {}
9 changes: 9 additions & 0 deletions cmd/config/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Package template initializes the config subcommand and its subcommands.
This package is utilized in the root command of [github.com/iton0/hkup-cli/cmd]
package.
*/
package config

// NOTE: This file is for documentation purposes and should be kept empty.
19 changes: 19 additions & 0 deletions cmd/config/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"github.com/spf13/cobra"
)

var (
// RootCmd is the config command that will be added to the root HkUp command.
RootCmd = &cobra.Command{
Use: "config",
Short: "HkUp configuration settings",
Hidden: true, // TODO: remove after finalizing configuration settings
}
)

func init() {
RootCmd.AddCommand(getCmd)
RootCmd.AddCommand(setCmd)
}
17 changes: 17 additions & 0 deletions cmd/config/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
"github.com/iton0/hkup-cli/internal/logic/config"
"github.com/spf13/cobra"
)

var (
setCmd = &cobra.Command{
Use: "set <config-setting> <value>",
Short: "Set a HkUp config setting",
Args: cobra.ExactArgs(2),
RunE: config.Set,
}
)

func init() {}
4 changes: 1 addition & 3 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ var (
}
)

func init() {
rootCmd.AddCommand(docCmd)
}
func init() {}
7 changes: 3 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ var (
initCmd = &cobra.Command{
Use: "init",
Short: "Initialize hkup",
Long: "Create an empty hkup folder or reinitialize an existing one",
Long: "Create an empty hkup directory or reinitialize an existing one",
Args: cobra.NoArgs,
RunE: logic.Init,
}
)

func init() {
initCmd.Flags().StringVar(&logic.GitDir, "gitdir", "", "specified path to git directory")
initCmd.Flags().StringVar(&logic.WorkTree, "worktree", "", "specified path to working tree")
initCmd.Flags().StringVar(&logic.GitDirFlg, "gitdir", "", "specified path to git directory")
initCmd.Flags().StringVar(&logic.WorkTreeFlg, "worktree", "", "specified path to working tree")
initCmd.MarkFlagsRequiredTogether("gitdir", "worktree")
rootCmd.AddCommand(initCmd)
}
4 changes: 1 addition & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ var (
}
)

func init() {
rootCmd.AddCommand(listCmd)
}
func init() {}
4 changes: 1 addition & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/*
Package cmd initializes all commands (including root command) for the HkUp CLI.
Additionally, the package holds all tests for commands.
*/
package cmd

// Note: This file should be kept empty.
// NOTE: This file is for documentation purposes and should be kept empty.
4 changes: 1 addition & 3 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ var (
}
)

func init() {
rootCmd.AddCommand(removeCmd)
}
func init() {}
24 changes: 20 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
package cmd

import (
"github.com/iton0/hkup-cli/cmd/config"
"github.com/iton0/hkup-cli/cmd/template"
"github.com/spf13/cobra"
)

var (
// version holds the centralized version of HkUp.
// It is updated to the latest release version at build time of the binaries.
//
// INFO: look at the .github/workflows/release-please.yml to view how version
// is updated.
version = "dev"

rootCmd = &cobra.Command{
Use: "hkup",
Short: "hkup CLI",
Long: `hkup is a management tool for git hooks`,
Args: cobra.MinimumNArgs(1),
Version: "0.2.1",
Version: version,
}
)

func init() {}
func init() {
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(template.RootCmd)
rootCmd.AddCommand(config.RootCmd)
rootCmd.AddCommand(docCmd)
rootCmd.AddCommand(listCmd)
}

// Execute serves as a wrapper for the Cobra API's Execute function,
// allowing it to be called from the main package.
// allowing it to be called from the [github.com/iton0/hkup-cli] package.
func Execute() {
rootCmd.Execute()
}
17 changes: 17 additions & 0 deletions cmd/template/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package template

import (
"github.com/iton0/hkup-cli/internal/logic/template"
"github.com/spf13/cobra"
)

var (
copyCmd = &cobra.Command{
Use: "copy <template-name>",
Short: "Copy a git hook template",
Args: cobra.ExactArgs(1),
RunE: template.Copy,
}
)

func init() {}
Loading

0 comments on commit 28c507e

Please sign in to comment.