main.go 1.6 KB

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