mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(wireguard): Add code actions
This commit is contained in:
parent
8bf626ebb6
commit
911e080fbb
@ -7,7 +7,8 @@ import (
|
|||||||
type codeActionName string
|
type codeActionName string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
codeActionGeneratePrivateKey codeActionName = "generatePrivateKey"
|
codeActionGeneratePrivateKey codeActionName = "generatePrivateKey"
|
||||||
|
codeActionGeneratePresharedKey codeActionName = "generatePresharedKey"
|
||||||
)
|
)
|
||||||
|
|
||||||
type codeActionGeneratePrivateKeyArgs struct {
|
type codeActionGeneratePrivateKeyArgs struct {
|
||||||
@ -22,13 +23,26 @@ func codeActionGeneratePrivateKeyArgsFromArguments(arguments map[string]any) cod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type codeActionGeneratePresharedKeyArgs struct {
|
||||||
|
URI protocol.DocumentUri
|
||||||
|
Line uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func codeActionGeneratePresharedKeyArgsFromArguments(arguments map[string]any) codeActionGeneratePresharedKeyArgs {
|
||||||
|
return codeActionGeneratePresharedKeyArgs{
|
||||||
|
URI: arguments["URI"].(protocol.DocumentUri),
|
||||||
|
Line: uint32(arguments["Line"].(float64)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p wireguardProperty) getInsertRange(line uint32) protocol.Range {
|
func (p wireguardProperty) getInsertRange(line uint32) protocol.Range {
|
||||||
insertPosition := p.Separator.Location.End
|
var insertPosition uint32 = p.Separator.Location.End
|
||||||
var length uint32 = 0
|
var length uint32 = 0
|
||||||
|
|
||||||
if p.Value != nil {
|
if p.Value != nil {
|
||||||
// Length of the value
|
insertPosition = p.Value.Location.Start - 1
|
||||||
length = p.Value.Location.End - p.Value.Location.Start
|
// Length of the value; +1 because of the starting space
|
||||||
|
length = (p.Value.Location.End - p.Value.Location.Start) + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return protocol.Range{
|
return protocol.Range{
|
||||||
@ -71,3 +85,32 @@ func (p *wireguardParser) runGeneratePrivateKey(args codeActionGeneratePrivateKe
|
|||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *wireguardParser) runGeneratePresharedKey(args codeActionGeneratePresharedKeyArgs) (*protocol.ApplyWorkspaceEditParams, error) {
|
||||||
|
presharedKey, err := createPresharedKey()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return &protocol.ApplyWorkspaceEditParams{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
section, property := p.getPropertyByLine(args.Line)
|
||||||
|
|
||||||
|
if section == nil || property == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
label := "Generate Preshared Key"
|
||||||
|
return &protocol.ApplyWorkspaceEditParams{
|
||||||
|
Label: &label,
|
||||||
|
Edit: protocol.WorkspaceEdit{
|
||||||
|
Changes: map[protocol.DocumentUri][]protocol.TextEdit{
|
||||||
|
args.URI: {
|
||||||
|
{
|
||||||
|
NewText: " " + presharedKey,
|
||||||
|
Range: property.getInsertRange(args.Line),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
@ -39,6 +39,29 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa
|
|||||||
Command: &command,
|
Command: &command,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
|
case "PresharedKey":
|
||||||
|
if !areWireguardToolsAvailable() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
commandID := "wireguard." + codeActionGeneratePresharedKey
|
||||||
|
command := protocol.Command{
|
||||||
|
Title: "Generate PresharedKey",
|
||||||
|
Command: string(commandID),
|
||||||
|
Arguments: []any{
|
||||||
|
codeActionGeneratePresharedKeyArgs{
|
||||||
|
URI: params.TextDocument.URI,
|
||||||
|
Line: line,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return []protocol.CodeAction{
|
||||||
|
{
|
||||||
|
Title: "Generate PresharedKey",
|
||||||
|
Command: &command,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -2,9 +2,12 @@ package wireguard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var whitespacePattern = regexp.MustCompile(`[\s\n]+`)
|
||||||
|
|
||||||
func areWireguardToolsAvailable() bool {
|
func areWireguardToolsAvailable() bool {
|
||||||
_, err := exec.LookPath("wg")
|
_, err := exec.LookPath("wg")
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ func createNewPrivateKey() (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(bytes), nil
|
return string(whitespacePattern.ReplaceAll(bytes, []byte(""))), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createPublicKey(privateKey string) (string, error) {
|
func createPublicKey(privateKey string) (string, error) {
|
||||||
@ -33,5 +36,17 @@ func createPublicKey(privateKey string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ReplaceAll(string(bytes), "\n", ""), nil
|
return string(whitespacePattern.ReplaceAll(bytes, []byte(""))), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createPresharedKey() (string, error) {
|
||||||
|
cmd := exec.Command("wg", "genpsk")
|
||||||
|
|
||||||
|
bytes, err := cmd.Output()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(whitespacePattern.ReplaceAll(bytes, []byte(""))), nil
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,7 @@ func (p *wireguardParser) getBelongingSectionByLine(line uint32) *wireguardSecti
|
|||||||
func (p *wireguardParser) getPropertyByLine(line uint32) (*wireguardSection, *wireguardProperty) {
|
func (p *wireguardParser) getPropertyByLine(line uint32) (*wireguardSection, *wireguardProperty) {
|
||||||
section := p.getSectionByLine(line)
|
section := p.getSectionByLine(line)
|
||||||
|
|
||||||
if section.Name == nil {
|
if section == nil || section.Name == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,12 @@ func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteComm
|
|||||||
parser := documentParserMap[args.URI]
|
parser := documentParserMap[args.URI]
|
||||||
|
|
||||||
return parser.runGeneratePrivateKey(args)
|
return parser.runGeneratePrivateKey(args)
|
||||||
|
case string(codeActionGeneratePresharedKey):
|
||||||
|
args := codeActionGeneratePresharedKeyArgsFromArguments(params.Arguments[0].(map[string]any))
|
||||||
|
|
||||||
|
parser := documentParserMap[args.URI]
|
||||||
|
|
||||||
|
return parser.runGeneratePresharedKey(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user