Created
December 5, 2024 00:14
-
-
Save jbranchaud/64d0ad3f3e982489e410f6400a465d6c to your computer and use it in GitHub Desktop.
quick TIL file creator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"path/filepath" | |
"regexp" | |
"strings" | |
"time" | |
) | |
// qtil -- quick TIL file creator | |
// | |
// Usage: | |
// ./qtil --lang go This Is A New Post | |
// # creates `go/this-is-a-new-post.md` | |
// # with a heading "This Is A New Post" | |
func main() { | |
language := flag.String("lang", "", "language or technology post falls under") | |
technology := flag.String("tech", "", "language or technology post falls under") | |
category := flag.String("cat", "", "category post falls under") | |
flag.Parse() | |
bucket, err := coalesce(*language, *technology, *category) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
relativeDir := "./" + bucket | |
if !dirExists(relativeDir) { | |
fmt.Printf("The dir '%s' doesn't exist.\n", relativeDir) | |
os.Exit(1) | |
} | |
args := flag.Args() | |
title := strings.Join(args, " ") | |
filename := strings.Join([]string{dasherize(title), "md"}, ".") | |
path := filepath.Join(bucket, filename) | |
fmt.Println("Path:", path) | |
fmt.Println("Title:", title) | |
assertFileDoesNotExist(path) | |
fmt.Print("Let's build the file now") | |
takeABeat(3) | |
newFile, err := os.Create(path) | |
if err != nil { | |
fmt.Printf("Error creating the file: %v", err) | |
os.Exit(1) | |
} | |
defer newFile.Close() | |
content := fmt.Sprintf("# %s\n\n", title) | |
_, err = newFile.WriteString(content) | |
if err != nil { | |
fmt.Printf("Error writing to file: %v", err) | |
os.Exit(1) | |
} | |
fmt.Println("Done building the file, check it out") | |
} | |
func dasherize(title string) string { | |
words := strings.Fields(title) | |
nonWordCharMatcher := regexp.MustCompile("[^a-zA-Z0-9]+") | |
var sanitizedWords []string | |
for _, word := range words { | |
sanitizedWord := nonWordCharMatcher.ReplaceAllString(word, "") | |
lowerSanitizedWord := strings.ToLower(sanitizedWord) | |
if len(lowerSanitizedWord) > 0 { | |
sanitizedWords = append(sanitizedWords, lowerSanitizedWord) | |
} | |
} | |
return strings.Join(sanitizedWords, "-") | |
} | |
func dirExists(dir string) bool { | |
if fileInfo, err := os.Stat(dir); err == nil { | |
return fileInfo.IsDir() | |
} | |
return false | |
} | |
func assertFileDoesNotExist(pathname string) { | |
if fileInfo, err := os.Stat(pathname); err == nil { | |
if fileInfo.Mode().IsRegular() { | |
msg := fmt.Sprintf("%s already exists, could not proceed", pathname) | |
panic(msg) | |
} else { | |
msg := fmt.Sprintf("%s exists, but is not a regular file", pathname) | |
panic(msg) | |
} | |
} | |
} | |
func coalesce(strs ...string) (string, error) { | |
for _, str := range strs { | |
if len(str) > 0 { | |
return str, nil | |
} | |
} | |
msg := "None of `--tech`, `--lang` or `--cat` were specified, see `--help` for details." | |
return "", fmt.Errorf(msg) | |
} | |
func takeABeat(n int) { | |
for range n { | |
time.Sleep(1 * time.Second) | |
fmt.Print(".") | |
} | |
time.Sleep(500 * time.Millisecond) | |
fmt.Print("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment