2025-12-08 09:49:07 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var cfgFile string
|
|
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
|
Use: "fgj",
|
|
|
|
|
Short: "Forgejo CLI tool - work seamlessly with Forgejo from the command line",
|
|
|
|
|
Long: `fgj is a command line tool for Forgejo instances (including Codeberg).
|
|
|
|
|
It brings pull requests, issues, and other Forgejo concepts to the terminal.`,
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Execute() error {
|
|
|
|
|
return rootCmd.Execute()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/fgj/config.yaml)")
|
|
|
|
|
rootCmd.PersistentFlags().String("hostname", "", "Forgejo instance hostname")
|
2025-12-08 10:00:50 +01:00
|
|
|
_ = viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname"))
|
2025-12-08 09:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initConfig() {
|
|
|
|
|
if cfgFile != "" {
|
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
|
} else {
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configDir := home + "/.config/fgj"
|
2025-12-08 10:00:50 +01:00
|
|
|
_ = os.MkdirAll(configDir, 0755)
|
2025-12-08 09:49:07 +01:00
|
|
|
|
|
|
|
|
viper.AddConfigPath(configDir)
|
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
viper.SetEnvPrefix("FGJ")
|
|
|
|
|
|
2025-12-08 10:00:50 +01:00
|
|
|
_ = viper.ReadInConfig()
|
2025-12-08 09:49:07 +01:00
|
|
|
}
|