package config import ( "fmt" "os" "path/filepath" "github.com/spf13/cobra" ) // Config holds the application configuration type Config struct { DataDir string Port int Host string } // NewConfig creates a new config with default values func NewConfig() *Config { return &Config{ DataDir: "./data", Port: 8080, Host: "127.0.0.1", } } // AddFlags adds the CLI flags to the command func (c *Config) AddFlags(cmd *cobra.Command) { cmd.PersistentFlags().StringVar(&c.DataDir, "data-dir", c.DataDir, "Storage path for markdown files") cmd.PersistentFlags().IntVar(&c.Port, "port", c.Port, "Server port") cmd.PersistentFlags().StringVar(&c.Host, "host", c.Host, "Bind address") } // Validate validates the configuration func (c *Config) Validate() error { if c.Port < 1 || c.Port > 65535 { return fmt.Errorf("invalid port: %d", c.Port) } return nil } // DataDirPath returns the absolute path to the data directory func (c *Config) DataDirPath() string { absPath, err := filepath.Abs(c.DataDir) if err != nil { return c.DataDir } return absPath } // EnsureDataDir ensures the data directory exists func (c *Config) EnsureDataDir() error { return os.MkdirAll(c.DataDirPath(), 0755) }