Implement cli argument for file path, path validation, file type validation

This commit is contained in:
Daniel Fichtinger 2024-10-19 03:14:20 -04:00
parent 1667fb10ef
commit 26adec4a97
6 changed files with 111 additions and 2 deletions

View file

@ -1,9 +1,57 @@
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/ficcdaf/zona/internal/util"
)
func main() {
fmt.Println("Hello, world!")
// validateFile checks whether a given path
// is a valid file && matches an expected extension
func validateFile(path, ext string) bool {
return (util.CheckExtension(path, ext) == nil) && (util.PathIsValid(path, true))
}
func main() {
mdPath := flag.String("file", "", "Path to the markdown file.")
flag.Parse()
if *mdPath == "" {
// no flag provided, check for positional argument instead
n := flag.NArg()
var e error
switch n {
case 1:
// we read the positional arg
arg := flag.Arg(0)
// mdPath wants a pointer so we get arg's address
mdPath = &arg
case 0:
// in case of no flag and no arg, we fail
e = errors.New("Required argument missing!")
default:
// more args than expected is also fail
e = errors.New("Unexpected arguments!")
}
if e != nil {
fmt.Printf("Error: %s\n", e.Error())
os.Exit(1)
}
}
if !validateFile(*mdPath, ".md") {
fmt.Println("File validation failed!")
os.Exit(1)
}
file, err := os.Open(*mdPath)
if err != nil {
fmt.Println("Error reading file:", err)
os.Exit(1)
}
defer file.Close()
fmt.Println("File opened :)")
}

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/ficcdaf/zona
go 1.23.2
require github.com/gomarkdown/markdown v0.0.0-20240930133441-72d49d9543d8 // indirect

View file

@ -0,0 +1,22 @@
package convert
import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
// This function takes a Markdown document and returns an HTML document.
func MdToHTML(md []byte) ([]byte, error) {
// create parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(md)
// build HTML renderer
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
return markdown.Render(doc, renderer), nil
}

33
internal/util/path.go Normal file
View file

@ -0,0 +1,33 @@
// Package util provides general utilities.
package util
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
)
// CheckExtension checks if the file located at path (string)
// matches the provided extension type
func CheckExtension(path, ext string) error {
if filepath.Ext(path) == ext {
return nil
} else {
return errors.New("Invalid extension.")
}
}
// PathIsValid checks if a path is valid.
// If requireFile is set, directories are not considered valid.
func PathIsValid(path string, requireFile bool) bool {
s, err := os.Stat(path)
if os.IsNotExist(err) {
return false
} else if requireFile {
fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
return !s.IsDir()
}
return err == nil
}

3
test/in.md Normal file
View file

@ -0,0 +1,3 @@
# My amazing markdown file!
I can _even_ do **this**!

1
zona Symbolic link
View file

@ -0,0 +1 @@
bin/zona