feat: auto detect hostname

This commit is contained in:
Romain Bertrand 2026-01-05 12:47:28 +01:00
parent 2c27823e18
commit c0baf4fa3b
13 changed files with 300 additions and 125 deletions

View file

@ -3,9 +3,11 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"codeberg.org/romaintb/fgj/internal/git"
)
var cfgFile string
@ -53,3 +55,34 @@ func initConfig() {
_ = viper.ReadInConfig()
}
// parseRepo parses the repository string in the format "owner/name".
// If not provided, it attempts to auto-detect from the git repository.
func parseRepo(repo string) (string, string, error) {
// If repo flag is provided, use it
if repo != "" {
parts := strings.Split(repo, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("invalid repository format: %s (expected: owner/name)", repo)
}
return parts[0], parts[1], nil
}
// Try to auto-detect from git
owner, name, err := git.DetectRepo()
if err != nil {
return "", "", fmt.Errorf("repository flag is required (use -R owner/name) or run from a git repository: %w", err)
}
return owner, name, nil
}
// getDetectedHost attempts to auto-detect the Forgejo instance hostname.
// Returns empty string if detection fails, which will fall back to other methods.
func getDetectedHost() string {
host, err := git.DetectHost()
if err != nil {
return ""
}
return host
}