FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
endgit-cli/cmd/init.go at main · two-tech-dev/endgit-cli · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
two-tech-dev
/
endgit-cli
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
endgit-cli
/
cmd
/
init.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (128 loc) · 3.48 KB
Breadcrumbs
endgit-cli
/
cmd
/
init.go
Copy path
File metadata and controls
152 lines (128 loc) · 3.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright © 2026 Two Tech Studio
*/
package
cmd
import
(
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
survey
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/two-tech-dev/endgit-cli/internal/log"
)
type
PluginJSON
struct
{
Name
string
`json:"name"`
Version
string
`json:"version"`
Description
string
`json:"description"`
API
[]
string
`json:"api"`
Main
string
`json:"main"`
}
type
EndgitJSON
struct
{
DisplayName
string
`json:"displayName"`
PluginType
string
`json:"pluginType"`
}
var
initCmd
=
&
cobra.
Command
{
Use
:
"init"
,
Short
:
"Initialize a new Endstone plugin configuration"
,
Run
:
func
(
cmd
*
cobra.
Command
,
args
[]
string
) {
log
.
Info
(
"Initialize Endstone Plugin"
)
dir
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
log
.
Fatal
(
"Failed to determine current directory"
,
err
)
}
currentDir
:=
filepath
.
Base
(
dir
)
reader
:=
bufio
.
NewReader
(
os
.
Stdin
)
read
:=
func
(
prompt
,
def
string
)
string
{
if
def
!=
""
{
fmt
.
Printf
(
"%s [%s]: "
,
prompt
,
def
)
}
else
{
fmt
.
Printf
(
"%s: "
,
prompt
)
}
input
,
_
:=
reader
.
ReadString
(
'\n'
)
input
=
strings
.
TrimSpace
(
input
)
if
input
==
""
{
return
def
}
return
input
}
name
:=
read
(
"Plugin Name (slug)"
,
sanitizeSlug
(
currentDir
))
displayName
:=
read
(
"Display Name"
,
currentDir
)
version
:=
read
(
"Version"
,
"1.0.0"
)
description
:=
read
(
"Description"
,
""
)
apiVersion
:=
read
(
"Required Endstone API version"
,
"^0.5.0"
)
var
typeResult
string
typePrompt
:=
&
survey.
Select
{
Message
:
"Plugin Type:"
,
Options
: []
string
{
"Python"
,
"C++"
},
}
if
err
:=
survey
.
AskOne
(
typePrompt
,
&
typeResult
);
err
!=
nil
{
log
.
Warn
(
"Initialization cancelled"
)
return
}
pluginType
:=
"PYTHON"
if
typeResult
==
"C++"
{
pluginType
=
"CPP"
}
if
name
==
""
{
log
.
Warn
(
"Initialization cancelled"
)
return
}
plugin
:=
PluginJSON
{
Name
:
name
,
Version
:
version
,
Description
:
description
,
API
: []
string
{
apiVersion
},
Main
:
"src.main"
,
}
if
pluginType
==
"CPP"
{
plugin
.
Main
=
"EndstonePlugin"
}
pluginPath
:=
filepath
.
Join
(
dir
,
"plugin.json"
)
endgitPath
:=
filepath
.
Join
(
dir
,
"endgit.json"
)
if
_
,
err
:=
os
.
Stat
(
pluginPath
);
err
==
nil
{
log
.
Warn
(
"plugin.json already exists and will be overwritten"
)
}
if
_
,
err
:=
os
.
Stat
(
endgitPath
);
err
==
nil
{
log
.
Warn
(
"endgit.json already exists and will be overwritten"
)
}
if
err
:=
writeJSON
(
pluginPath
,
plugin
);
err
!=
nil
{
log
.
Fatal
(
"Failed to write plugin.json"
,
err
)
}
endgitCfg
:=
EndgitJSON
{
DisplayName
:
displayName
,
PluginType
:
pluginType
,
}
if
err
:=
writeJSON
(
endgitPath
,
endgitCfg
);
err
!=
nil
{
log
.
Fatal
(
"Failed to write endgit.json"
,
err
)
}
log
.
SuccessBox
(
"Plugin Initialized"
,
fmt
.
Sprintf
(
"Created plugin.json and endgit.json
\n
\n
Plugin: %s
\n
Type: %s
\n
Version: %s
\n
\n
Run 'endgit publish' to push your plugin."
,
name
,
pluginType
,
version
),
)
},
}
func
init
() {
rootCmd
.
AddCommand
(
initCmd
)
}
func
writeJSON
(
path
string
,
v
interface
{})
error
{
data
,
err
:=
json
.
MarshalIndent
(
v
,
""
,
" "
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to marshal JSON: %w"
,
err
)
}
data
=
append
(
data
,
'\n'
)
return
os
.
WriteFile
(
path
,
data
,
0o644
)
}
func
sanitizeSlug
(
s
string
)
string
{
s
=
strings
.
ToLower
(
s
)
s
=
strings
.
ReplaceAll
(
s
,
" "
,
"-"
)
var
out
strings.
Builder
for
_
,
r
:=
range
s
{
if
(
r
>=
'a'
&&
r
<=
'z'
)
||
(
r
>=
'0'
&&
r
<=
'9'
)
||
r
==
'-'
{
out
.
WriteRune
(
r
)
}
}
return
out
.
String
()
}
Back
|
FazBrowse Home
|
New Git URL