diff --git a/README.md b/README.md index 82c2d54..6b19ce0 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ You can install the binary using `go install github.com/TimoKats/mdrss@latest` ( } ``` -Note, the most recent update also allows users to specify their own config path, which you can define like so. +Note, since the last update (credit to [@varphi-online](https://github.com/varphi-online)) you can also run `mdrss init` to enter an interactive prompt that will fill in this config for you. Finally, you can also specify your own config path like so. + ```shell mdrss --config other/path/to/config update ``` diff --git a/lib/args.go b/lib/args.go index cad5a52..7f30943 100644 --- a/lib/args.go +++ b/lib/args.go @@ -7,7 +7,7 @@ import ( ) func getCommand(arguments []string) (string, error) { - validCommands := []string{"update", "ls", "conf"} + validCommands := []string{"update", "ls", "conf", "init"} for _, validCommand := range validCommands { for _, argument := range arguments { if argument == validCommand { @@ -15,10 +15,10 @@ func getCommand(arguments []string) (string, error) { } } } - return "", errors.New("No valid command found. Use mdrss <>") + return "", errors.New("No valid command found. Use mdrss <>") } -func defaultConfigPath() string { +func DefaultConfigPath() string { dirname, _ := os.UserHomeDir() return dirname + "/.mdrss/config.json" } @@ -26,7 +26,7 @@ func defaultConfigPath() string { func ParseArguments(arguments []string) (map[string]*string, error) { parsedArguments := make(map[string]*string) command, commandErr := getCommand(arguments) - parsedArguments["config"] = flag.String("config", defaultConfigPath(), "path to config.json") + parsedArguments["config"] = flag.String("config", DefaultConfigPath(), "path to config.json") parsedArguments["command"] = &command flag.Parse() return parsedArguments, commandErr diff --git a/lib/commands.go b/lib/commands.go new file mode 100644 index 0000000..fd59f74 --- /dev/null +++ b/lib/commands.go @@ -0,0 +1,87 @@ +package lib + +import ( + "bufio" + "reflect" + "encoding/json" + "errors" + "os" + "fmt" + "path/filepath" + "strings" + +) + +func LsCommand(config Config) error { + files, fileErr := GetArticles(config) + if fileErr == nil { + for _, file := range files { + Info.Println(file) + } + } + return fileErr +} + +func UpdateCommand(config Config) error { + files, fileErr := GetArticles(config) + if fileErr == nil { + config.Articles = ReadMarkdown(config, files) + rssXml := CreateRSS(config) + rssErr := WriteRSS(rssXml, config) + if rssErr != nil { + return rssErr + } + Info.Printf("Content written to %s", config.OutputFile) + } + return fileErr +} + +func ConfCommand(config Config) error { + configValues := reflect.ValueOf(config) + typeOfS := configValues.Type() + for i := 0; i < configValues.NumField(); i++ { + if len(typeOfS.Field(i).Name) < 8 { + Info.Printf("%s\t\t%v\n", typeOfS.Field(i).Name, configValues.Field(i).Interface()) + } else { + Info.Printf("%s\t%v\n", typeOfS.Field(i).Name, configValues.Field(i).Interface()) + } + } + return nil +} + +func promptUsr(prompt string, attrubute *string, fallback string) { + fmt.Print("\033[33m?\033[0m " + prompt + " \033[37;2m(" + fallback + ") > \033[0m") + var input string + in := bufio.NewReader(os.Stdin) + input, err := in.ReadString('\n') + input = strings.TrimSpace(input) + if input == "" || err != nil { + *attrubute = fallback + return + } + *attrubute = input +} + +func InitCommand() error { + var configPath string + var newConfig Config + promptUsr("Feed description", &newConfig.Description, "An RSS feed") + promptUsr("Feed link", &newConfig.Link, "https://localhost:5500/index.xml") + promptUsr("Feed author", &newConfig.Author, "anonymous") + promptUsr("Config path", &configPath, DefaultConfigPath()) + promptUsr("Input folder path (where you have md files)", &newConfig.InputFolder, "") + promptUsr("Output file path", &newConfig.OutputFile, "index.xml") + + // Only update file after ALL init fields are inputted + dirErr := os.MkdirAll(filepath.Dir(configPath), 0755) + configFile, fileErr := os.Create(configPath) + out, jsonErr := json.MarshalIndent(newConfig, "", " ") + if err := errors.Join(dirErr, fileErr, jsonErr); err != nil { + return err + } + _, writeErr := configFile.Write(out) + if writeErr == nil { + Info.Println("Initialized! Run \"mdrss update\" to update your feed!") + } + return writeErr +} diff --git a/mdrss.go b/mdrss.go index e1a8e97..1a92671 100644 --- a/mdrss.go +++ b/mdrss.go @@ -2,56 +2,20 @@ package main import ( mdrss "github.com/TimoKats/mdrss/lib" - "reflect" "errors" "os" ) -func lsCommand(config mdrss.Config) error { - files, fileErr := mdrss.GetArticles(config) - if fileErr == nil { - for _, file := range files { - mdrss.Info.Println(file) - } - } - return fileErr -} - -func updateCommand(config mdrss.Config) error { - files, fileErr := mdrss.GetArticles(config) - if fileErr == nil { - config.Articles = mdrss.ReadMarkdown(config, files) - rssXml := mdrss.CreateRSS(config) - rssErr := mdrss.WriteRSS(rssXml, config) - if rssErr != nil { - return rssErr - } - mdrss.Info.Printf("Content written to %s", config.OutputFile) - } - return fileErr -} - -func confCommand(config mdrss.Config) error { - configValues := reflect.ValueOf(config) - typeOfS := configValues.Type() - for i := 0; i < configValues.NumField(); i++ { - if len(typeOfS.Field(i).Name) < 8 { - mdrss.Info.Printf("%s\t\t%v\n", typeOfS.Field(i).Name, configValues.Field(i).Interface()) - } else { - mdrss.Info.Printf("%s\t%v\n", typeOfS.Field(i).Name, configValues.Field(i).Interface()) - } - } - return nil -} - func parseCommand(command string, config mdrss.Config) error { switch (command) { case "ls": - return lsCommand(config) + return mdrss.LsCommand(config) case "conf": - return confCommand(config) + return mdrss.ConfCommand(config) case "update": - return updateCommand(config) + return mdrss.UpdateCommand(config) + case "init": + return mdrss.InitCommand() default: return errors.New("Command not found.") } @@ -61,7 +25,7 @@ func main() { arguments, argumentErr := mdrss.ParseArguments(os.Args) config, configErr := mdrss.ReadConfig(*arguments["config"]) mdrss.Info.Printf("Using config location: %v", *arguments["config"]) - if err := errors.Join(argumentErr, configErr); err != nil { + if err := errors.Join(argumentErr, configErr); err != nil && *arguments["command"] != "init" { mdrss.Error.Println(err) return }