main.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/spf13/cobra"
  6. "github.com/spf13/viper"
  7. )
  8. var (
  9. cfgFile string
  10. rootCommand = &cobra.Command{
  11. Use: "dify",
  12. Short: "Dify",
  13. Long: "Dify is a cli tool to help you develop your Dify projects.",
  14. }
  15. pluginCommand = &cobra.Command{
  16. Use: "plugin",
  17. Short: "Plugin",
  18. Long: "Plugin related commands",
  19. }
  20. )
  21. func init() {
  22. cobra.OnInitialize(initConfig)
  23. rootCommand.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dify.yaml)")
  24. rootCommand.AddCommand(pluginCommand)
  25. }
  26. func initConfig() {
  27. if cfgFile != "" {
  28. // Use config file from the flag.
  29. viper.SetConfigFile(cfgFile)
  30. } else {
  31. // Find home directory.
  32. home, err := os.UserHomeDir()
  33. cobra.CheckErr(err)
  34. // Search config in home directory with name ".dify" (without extension).
  35. viper.AddConfigPath(home)
  36. viper.SetConfigType("yaml")
  37. viper.SetConfigName(".dify")
  38. }
  39. viper.AutomaticEnv()
  40. if err := viper.ReadInConfig(); err == nil {
  41. fmt.Println("Using config file:", viper.ConfigFileUsed())
  42. }
  43. }
  44. func main() {
  45. rootCommand.Execute()
  46. }