feat(wireguard): Add code actions

This commit is contained in:
Myzel394 2024-08-18 22:14:19 +02:00
parent 8bf626ebb6
commit 911e080fbb
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
5 changed files with 94 additions and 7 deletions

View File

@ -7,7 +7,8 @@ import (
type codeActionName string
const (
codeActionGeneratePrivateKey codeActionName = "generatePrivateKey"
codeActionGeneratePrivateKey codeActionName = "generatePrivateKey"
codeActionGeneratePresharedKey codeActionName = "generatePresharedKey"
)
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 {
insertPosition := p.Separator.Location.End
var insertPosition uint32 = p.Separator.Location.End
var length uint32 = 0
if p.Value != nil {
// Length of the value
length = p.Value.Location.End - p.Value.Location.Start
insertPosition = p.Value.Location.Start - 1
// Length of the value; +1 because of the starting space
length = (p.Value.Location.End - p.Value.Location.Start) + 1
}
return protocol.Range{
@ -71,3 +85,32 @@ func (p *wireguardParser) runGeneratePrivateKey(args codeActionGeneratePrivateKe
},
}, 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
}

View File

@ -39,6 +39,29 @@ func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionPa
Command: &command,
},
}, 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

View File

@ -2,9 +2,12 @@ package wireguard
import (
"os/exec"
"regexp"
"strings"
)
var whitespacePattern = regexp.MustCompile(`[\s\n]+`)
func areWireguardToolsAvailable() bool {
_, err := exec.LookPath("wg")
@ -20,7 +23,7 @@ func createNewPrivateKey() (string, error) {
return "", err
}
return string(bytes), nil
return string(whitespacePattern.ReplaceAll(bytes, []byte(""))), nil
}
func createPublicKey(privateKey string) (string, error) {
@ -33,5 +36,17 @@ func createPublicKey(privateKey string) (string, error) {
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
}

View File

@ -331,7 +331,7 @@ func (p *wireguardParser) getBelongingSectionByLine(line uint32) *wireguardSecti
func (p *wireguardParser) getPropertyByLine(line uint32) (*wireguardSection, *wireguardProperty) {
section := p.getSectionByLine(line)
if section.Name == nil {
if section == nil || section.Name == nil {
return nil, nil
}

View File

@ -17,6 +17,12 @@ func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteComm
parser := documentParserMap[args.URI]
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