git-ea/cmd/init.go

49 lines
1.0 KiB
Go

package cmd
import (
"context"
"flag"
"fmt"
"os"
"go.jolheiser.com/git-ea/config"
"github.com/peterbourgon/ff/v3/ffcli"
)
func (h *Handler) Init() *ffcli.Command {
fs := flag.NewFlagSet("init", flag.ContinueOnError)
return &ffcli.Command{
Name: "init",
FlagSet: fs,
ShortUsage: "init",
ShortHelp: "init initializes a workspace for Gitea",
Exec: func(ctx context.Context, args []string) error {
if h.Config.Base != "" {
return fmt.Errorf("a git-ea workspace already exists at %q", h.Config.Base)
}
cwd, err := os.Getwd()
if err != nil {
return err
}
if err := run(ctx, ".", "git", "clone", "--bare", "git@github.com:jolheiser/gitea.git", "."); err != nil {
return err
}
if err := run(ctx, ".", "git", "remote", "add", "upstream", "https://github.com/go-gitea/gitea.git"); err != nil {
return err
}
cfg := config.Config{
Base: cwd,
}
if err := os.MkdirAll(cfg.Workspace(), os.ModePerm); err != nil {
return err
}
return config.Save(cfg)
},
}
}