feat: first impl of the init function

This commit is contained in:
2026-01-24 23:05:25 +01:00
parent 0a07b85e4e
commit 9866313b60

72
main.go
View File

@@ -2,18 +2,86 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/urfave/cli/v3"
)
func main (){
func exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func Getwtd() (string, error) {
hd, err := os.UserHomeDir()
if err != nil {
return "", err
}
wtd := filepath.Join(hd, ".wt")
if !exists(wtd) {
if err := os.Mkdir(wtd, 0o755); err != nil {
return "", err
}
}
return wtd, nil
}
func Init(ctx context.Context, cmd *cli.Command) error {
fmt.Println("init")
cwd, err := os.Getwd()
if err != nil {
return err
}
wtd, err := Getwtd()
if err != nil {
return err
}
if cmd.Args().Get(0) != "" {
cwd, err = filepath.Abs(cmd.Args().Get(0))
if err != nil {
return err
}
}
if !exists(cwd) {
return fmt.Errorf("%s does not exists", cwd)
}
project := filepath.Join(wtd, filepath.Base(cwd))
if !exists(project) {
if err := os.Mkdir(project, 0o755); err != nil {
return err
}
c := exec.Command("git", "branch", "--show-current")
c.Dir = cwd
out, err := c.Output()
if err != nil {
return err
}
branch := strings.TrimSpace(string(out))
worktree := filepath.Join(project, branch)
if err := os.Rename(cwd, worktree); err != nil {
return err
}
if err := os.Symlink(worktree, cwd); err != nil {
return err
}
}
return nil
}
func main() {
cmd := cli.Command{
Name: "wt",
Commands: []*cli.Command{
{
Name: "init",
Name: "init",
Action: Init,
},
{
Name: "clone",