Skip to content

Commit

Permalink
Merge pull request #17 from kazhuravlev/ka/support-local-installation
Browse files Browse the repository at this point in the history
Ka/support local installation
  • Loading branch information
kazhuravlev authored Jul 20, 2024
2 parents 1e9aed0 + 4814079 commit e2d5465
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cmd/gt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
repomanager "github.com/kazhuravlev/git-tools/internal/repo-manager"
"github.com/kazhuravlev/git-tools/internal/which"
"github.com/urfave/cli/v3"
"io"
"os"
Expand Down Expand Up @@ -310,15 +311,25 @@ func cmdHooksInstallAll(ctx context.Context, c *cli.Command, m *repomanager.Mana
content.WriteString(fmt.Sprintf("# hook file (%s) is backed up into (%s) at (%s)\n", hookFilename, hookFilenameBackup, time.Now().Format(time.DateTime)))
}

gtExecutable := "gt"
if _, err := which.Executable(os.DirFS("/"), "gt"); err != nil {
p, err := os.Executable()
if err != nil {
return fmt.Errorf("cannot get executable: %w", err)
}

gtExecutable = p
}

content.WriteString(fmt.Sprintf(`
if command -v gt >/dev/null 2>&1; then
gt hooks exec %s $1
%s hooks exec %s $1
else
echo "Can't find git-tools (gt binary)"
echo "Check the docs https://github.com/kazhuravlev/git-tools"
exit 1
fi
`, hooks[i]))
`, gtExecutable, hooks[i]))
content.WriteString("\n")

target, err := os.Create(hookFilename)
Expand Down
58 changes: 58 additions & 0 deletions internal/which/which.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package which

import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
)

var ErrNotExecutable = errors.New("not executable")

// Executable returns a full path to first executable program in $PATH.
func Executable(fSys fs.FS, program string) (string, error) {
for _, path := range getPaths() {
filename := filepath.Join(path, program)
executable, err := isExecutable(fSys, filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return "", fmt.Errorf("check executable for file (%s): %w", filename, err)
}

if executable {
return filename, nil
}
}

return "", ErrNotExecutable
}

func isExecAny(mode os.FileMode) bool {
return mode&0x0111 != 0
}

func isExecutable(fSys fs.FS, path string) (bool, error) {
info, err := fs.Stat(fSys, path[1:])
if err != nil {
return false, fmt.Errorf("get file stat: %w", err)
}

if info.IsDir() {
return false, nil
}

if isExecAny(info.Mode()) {
return true, nil
}

return runtime.GOOS == "windows", nil
}

func getPaths() []string {
return strings.Split(os.Getenv("PATH"), string(os.PathListSeparator))
}

0 comments on commit e2d5465

Please sign in to comment.