(ctx *snap.Context, commandLabel string, openAfter bool)
| 3051 | } |
| 3052 | |
| 3053 | func privateForkRepoFlow(ctx *snap.Context, commandLabel string, openAfter bool) error { |
| 3054 | if ctx.NArgs() > 1 { |
| 3055 | fmt.Fprintf(ctx.Stderr(), "Usage: %s %s [github-repo-url]\n", commandName, commandLabel) |
| 3056 | return fmt.Errorf("expected at most 1 argument, got %d", ctx.NArgs()) |
| 3057 | } |
| 3058 | |
| 3059 | var input string |
| 3060 | if ctx.NArgs() == 1 { |
| 3061 | input = strings.TrimSpace(ctx.Arg(0)) |
| 3062 | } else { |
| 3063 | var err error |
| 3064 | input, err = promptLine(ctx, "GitHub repository URL: ") |
| 3065 | if err != nil { |
| 3066 | return reportError(ctx, fmt.Errorf("read repository URL: %w", err)) |
| 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | if input == "" { |
| 3071 | fmt.Fprintf(ctx.Stderr(), "Usage: %s %s [github-repo-url]\n", commandName, commandLabel) |
| 3072 | return fmt.Errorf("github repository url cannot be empty") |
| 3073 | } |
| 3074 | |
| 3075 | owner, repo, cloneURL, err := parseGitHubCloneInfo(input) |
| 3076 | if err != nil { |
| 3077 | return reportError(ctx, fmt.Errorf("parse GitHub repository reference: %w", err)) |
| 3078 | } |
| 3079 | |
| 3080 | login, err := currentGitHubLogin() |
| 3081 | if err != nil { |
| 3082 | return reportError(ctx, fmt.Errorf("determine GitHub login: %w", err)) |
| 3083 | } |
| 3084 | |
| 3085 | homeDir, err := os.UserHomeDir() |
| 3086 | if err != nil { |
| 3087 | return reportError(ctx, fmt.Errorf("determine home directory: %w", err)) |
| 3088 | } |
| 3089 | |
| 3090 | targetDir := filepath.Join(homeDir, "fork-i", owner, repo) |
| 3091 | parentDir := filepath.Dir(targetDir) |
| 3092 | if err := os.MkdirAll(parentDir, 0o755); err != nil { |
| 3093 | return reportError(ctx, fmt.Errorf("create directory %s: %w", parentDir, err)) |
| 3094 | } |
| 3095 | |
| 3096 | if info, err := os.Stat(targetDir); err == nil { |
| 3097 | if info.IsDir() { |
| 3098 | if openAfter { |
| 3099 | fmt.Fprintf(ctx.Stdout(), "ℹ️ Destination %s already exists; skipping clone.\n", targetDir) |
| 3100 | if err := openInZed(ctx, targetDir); err != nil { |
| 3101 | return reportError(ctx, fmt.Errorf("open repository in Zed: %w", err)) |
| 3102 | } |
| 3103 | fmt.Fprintf(ctx.Stdout(), "✔️ Opened %s in Zed\n", targetDir) |
| 3104 | return nil |
| 3105 | } |
| 3106 | return reportError(ctx, fmt.Errorf("destination %s already exists", targetDir)) |
| 3107 | } |
| 3108 | return reportError(ctx, fmt.Errorf("destination %s exists and is not a directory", targetDir)) |
| 3109 | } else if !errors.Is(err, os.ErrNotExist) { |
| 3110 | return reportError(ctx, fmt.Errorf("check %s: %w", targetDir, err)) |
no test coverage detected