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
22---`, url, time.Now().Format("2006-01-02"))
23
24 if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
25 _, err := os.Create(path)
26 if err != nil {
27 return err
28 }
29 os.WriteFile(path, []byte(content), 0755)
30 fmt.Printf("vite: created new post at %s\n", path)
31 return nil
32 }
33
34 fmt.Printf("error: %s already exists\n", path)
35 return nil
36}