commands/new.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package commands
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
func New(path string) error {
_, file := filepath.Split(path)
url := strings.TrimSuffix(file, filepath.Ext(file))
content := fmt.Sprintf(`---
atroot: true
template:
slug: %s
title:
subtitle:
date: %s
draft: true
---`, url, time.Now().Format("2006-01-02"))
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
_, err := os.Create(path)
if err != nil {
return err
}
os.WriteFile(path, []byte(content), 0755)
fmt.Printf("vite: created new post at %s\n", path)
return nil
}
fmt.Printf("error: %s already exists\n", path)
return nil
}
|