feat: auto detect hostname
This commit is contained in:
parent
2c27823e18
commit
c0baf4fa3b
13 changed files with 300 additions and 125 deletions
33
cmd/root.go
33
cmd/root.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue