Skip to content

Commit dbd599c

Browse files
committed
feat: add init command and it's actions
1 parent 3ffb933 commit dbd599c

File tree

7 files changed

+206
-16
lines changed

7 files changed

+206
-16
lines changed

commands/init.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/barelyhuman/alvu/pkg/alvu"
9+
"github.com/barelyhuman/go/color"
10+
"github.com/urfave/cli/v2"
11+
)
12+
13+
func AlvuInit(c *cli.Context) (err error) {
14+
basePath := c.Args().First()
15+
forceFlag := c.Bool("force")
16+
logger := alvu.NewLogger()
17+
logger.LogPrefix = "[alvu]"
18+
19+
fileInfo, err := os.Stat(basePath)
20+
21+
if err == nil {
22+
if fileInfo.IsDir() && !forceFlag {
23+
logger.Error(fmt.Sprintf("Directory: %v , already exists, cannot overwrite, if you wish to force overwrite use the -f flag with the `init` command", basePath))
24+
os.Exit(1)
25+
}
26+
}
27+
28+
mustCreateDir(basePath, "public")
29+
mustCreateDir(basePath, "hooks")
30+
mustCreateDir(basePath, "pages")
31+
prepareBaseStyles(basePath)
32+
preparePages(basePath)
33+
34+
logger.Success(
35+
fmt.Sprintf("Alvu initialized in: %v", basePath),
36+
)
37+
38+
runStr := color.ColorString{}
39+
40+
fmt.Println(runStr.Dim("\n> Run the following to get started").String())
41+
42+
commandStr := color.ColorString{}
43+
commandStr.Cyan(
44+
fmt.Sprintf("\n alvu -s -path %v\n", basePath),
45+
)
46+
fmt.Println(commandStr.String())
47+
return
48+
}
49+
50+
func mustCreateDir(root, dir string) {
51+
pathToCreate := filepath.Join(root, dir)
52+
err := os.MkdirAll(pathToCreate, os.ModePerm)
53+
if err != nil {
54+
panic(fmt.Sprintf("Failed to create %v due to error: %v\n", pathToCreate, err))
55+
}
56+
}
57+
58+
func prepareBaseStyles(root string) {
59+
fileHandle, err := os.OpenFile(filepath.Join(root, "public", "styles.css"), os.O_CREATE|os.O_RDWR, os.ModePerm)
60+
if err != nil {
61+
fmt.Printf("Failed to open file public/styles.css with error: %v", err)
62+
}
63+
defer fileHandle.Sync()
64+
defer fileHandle.Close()
65+
66+
fileHandle.WriteString(`
67+
/* Resets */
68+
html {
69+
box-sizing: border-box;
70+
font-size: 16px;
71+
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
72+
}
73+
74+
*, *:before, *:after {
75+
box-sizing: inherit;
76+
}
77+
78+
body, h1, h2, h3, h4, h5, h6, p {
79+
margin: 0;
80+
padding: 0;
81+
font-weight: normal;
82+
}
83+
84+
img {
85+
max-width: 100%;
86+
height: auto;
87+
}
88+
89+
/* Styles */
90+
91+
:root {
92+
--base: #efefef;
93+
--text: #181819;
94+
}
95+
96+
@media (prefers-color-scheme: dark) {
97+
:root {
98+
--base: #181819;
99+
--text: #efefef;
100+
}
101+
}
102+
103+
body {
104+
105+
background: var(--base);
106+
color: var(--text);
107+
108+
max-width: 900px;
109+
margin: 0 auto;
110+
padding: 4px;
111+
display: flex;
112+
flex-direction: column;
113+
justify-content: center;
114+
min-height: 100vh;
115+
}
116+
117+
118+
ol,ul,p{
119+
line-height: 1.7;
120+
}
121+
122+
`)
123+
124+
}
125+
126+
func preparePages(root string) {
127+
layoutHandle, _ := os.OpenFile(filepath.Join(root, "pages", "_layout.html"), os.O_CREATE|os.O_RDWR, os.ModePerm)
128+
defer layoutHandle.Sync()
129+
defer layoutHandle.Close()
130+
131+
rootPageHandle, _ := os.OpenFile(filepath.Join(root, "pages", "index.md"), os.O_CREATE|os.O_RDWR, os.ModePerm)
132+
133+
defer rootPageHandle.Sync()
134+
defer rootPageHandle.Close()
135+
136+
layoutHandle.WriteString(`<!DOCTYPE html>
137+
<html lang="en">
138+
<head>
139+
<meta charset="UTF-8">
140+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
141+
<title>Alvu | Minimal Starter</title>
142+
<link href="/styles.css" rel="stylesheet">
143+
</head>
144+
<body>
145+
<slot></slot>
146+
</body>
147+
</html>`)
148+
149+
rootPageHandle.WriteString(`# Alvu
150+
151+
- Scriptable
152+
- Fast
153+
- Tiny
154+
155+
In whatever order you'd like...
156+
157+
`)
158+
159+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/barelyhuman/alvu
33
go 1.18
44

55
require (
6-
github.com/barelyhuman/go v0.2.2
6+
github.com/barelyhuman/go v0.2.3-0.20240516192751-30a6c804e4e5
77
github.com/cjoudrey/gluahttp v0.0.0-20201111170219-25003d9adfa9
88
github.com/joho/godotenv v1.5.1
99
github.com/urfave/cli/v2 v2.27.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
99
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
1010
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
1111
github.com/aws/aws-sdk-go v1.34.0/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
12-
github.com/barelyhuman/go v0.2.2 h1:Lpk1XrlP40F3II8BibVzViZUOJ1GgDdzXUBb8ENwb0U=
13-
github.com/barelyhuman/go v0.2.2/go.mod h1:hox2iDYZAarjpS7jKQeYIi2F+qMA8KLMtCws++L2sSY=
12+
github.com/barelyhuman/go v0.2.3-0.20240516192751-30a6c804e4e5 h1:AbJ6ZaRkEc6CguQ6rXe0epKmFe/TmwSSFX5N6hsNO+A=
13+
github.com/barelyhuman/go v0.2.3-0.20240516192751-30a6c804e4e5/go.mod h1:hox2iDYZAarjpS7jKQeYIi2F+qMA8KLMtCws++L2sSY=
1414
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
1515
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1616
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ func main() {
7272
Version: version,
7373
Compiled: time.Now(),
7474
HideVersion: false,
75+
Commands: []*cli.Command{
76+
{
77+
Name: "init",
78+
Description: "Initialise a new alvu Project",
79+
Args: true,
80+
ArgsUsage: "<directory>",
81+
Flags: []cli.Flag{
82+
&cli.BoolFlag{
83+
Name: "force",
84+
Aliases: []string{"f"},
85+
Usage: "Force create in the directory even overwriting any files that exist",
86+
},
87+
},
88+
Action: func(ctx *cli.Context) error {
89+
return commands.AlvuInit(ctx)
90+
},
91+
},
92+
},
7593
}
7694

7795
err := app.Run(os.Args)

pkg/alvu/alvu.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ func (ac *AlvuConfig) Rebuild(path string) {
7474
}
7575

7676
func (ac *AlvuConfig) Run() error {
77-
ac.logger = Logger{
78-
logPrefix: "[alvu]",
79-
}
77+
ac.logger = NewLogger()
78+
ac.logger.LogPrefix = "[alvu]"
8079

8180
ac.rebuildCond = sync.NewCond(&ac.rebuildLock)
8281
ac.connections = make(map[*websocket.Conn]struct{})
@@ -158,9 +157,11 @@ func (ac *AlvuConfig) Build() error {
158157
return err
159158
}
160159

161-
ac.watcher.AddDir(pageDir)
162-
ac.watcher.AddDir(publicDir)
163-
ac.watcher.AddDir(hooksDir)
160+
if ac.Serve {
161+
ac.watcher.AddDir(pageDir)
162+
ac.watcher.AddDir(publicDir)
163+
ac.watcher.AddDir(hooksDir)
164+
}
164165

165166
normalizedFiles, err := runTransfomers(filesToProcess, ac)
166167
if err != nil {

pkg/alvu/hooks.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package alvu
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"io/fs"
78
"net/http"
89
"os"
@@ -52,13 +53,20 @@ func (h *Hooks) Load() {
5253
if os.IsNotExist(err) {
5354
return
5455
}
56+
5557
readHookDirError(err, hookDir, h.ac.logger)
5658
}
5759

5860
file, err := os.Open(hookDir)
5961
readHookDirError(err, hookDir, h.ac.logger)
6062
childs, err := file.Readdirnames(1)
61-
readHookDirError(err, hookDir, h.ac.logger)
63+
if err != nil {
64+
if err == io.EOF {
65+
return
66+
}
67+
readHookDirError(err, hookDir, h.ac.logger)
68+
}
69+
6270
if len(childs) == 0 {
6371
return
6472
}

pkg/alvu/logger.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
)
99

1010
type Logger struct {
11-
logPrefix string
11+
LogPrefix string
12+
}
13+
14+
func NewLogger() Logger {
15+
return Logger{}
1216
}
1317

1418
func (l *Logger) Debug(msg string) {
@@ -17,30 +21,30 @@ func (l *Logger) Debug(msg string) {
1721
return
1822
}
1923
cs := color.ColorString{}
20-
cs.Gray(l.logPrefix).Reset(" ").Gray("-").Reset(" ").Gray(msg)
24+
cs.Gray(l.LogPrefix).Reset(" ").Gray("-").Reset(" ").Gray(msg)
2125
fmt.Println(cs.String())
2226
}
2327

2428
func (l *Logger) Success(msg string) {
2529
cs := color.ColorString{}
26-
cs.Gray(l.logPrefix).Reset(" ").Green("✔").Reset(" ").Green(msg)
30+
cs.Gray(l.LogPrefix).Reset(" ").Green("✔").Reset(" ").Green(msg)
2731
fmt.Println(cs.String())
2832
}
2933

3034
func (l *Logger) Info(msg string) {
3135
cs := color.ColorString{}
32-
cs.Gray(l.logPrefix).Reset(" ").Cyan("ℹ").Reset(" ").Cyan(msg)
36+
cs.Gray(l.LogPrefix).Reset(" ").Cyan("ℹ").Reset(" ").Cyan(msg)
3337
fmt.Println(cs.String())
3438
}
3539

3640
func (l *Logger) Warning(msg string) {
3741
cs := color.ColorString{}
38-
cs.Gray(l.logPrefix).Reset(" ").Yellow(msg)
42+
cs.Gray(l.LogPrefix).Reset(" ").Yellow(msg)
3943
fmt.Println(cs.String())
4044
}
4145

4246
func (l *Logger) Error(msg string) {
4347
cs := color.ColorString{}
44-
cs.Gray(l.logPrefix).Reset(" ").Red(msg)
48+
cs.Gray(l.LogPrefix).Reset(" ").Red(msg)
4549
fmt.Println(cs.String())
4650
}

0 commit comments

Comments
 (0)