fix(server): Improvements

Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
This commit is contained in:
Myzel394 2025-05-29 16:16:45 +02:00
parent 9a1686a7d8
commit 429c2cd4be
No known key found for this signature in database
GPG Key ID: 3B955307C2FC2F11
8 changed files with 81 additions and 43 deletions

6
flake.lock generated
View File

@ -41,11 +41,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1748026106, "lastModified": 1748370509,
"narHash": "sha256-6m1Y3/4pVw1RWTsrkAK2VMYSzG4MMIj7sqUy7o8th1o=", "narHash": "sha256-QlL8slIgc16W5UaI3w7xHQEP+Qmv/6vSNTpoZrrSlbk=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "063f43f2dbdef86376cc29ad646c45c46e93234c", "rev": "4faa5f5321320e49a78ae7848582f684d64783e9",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -2,31 +2,21 @@ package docvalues
import ( import (
"config-lsp/utils" "config-lsp/utils"
protocol "github.com/tliron/glsp/protocol_3_16" "errors"
"strings" "strings"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
type PathDoesNotExistError struct{}
func (e PathDoesNotExistError) Error() string {
return "This path does not exist"
}
type PathInvalidError struct{}
func (e PathInvalidError) Error() string {
return "This path is invalid"
}
type PathType uint8 type PathType uint8
const ( const (
PathTypeFile PathType = 1 PathTypeFile PathType = 1
PathTypeDirectory PathType = 2 PathTypeDirectory PathType = 2
) )
type PathValue struct { type PathValue struct {
IsOptional bool IsOptional bool
RequiredType PathType RequiredType PathType
} }
@ -53,29 +43,66 @@ func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
return nil return nil
} else { } else {
return []*InvalidValue{{ return []*InvalidValue{{
Err: PathDoesNotExistError{}, Err: errors.New("This path does not exist"),
Start: 0, Start: 0,
End: uint32(len(value)), End: uint32(len(value)),
}} }}
} }
} }
isValid := false fileRequired := (v.RequiredType & PathTypeFile) == PathTypeFile
directoryRequired := (v.RequiredType & PathTypeDirectory) == PathTypeDirectory
if (v.RequiredType & PathTypeFile) == PathTypeFile { isValid := true
// If file is expected
if fileRequired {
// and exists
isValid = isValid && utils.IsPathFile(value) isValid = isValid && utils.IsPathFile(value)
// file not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathFile(value)
} }
if (v.RequiredType & PathTypeDirectory) == PathTypeDirectory { // if directory
if directoryRequired {
// and exists
isValid = isValid && utils.IsPathDirectory(value) isValid = isValid && utils.IsPathDirectory(value)
// directory not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathDirectory(value)
} }
if isValid { if isValid {
return nil return nil
} }
if fileRequired && directoryRequired {
return []*InvalidValue{{
Err: errors.New("This must be either a file or a directory"),
Start: 0,
End: uint32(len(value)),
}}
}
if fileRequired {
return []*InvalidValue{{
Err: errors.New("This must be a file"),
Start: 0,
End: uint32(len(value)),
}}
}
if directoryRequired {
return []*InvalidValue{{
Err: errors.New("This must be a directory"),
Start: 0,
End: uint32(len(value)),
}}
}
return []*InvalidValue{{ return []*InvalidValue{{
Err: PathInvalidError{}, Err: errors.New("This path is invalid"),
Start: 0, Start: 0,
End: uint32(len(value)), End: uint32(len(value)),
}} }}

View File

@ -15,7 +15,7 @@ var UserDeclaration = "`user`"
var PathField = docvalues.DocumentationValue{ var PathField = docvalues.DocumentationValue{
Documentation: "Append messages to file, specified by its absolute pathname", Documentation: "Append messages to file, specified by its absolute pathname",
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: true, IsOptional: true,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
} }
@ -41,7 +41,7 @@ var EmailDeclaration = "`user-part@domain-part`"
var IncludeField = docvalues.DocumentationValue{ var IncludeField = docvalues.DocumentationValue{
Documentation: "Include any definitions in file as alias entries. The format of the file is identical to this one.", Documentation: "Include any definitions in file as alias entries. The format of the file is identical to this one.",
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
} }

View File

