feat: first impl of the init function
This commit is contained in:
70
main.go
70
main.go
@@ -2,18 +2,86 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/urfave/cli/v3"
|
"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{
|
cmd := cli.Command{
|
||||||
Name: "wt",
|
Name: "wt",
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
{
|
{
|
||||||
Name: "init",
|
Name: "init",
|
||||||
|
Action: Init,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "clone",
|
Name: "clone",
|
||||||
|
|||||||
Reference in New Issue
Block a user