go-commons
is a versatile collection of utility packages that simplify common tasks related to Cli,Git and GitHub.
For examples check the examples directory.
Easily create CLI commands with Cobra
to perform common tasks.
Easily interact with GitHub using the go-github
library. Key features include:
- Listing user organizations and repositories.
- Checking rate limits.
- Managing authentication via tokens or SSH keys.
Streamline Git-related tasks with go-git
, including:
- Cloning repositories.
- Updating local repositories.
- Supporting both HTTPS and SSH protocols for authentication.
- Directory management: Check for existence and create directories if necessary.
- Environment variable handling: Fetch variables with default fallback options.
To add go-commons
to your project, run:
go get -u github.com/cloudnative-zoo/go-commons
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cloudnative-zoo/go-commons/cli"
)
func main() {
rootCmd := cli.NewCommand(cli.CommandConfig{
Use: "app",
Short: "A sample CLI app",
Long: "This is a sample CLI app demonstrating the usage of the NewCommand function",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("App executed!")
},
Flags: []cli.FlagConfig{
{
Name: "name",
Short: "n",
DefaultValue: "World",
Usage: "Specify the name",
Required: false,
},
{
Name: "verbose",
Short: "v",
DefaultValue: false,
Usage: "Enable verbose mode",
Required: false,
},
},
})
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}
package main
import (
"context"
"log/slog"
"github.com/cloudnative-zoo/go-commons/github"
)
func main() {
ctx := context.Background()
githubService, err := github.New(
github.WithToken("your_github_token"),
)
if err != nil {
slog.With("error", err).Error("Failed to initialize GitHub service")
return
}
orgs, err := githubService.ListOrganizations(ctx)
if err != nil {
slog.With("error", err).Error("Failed to list organizations")
return
}
slog.With("organizations", orgs).Info("Retrieved organizations")
}
package main
import (
"context"
"log/slog"
"github.com/cloudnative-zoo/go-commons/git"
)
func main() {
ctx := context.Background()
gitService, err := git.New(
git.WithSSHKeyPath("/path/to/ssh/key", ""),
git.WithRepoPath("/path/to/local/repo"),
git.WithURL("[email protected]:user/repo.git"),
)
if err != nil {
slog.With("error", err).Error("Failed to initialize Git service")
return
}
err = gitService.CloneOrPull(ctx)
if err != nil {
slog.With("error", err).Error("Failed to clone or pull repository")
return
}
slog.Info("Repository synchronized successfully")
}
- Install Go
- Install pre-commit
A Taskfile.yaml
is included to streamline common development tasks:
- Pre-commit checks
- Updating Go modules
- Formatting code
- Running
go vet
- Running
golangci-lint
The .pre-commit-config.yaml
file configures hooks for:
- Sensitive data detection:
gitleaks
- Formatting tools: End-of-file fixer, trailing whitespace remover, JSON formatter
- Linting tools:
golangci-lint
,gofmt
,goimports
- Spelling and syntax checks
We welcome contributions! Follow these steps:
- Fork the repository.
- Create a new branch:
git checkout -b feature-branch
- Make your changes.
- Commit your changes:
git commit -am 'Add new feature'
- Push to the branch:
git push origin feature-branch
- Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
We are grateful for the following libraries and tools that power this project:
- Cobra for CLI commands
- go-git for Git operations
- go-github for GitHub API interactions
- pre-commit for managing pre-commit hooks
For questions, suggestions, or feedback, please open an issue on the GitHub repository.
- Hassnat Ahmad - github