Skip to content

Commit

Permalink
add init command, Co-authored-by: varphi-online <[email protected]> (
Browse files Browse the repository at this point in the history
…#32)

* add init command

* Some more fixes, trying to give credit.

---------

Co-authored-by: varphi-online <[email protected]>
  • Loading branch information
TimoKats and varphi-online authored Sep 20, 2024
1 parent 07e0235 commit 95c807f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 47 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
8 changes: 4 additions & 4 deletions lib/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ 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 {
return validCommand, nil
}
}
}
return "", errors.New("No valid command found. Use mdrss <<ls, update, conf>>")
return "", errors.New("No valid command found. Use mdrss <<ls, update, conf, init>>")
}

func defaultConfigPath() string {
func DefaultConfigPath() string {
dirname, _ := os.UserHomeDir()
return dirname + "/.mdrss/config.json"
}

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
Expand Down
87 changes: 87 additions & 0 deletions lib/commands.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 6 additions & 42 deletions mdrss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand All @@ -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
}
Expand Down

0 comments on commit 95c807f

Please sign in to comment.