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