main.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. versionCommand = &cobra.Command{
  26. Use: "version",
  27. Short: "Version",
  28. Long: "Show the version of dify cli",
  29. Run: func(cmd *cobra.Command, args []string) {
  30. fmt.Println(VersionX)
  31. },
  32. }
  33. )
  34. func init() {
  35. cobra.OnInitialize(initConfig)
  36. rootCommand.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dify.yaml)")
  37. rootCommand.AddCommand(pluginCommand)
  38. rootCommand.AddCommand(bundleCommand)
  39. rootCommand.AddCommand(versionCommand)
  40. }
  41. func initConfig() {
  42. if cfgFile != "" {
  43. // Use config file from the flag.
  44. viper.SetConfigFile(cfgFile)
  45. } else {
  46. // Find home directory.
  47. home, err := os.UserHomeDir()
  48. cobra.CheckErr(err)
  49. // Search config in home directory with name ".dify" (without extension).
  50. viper.AddConfigPath(home)
  51. viper.SetConfigType("yaml")
  52. viper.SetConfigName(".dify")
  53. }
  54. viper.AutomaticEnv()
  55. if err := viper.ReadInConfig(); err == nil {
  56. fmt.Println("Using config file:", viper.ConfigFileUsed())
  57. }
  58. }
  59. func main() {
  60. rootCommand.Execute()
  61. }