@ -16,7 +16,7 @@ var LabelField = docvalues.RegexValue{
var SpecField = docvalues.OrValue{ var SpecField = docvalues.OrValue{
Values: []docvalues.DeprecatedValue{ Values: []docvalues.DeprecatedValue{
docvalues.PathValue{ docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
docvalues.KeyEnumAssignmentValue{ docvalues.KeyEnumAssignmentValue{

View File

@ -139,7 +139,7 @@ rsa-sha2-512,rsa-sha2-256
Arguments to CertificateFile may use the tilde syntax to refer to a user's home directory, the tokens described in the TOKENS section and environment variables as described in the ENVIRONMENT VARIABLES section. Arguments to CertificateFile may use the tilde syntax to refer to a user's home directory, the tokens described in the TOKENS section and environment variables as described in the ENVIRONMENT VARIABLES section.
It is possible to have multiple certificate files specified in configuration files; these certificates will be tried in sequence. Multiple CertificateFile directives will add to the list of certificates used for authentication.`, It is possible to have multiple certificate files specified in configuration files; these certificates will be tried in sequence. Multiple CertificateFile directives will add to the list of certificates used for authentication.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: true, IsOptional: true,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -367,7 +367,7 @@ aes128-gcm@openssh.com,aes256-gcm@openssh.com
DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor, DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor,
RespectQuotes: true, RespectQuotes: true,
SubValue: docvalues.PathValue{ SubValue: docvalues.PathValue{
IsOptional: true, IsOptional: true,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -836,7 +836,7 @@ rsa-sha2-512,rsa-sha2-256
Documentation: `Specifies a path to a library that will be used when loading any FIDO authenticator-hosted keys, overriding the default of using the built-in USB HID support. Documentation: `Specifies a path to a library that will be used when loading any FIDO authenticator-hosted keys, overriding the default of using the built-in USB HID support.
If the specified value begins with a $ character, then it will be treated as an environment variable containing the path to the library.`, If the specified value begins with a $ character, then it will be treated as an environment variable containing the path to the library.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -966,7 +966,7 @@ rsa-sha2-512,rsa-sha2-256
DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor, DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor,
RespectQuotes: true, RespectQuotes: true,
SubValue: docvalues.PathValue{ SubValue: docvalues.PathValue{
IsOptional: true, IsOptional: true,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -990,7 +990,7 @@ rsa-sha2-512,rsa-sha2-256
"xauthlocation": { "xauthlocation": {
Documentation: `Specifies the full pathname of the xauth(1) program. The default is /usr/X11R6/bin/xauth.`, Documentation: `Specifies the full pathname of the xauth(1) program. The default is /usr/X11R6/bin/xauth.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },

View File

@ -155,7 +155,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may
Values: []docvalues.DeprecatedValue{ Values: []docvalues.DeprecatedValue{
docvalues.SingleEnumValue("none"), docvalues.SingleEnumValue("none"),
docvalues.PathValue{ docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -167,7 +167,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may
Values: []docvalues.DeprecatedValue{ Values: []docvalues.DeprecatedValue{
docvalues.SingleEnumValue("none"), docvalues.SingleEnumValue("none"),
docvalues.PathValue{ docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -360,8 +360,8 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may
}, },
"hostcertificate": { "hostcertificate": {
Documentation: `Specifies a file containing a public host certificate. The certificate's public key must match a private host key already specified by HostKey. The default behaviour of sshd(8) is not to load any certificates.`, Documentation: `Specifies a file containing a public host certificate. The certificate's public key must match a private host key already specified by HostKey. The default behaviour of sshd(8) is not to load any certificates.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: true, IsOptional: true,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -370,7 +370,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may
Note that sshd(8) will refuse to use a file if it is group/world-accessible and that the HostKeyAlgorithms option restricts which of the keys are actually used by sshd(8). Note that sshd(8) will refuse to use a file if it is group/world-accessible and that the HostKeyAlgorithms option restricts which of the keys are actually used by sshd(8).
It is possible to have multiple host key files. It is also possible to specify public host key files instead. In this case operations on the private key will be delegated to an ssh-agent(1).`, It is possible to have multiple host key files. It is also possible to specify public host key files instead. In this case operations on the private key will be delegated to an ssh-agent(1).`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: true, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -617,7 +617,7 @@ Only a subset of keywords may be used on the lines following a Match keyword. Av
"modulifile": { "modulifile": {
Documentation: `Specifies the moduli(5) file that contains the Diffie-Hellman groups used for the “diffie-hellman-group-exchange-sha1” and “diffie-hellman-group-exchange-sha256” key exchange methods. The default is /etc/moduli.`, Documentation: `Specifies the moduli(5) file that contains the Diffie-Hellman groups used for the “diffie-hellman-group-exchange-sha1” and “diffie-hellman-group-exchange-sha256” key exchange methods. The default is /etc/moduli.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },
@ -883,7 +883,7 @@ Only a subset of keywords may be used on the lines following a Match keyword. Av
"securitykeyprovider": { "securitykeyprovider": {
Documentation: `Specifies a path to a library that will be used when loading FIDO authenticator-hosted keys, overriding the default of using the built-in USB HID support.`, Documentation: `Specifies a path to a library that will be used when loading FIDO authenticator-hosted keys, overriding the default of using the built-in USB HID support.`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: false, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },

View File

@ -24,7 +24,6 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
entry, matchBlock := d.Config.FindOption(line) entry, matchBlock := d.Config.FindOption(line)
if entry == nil || if entry == nil ||
entry.Separator == nil || entry.Separator == nil ||
entry.Key == nil || entry.Key == nil ||

View File

@ -1,6 +1,8 @@
package utils package utils
import ( import (
"errors"
"fmt"
"os" "os"
) )
@ -13,11 +15,21 @@ func DoesPathExist(path string) bool {
func IsPathDirectory(path string) bool { func IsPathDirectory(path string) bool {
info, err := os.Stat(path) info, err := os.Stat(path)
return err == nil && info.IsDir() if err != nil {
return false
}
return info.IsDir()
} }
func IsPathFile(path string) bool { func IsPathFile(path string) bool {
info, err := os.Stat(path) _, err := os.Stat(path)
return err == nil && !info.IsDir() print(fmt.Sprintf("Checking if path %s is a file: %v\n", path, err))
if errors.Is(err, os.ErrNotExist) {
return false
}
return true
} }