Skip to content

Commit

Permalink
feat: Make git optional (#150)
Browse files Browse the repository at this point in the history
- Fix verbosity level 3 TRACE throwing
- Fixup some debugf/infof calls with missing newline
- Making git an optional dependency

---------

Closes #122
Closes #139
Signed-off-by: AlexNg <contact@ngjx.org>
  • Loading branch information
caffeine-addictt authored Oct 3, 2024
2 parents 6fae352 + bac7432 commit a00d459
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 66 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ jobs:
- name: Set up Cosign
uses: sigstore/cosign-installer@v3

# - name: setup-snapcraft
# run: |
# sudo apt-get update
# sudo apt-get -yq --no-install-suggests --no-install-recommends install snapcraft
- name: setup-snapcraft
run: |
sudo apt-get update
sudo apt-get -yq --no-install-suggests --no-install-recommends install snapcraft
- name: Set up Chocolately
run: |
Expand Down
20 changes: 10 additions & 10 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ brews:
# source_repo: "https://push.chocolatey.org/"
# skip_publish: false

# snapcrafts:
# - name_template: "{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
# summary: Let's make starting new projects feel like a breeze again.
# description: |
# A simple template repository generator.
# Lets make starting new projects feel like a breeze again!
# license: AGPL-3.0
# grade: stable
# confinement: classic
# publish: true
snapcrafts:
- name_template: "{{ .ProjectName }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
summary: Let's make starting new projects feel like a breeze again.
description: |
A simple template repository generator.
Lets make starting new projects feel like a breeze again!
license: AGPL-3.0
grade: stable
confinement: strict
publish: true

scoops:
- repository:
Expand Down
14 changes: 9 additions & 5 deletions cmd/commands/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package commands

import (
"os/exec"

e "github.com/caffeine-addictt/waku/internal/errors"
"github.com/caffeine-addictt/waku/internal/git"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/spf13/cobra"
)

Expand All @@ -14,8 +13,13 @@ var HealthcheckCmd = &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := exec.Command("git", "--version").Run(); err != nil {
return e.NewWakuErrorf("%v", err)
ok, err := git.HasGit()
if err != nil {
return err
}

if !ok {
log.Warnln("Git not found in $PATH, Waku will fallback to go-git. Authentication may not work as expected.")
}

return nil
Expand Down
12 changes: 4 additions & 8 deletions cmd/commands/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"

"github.com/caffeine-addictt/waku/cmd/options"
"github.com/caffeine-addictt/waku/internal/git"
"github.com/caffeine-addictt/waku/internal/license"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/caffeine-addictt/waku/internal/template"
Expand Down Expand Up @@ -231,7 +231,7 @@ var NewCmd = &cobra.Command{
finalTmpl["Name"] = name
finalTmpl["License"] = license.Name
finalTmpl["Spdx"] = license.Spdx
log.Debugf("final template: %v", finalTmpl)
log.Debugf("final template: %v\n", finalTmpl)

if err := WriteFiles(rootDir, projectRootDir, ignoredPaths.ToSlice(), licenseText, finalTmpl, licenseTmpl); err != nil {
fmt.Printf("failed to write files: %s\n", err)
Expand All @@ -242,11 +242,7 @@ var NewCmd = &cobra.Command{
if options.NewOpts.NoGit {
log.Infoln("skipping git initialization")
} else {
cmd.Println("initializing git...")
initGit := exec.Command("git", "init")
initGit.Dir = projectRootDir

if err := initGit.Run(); err != nil {
if err := git.Init(projectRootDir); err != nil {
fmt.Printf("failed to initialize git: %s\n", err)
exitCode = 1
return
Expand Down Expand Up @@ -309,7 +305,7 @@ func WriteFiles(tmpRoot, projectRoot string, paths []string, licenseText string,
return
}
defer newFile.Close()
log.Debugf("opened file for writing: %s", newPath)
log.Debugf("opened file for writing: %s\n", newPath)

reader := bufio.NewScanner(tmpFile)
writer := bufio.NewWriter(newFile)
Expand Down
27 changes: 9 additions & 18 deletions cmd/options/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package options
import (
"errors"
"os"
"os/exec"

"github.com/caffeine-addictt/waku/internal/git"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/caffeine-addictt/waku/internal/types"
"github.com/caffeine-addictt/waku/internal/utils"
Expand Down Expand Up @@ -83,32 +83,23 @@ func (o *NewOptions) Validate() error {

// To clone the repository
func (o *NewOptions) CloneRepo() (string, error) {
log.Debugln("Creating tmp dir")
log.Debugln("creating tmp dir")

tmpDirPath, err := os.MkdirTemp("", "template-*")
if err != nil {
return "", err
}

log.Infoln("Create tmp dir at", tmpDirPath)
log.Infoln("create tmp dir at", tmpDirPath)

args := []string{"clone", "--depth", "1"}
if o.Branch.Value() != "" {
args = append(args, "--branch", utils.EscapeTermString(o.Branch.Value()))
opts := git.CloneOptions{
Depth: 1,
Branch: o.Branch.Value(),
Url: utils.EscapeTermString(o.Repo.Value()),
ClonePath: utils.EscapeTermString(tmpDirPath),
}
args = append(args, utils.EscapeTermString(o.Repo.Value()), utils.EscapeTermString(tmpDirPath))

log.Debugln("git args:", args, len(args))

c := exec.Command("git", args...)
c.Stdin = os.Stdin

if log.GetLevel() <= log.INFO {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
}

if err := c.Run(); err != nil {
if err := git.Clone(opts); err != nil {
if errCleanup := os.RemoveAll(tmpDirPath); errCleanup != nil {
return "", errors.Join(errCleanup, err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/caffeine-addictt/waku/cmd/commands"
"github.com/caffeine-addictt/waku/cmd/options"
"github.com/caffeine-addictt/waku/internal/utils"
"github.com/caffeine-addictt/waku/internal/errors"
"github.com/caffeine-addictt/waku/internal/log"
"github.com/caffeine-addictt/waku/internal/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ var RootCmd = &cobra.Command{
func init() {
RootCmd.PersistentFlags().BoolVarP(&options.GlobalOpts.Quiet, "quiet", "q", false, "quiet mode")
RootCmd.PersistentFlags().BoolVarP(&options.GlobalOpts.Accessible, "accessible", "A", false, "accessible mode")
RootCmd.PersistentFlags().CountVarP(&options.GlobalOpts.Verbosity, "verbose", "v", "verobisty level (1: info, 2: debug, 3: trace)")
RootCmd.PersistentFlags().CountVarP(&options.GlobalOpts.Verbosity, "verbose", "v", "verobisty level (v: info, vv: debug, vvv: trace)")

RootCmd.MarkFlagsMutuallyExclusive("quiet", "verbose")

Expand Down
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/charmbracelet/huh v0.6.0
github.com/go-git/go-git/v5 v5.12.0
github.com/goccy/go-json v0.10.3
github.com/muesli/mango-cobra v1.2.0
github.com/muesli/roff v0.1.0
Expand All @@ -12,6 +13,9 @@ require (
)

require (
dario.cat/mergo v1.0.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
Expand All @@ -21,10 +25,18 @@ require (
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240829200707-9a7bd603a0d7 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/cloudflare/circl v1.4.0 // indirect
github.com/cyphar/filepath-securejoin v0.3.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand All @@ -35,11 +47,18 @@ require (
github.com/muesli/mango v0.2.0 // indirect
github.com/muesli/mango-pflag v0.1.0 // indirect
github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit a00d459

Please sign in to comment.