main.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. bundleCommand = &cobra.Command{
  21. Use: "bundle",
  22. Short: "Bundle",
  23. Long: "Bundle related commands",
  24. }
  25. )
  26. func init() {
  27. cobra.OnInitialize(initConfig)
  28. rootCommand.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dify.yaml)")
  29. rootCommand.AddCommand(pluginCommand)
  30. rootCommand.AddCommand(bundleCommand)
  31. }
  32. func initConfig() {
  33. if cfgFile != "" {
  34. // Use config file from the flag.
  35. viper.SetConfigFile(cfgFile)
  36. } else {
  37. // Find home directory.
  38. home, err := os.UserHomeDir()
  39. cobra.CheckErr(err)
  40. // Search config in home directory with name ".dify" (without extension).
  41. viper.AddConfigPath(home)
  42. viper.SetConfigType("yaml")
  43. viper.SetConfigName(".dify")
  44. }
  45. viper.AutomaticEnv()
  46. if err := viper.ReadInConfig(); err == nil {
  47. fmt.Println("Using config file:", viper.ConfigFileUsed())
  48. }
  49. }
  50. func main() {
  51. rootCommand.Execute()
  52. }