Skip to content

Commit 2cafb0a

Browse files
authored
Merge pull request #26 from nilotpaul/cli
feat(cli): added flags for stack config, new option for choosing rendering strategy & opts display with help cmd
2 parents d199240 + 3267194 commit 2cafb0a

File tree

11 files changed

+211
-90
lines changed

11 files changed

+211
-90
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ run: build
1313
@$(BINARY)
1414

1515
build:
16-
@CGO_ENABLED=0 go build -ldflags="-s -w" -o $(BINARY) .
16+
@go build -o $(BINARY) .
1717
@GOOS=windows GOARCH=amd64 go build -o $(BINARY).exe .
1818

1919
test:

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,22 @@ or download prebuilt binary
3232

3333
# Usage
3434

35-
```
35+
## Create a new project
36+
```sh
3637
gospur init [project-name]
3738
```
39+
## Update the CLI
40+
```sh
41+
gospur update
42+
```
43+
44+
Check more options by:
45+
```sh
46+
# help for cli
47+
gospur --help
48+
# help for each command
49+
gospur init --help
50+
```
3851

3952
# Docs
4053

@@ -57,7 +70,7 @@ Read detailed usage and examples of every stack configured.
5770
- Chi
5871

5972
**Styling**
60-
- Vanilla CSS
73+
- Vanilla
6174
- Tailwind
6275

6376
**UI Library**

cli.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package main
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/nilotpaul/gospur/config"
8+
"github.com/nilotpaul/gospur/util"
79
"github.com/spf13/cobra"
810
)
911

@@ -50,6 +52,28 @@ func Execute() error {
5052
}
5153

5254
func init() {
55+
// Flags for init cmd.
56+
initCmd.Flags().StringVar(
57+
&stackConfig.WebFramework, "framework", "",
58+
strings.Join(config.WebFrameworkOpts, ", "),
59+
)
60+
initCmd.Flags().StringVar(
61+
&stackConfig.CssStrategy, "styling", "",
62+
strings.Join(config.CssStrategyOpts, ", "),
63+
)
64+
initCmd.Flags().StringVar(
65+
&stackConfig.UILibrary, "ui", "",
66+
strings.Join(util.GetMapKeys(config.UILibraryOpts), ", "),
67+
)
68+
initCmd.Flags().StringVar(
69+
&stackConfig.RenderingStrategy, "render", "",
70+
strings.Join(config.RenderingStrategy, ", "),
71+
)
72+
initCmd.Flags().StringSliceVar(
73+
&stackConfig.ExtraOpts, "extra", []string{},
74+
fmt.Sprintf("One or Many: %s", strings.Join(config.ExtraOpts, ", ")),
75+
)
76+
5377
rootCmd.AddCommand(
5478
initCmd,
5579
updateCmd,

command.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15+
var stackConfig = &util.StackConfig{}
16+
1517
// handleInitCmd handles the `init` command for gospur CLI.
1618
func handleInitCmd(cmd *cobra.Command, args []string) {
1719
targetPath, err := util.GetProjectPath(args)
@@ -21,12 +23,17 @@ func handleInitCmd(cmd *cobra.Command, args []string) {
2123
}
2224

2325
// Building the stack config by talking user prompts.
24-
stackCfg, err := util.GetStackConfig()
25-
if err != nil {
26+
if err := util.GetStackConfig(stackConfig); err != nil {
27+
fmt.Println(config.ErrMsg(err))
28+
return
29+
}
30+
31+
cfg := *stackConfig
32+
// Validate the provided options.
33+
if err := util.ValidateStackConfig(cfg); err != nil {
2634
fmt.Println(config.ErrMsg(err))
2735
return
2836
}
29-
cfg := *stackCfg
3037

3138
// Asking for the go mod path from user.
3239
goModPath, err := util.GetGoModulePath()

config/config.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ var (
3030
"Fiber",
3131
"Chi",
3232
}
33-
ExtraOpts = []string{
34-
"HTMX",
35-
"Dockerfile",
36-
}
37-
3833
CssStrategyOpts = []string{
3934
"Tailwind",
40-
"Vanilla CSS",
35+
"Vanilla",
4136
}
4237
UILibraryOpts = map[string][]string{
4338
"Preline": {"Tailwind"},
4439
"DaisyUI": {"Tailwind"},
4540
}
41+
RenderingStrategy = []string{
42+
"Templates",
43+
}
44+
45+
// Flags Only
46+
ExtraOpts = []string{
47+
"HTMX",
48+
"Dockerfile",
49+
}
4650
)
4751

4852
// Project file structure

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func main() {
1111
if err := Execute(); err != nil {
12-
fmt.Println(config.ErrMsg("Go Spur exited"))
12+
fmt.Println(config.ErrMsg("GoSpur exited"))
1313
os.Exit(1)
1414
}
1515
}

template/base/makefile.tmpl

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,6 @@
11
{{- if .IsLinux -}}
2-
start:
3-
@node ./esbuild.config.js
4-
@go build -tags '!dev' -o bin/build
5-
@ENVIRONMENT=PRODUCTION ./bin/build
6-
7-
build:
8-
@node ./esbuild.config.js
9-
@go build -tags 'dev' -o bin/build
10-
11-
dev:
12-
@wgo \
13-
-exit \
14-
-file=.go \
15-
-file=.html \
16-
-file=.css \
17-
-xdir=public \
18-
go build -tags 'dev' -o bin/build . \
19-
:: ENVIRONMENT=DEVELOPMENT ./bin/build \
20-
:: wgo -xdir=bin -xdir=node_modules -xdir=public node ./esbuild.config.js \
21-
:: wgo -dir=node_modules npx livereload -w 400 public
22-
{{- else -}}
232
# As you're not using linux, please vist https://github.com/nilotpaul/gospur/blob/main/docs/development-usage.md
24-
3+
{{- end -}}
254
start:
265
@node ./esbuild.config.js
276
@go build -tags '!dev' -o bin/build
@@ -41,5 +20,4 @@ dev:
4120
go build -tags 'dev' -o bin/build . \
4221
:: ENVIRONMENT=DEVELOPMENT ./bin/build \
4322
:: wgo -xdir=bin -xdir=node_modules -xdir=public node ./esbuild.config.js \
44-
:: wgo -dir=node_modules npx livereload -w 400 public
45-
{{- end -}}
23+
:: wgo -dir=node_modules npx livereload -w 800 web

util/command_util.go

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type StackConfig struct {
2828
// UI Library is pre-made styled libs like Preline.
2929
UILibrary string
3030

31+
// RenderingStrategy defines how HTML is rendered.
32+
// Eg. templates, templ, seperate client.
33+
RenderingStrategy string
34+
35+
// Flags Only
3136
// Extras are extra add-ons like css lib, HTMX etc.
3237
ExtraOpts []string
3338
}
@@ -54,70 +59,74 @@ type GitHubReleaseResponse struct {
5459

5560
// GetStackConfig will give a series of prompts
5661
// to the user to configure their project stack.
57-
func GetStackConfig() (*StackConfig, error) {
58-
var cfg StackConfig
59-
62+
func GetStackConfig(cfg *StackConfig) error {
6063
// Framework options
61-
frameworkPrompt := promptui.Select{
62-
Label: "Choose a web framework",
63-
Items: config.WebFrameworkOpts,
64-
}
65-
_, framework, err := frameworkPrompt.Run()
66-
if err != nil {
67-
return nil, fmt.Errorf("failed to select web framework")
68-
}
69-
cfg.WebFramework = framework
70-
71-
// CSS Strategy
72-
extraPrompt := promptui.Select{
73-
Label: "Choose a CSS Strategy",
74-
Items: config.CssStrategyOpts,
75-
}
76-
_, css, err := extraPrompt.Run()
77-
if err != nil {
78-
return nil, fmt.Errorf("failed to select CSS Strategy")
64+
if len(cfg.WebFramework) == 0 {
65+
frameworkPrompt := promptui.Select{
66+
Label: "Choose a web framework",
67+
Items: config.WebFrameworkOpts,
68+
}
69+
_, framework, err := frameworkPrompt.Run()
70+
if err != nil {
71+
return fmt.Errorf("failed to select web framework")
72+
}
73+
cfg.WebFramework = framework
7974
}
80-
cfg.CssStrategy = css
81-
82-
// UI Library Options
83-
// Filtering the opts for UI Libs based on the css strategy chosen.
84-
filteredOpts := make([]string, 0)
85-
for lib, deps := range config.UILibraryOpts {
86-
if len(deps) == 0 {
87-
filteredOpts = append(filteredOpts, lib)
88-
continue
75+
// Rendering Strategy Options
76+
if len(cfg.RenderingStrategy) == 0 {
77+
renderingStratPrompt := promptui.Select{
78+
Label: "Choose a Rendering Strategy",
79+
Items: config.RenderingStrategy,
8980
}
90-
if contains(deps, cfg.CssStrategy) {
91-
filteredOpts = append(filteredOpts, lib)
81+
_, opts, err := renderingStratPrompt.Run()
82+
if err != nil {
83+
return fmt.Errorf("failed to select Rendering Strategy")
9284
}
85+
cfg.RenderingStrategy = opts
9386
}
94-
// Only ask anything if we have a compatible UI Lib for
95-
// the chosen CSS Strategy.
96-
if len(filteredOpts) != 0 {
97-
// Asking for UI Lib if we've any filtered opts.
98-
uiLibPrompt := promptui.Select{
99-
Label: "Choose a UI Library",
100-
Items: filteredOpts,
87+
// CSS Strategy
88+
if len(cfg.CssStrategy) == 0 && cfg.RenderingStrategy == "Templates" {
89+
extraPrompt := promptui.Select{
90+
Label: "Choose a CSS Strategy",
91+
Items: config.CssStrategyOpts,
10192
}
102-
_, uiLib, err := uiLibPrompt.Run()
93+
_, css, err := extraPrompt.Run()
10394
if err != nil {
104-
return nil, fmt.Errorf("failed to select UI Library")
95+
return fmt.Errorf("failed to select CSS Strategy")
10596
}
106-
cfg.UILibrary = uiLib
97+
cfg.CssStrategy = css
10798
}
99+
// UI Library Options
100+
if len(cfg.UILibrary) == 0 && cfg.RenderingStrategy == "Templates" {
101+
// Filtering the opts for UI Libs based on the css strategy chosen.
102+
filteredOpts := make([]string, 0)
103+
for lib, deps := range config.UILibraryOpts {
104+
if len(deps) == 0 {
105+
filteredOpts = append(filteredOpts, lib)
106+
continue
107+
}
108+
if contains(deps, cfg.CssStrategy) {
109+
filteredOpts = append(filteredOpts, lib)
110+
}
111+
}
108112

109-
// Extra Add-Ons
110-
extraOptsPrompt := ui.MultiSelect{
111-
Label: "Choose one or many extra options",
112-
Items: config.ExtraOpts,
113-
}
114-
opts, err := extraOptsPrompt.Run()
115-
if err != nil {
116-
return nil, fmt.Errorf("failed to select extra options")
113+
// Only ask anything if we have a compatible UI Lib for
114+
// the chosen CSS Strategy.
115+
if len(filteredOpts) != 0 {
116+
// Asking for UI Lib if we've any filtered opts.
117+
uiLibPrompt := promptui.Select{
118+
Label: "Choose a UI Library",
119+
Items: filteredOpts,
120+
}
121+
_, uiLib, err := uiLibPrompt.Run()
122+
if err != nil {
123+
return fmt.Errorf("failed to select UI Library")
124+
}
125+
cfg.UILibrary = uiLib
126+
}
117127
}
118-
cfg.ExtraOpts = opts
119128

120-
return &cfg, nil
129+
return nil
121130
}
122131

123132
// GetGoModulePath will give a input prompt to the user

0 commit comments

Comments
 (0)