From 429c2cd4be2b18be936099495662044e7153ba01 Mon Sep 17 00:00:00 2001 From: Myzel394 Date: Thu, 29 May 2025 16:16:45 +0200 Subject: [PATCH] fix(server): Improvements Signed-off-by: Myzel394 --- flake.lock | 6 +- server/doc-values/value-path.go | 69 +++++++++++++------ server/handlers/aliases/fields/fields.go | 4 +- server/handlers/fstab/fields/spec.go | 2 +- server/handlers/ssh_config/fields/fields.go | 10 +-- server/handlers/sshd_config/fields/fields.go | 14 ++-- .../lsp/text-document-completion.go | 1 - server/utils/common.go | 18 ++++- 8 files changed, 81 insertions(+), 43 deletions(-) diff --git a/flake.lock b/flake.lock index 3f6093a..35d34f1 100644 --- a/flake.lock +++ b/flake.lock @@ -41,11 +41,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1748026106, - "narHash": "sha256-6m1Y3/4pVw1RWTsrkAK2VMYSzG4MMIj7sqUy7o8th1o=", + "lastModified": 1748370509, + "narHash": "sha256-QlL8slIgc16W5UaI3w7xHQEP+Qmv/6vSNTpoZrrSlbk=", "owner": "nixos", "repo": "nixpkgs", - "rev": "063f43f2dbdef86376cc29ad646c45c46e93234c", + "rev": "4faa5f5321320e49a78ae7848582f684d64783e9", "type": "github" }, "original": { diff --git a/server/doc-values/value-path.go b/server/doc-values/value-path.go index 6160b68..6be9e88 100644 --- a/server/doc-values/value-path.go +++ b/server/doc-values/value-path.go @@ -2,31 +2,21 @@ package docvalues import ( "config-lsp/utils" - protocol "github.com/tliron/glsp/protocol_3_16" + "errors" "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 const ( - PathTypeFile PathType = 1 - PathTypeDirectory PathType = 2 + PathTypeFile PathType = 1 + PathTypeDirectory PathType = 2 ) type PathValue struct { - IsOptional bool + IsOptional bool RequiredType PathType } @@ -53,29 +43,66 @@ func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue { return nil } else { return []*InvalidValue{{ - Err: PathDoesNotExistError{}, + Err: errors.New("This path does not exist"), Start: 0, 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) + // 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) + // directory not expected + } else { + // and should not exist + isValid = isValid && !utils.IsPathDirectory(value) } if isValid { 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{{ - Err: PathInvalidError{}, + Err: errors.New("This path is invalid"), Start: 0, End: uint32(len(value)), }} diff --git a/server/handlers/aliases/fields/fields.go b/server/handlers/aliases/fields/fields.go index 65ae99f..f56db20 100644 --- a/server/handlers/aliases/fields/fields.go +++ b/server/handlers/aliases/fields/fields.go @@ -15,7 +15,7 @@ var UserDeclaration = "`user`" var PathField = docvalues.DocumentationValue{ Documentation: "Append messages to file, specified by its absolute pathname", Value: docvalues.PathValue{ - IsOptional: true, + IsOptional: true, RequiredType: docvalues.PathTypeFile, }, } @@ -41,7 +41,7 @@ var EmailDeclaration = "`user-part@domain-part`" var IncludeField = docvalues.DocumentationValue{ Documentation: "Include any definitions in file as alias entries. The format of the file is identical to this one.", Value: docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, } diff --git a/server/handlers/fstab/fields/spec.go b/server/handlers/fstab/fields/spec.go index 133fbc3..9a20997 100644 --- a/server/handlers/fstab/fields/spec.go +++ b/server/handlers/fstab/fields/spec.go @@ -16,7 +16,7 @@ var LabelField = docvalues.RegexValue{ var SpecField = docvalues.OrValue{ Values: []docvalues.DeprecatedValue{ docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, docvalues.KeyEnumAssignmentValue{ diff --git a/server/handlers/ssh_config/fields/fields.go b/server/handlers/ssh_config/fields/fields.go index 900fad1..eff98c2 100644 --- a/server/handlers/ssh_config/fields/fields.go +++ b/server/handlers/ssh_config/fields/fields.go @@ -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. 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{ - IsOptional: true, + IsOptional: true, RequiredType: docvalues.PathTypeFile, }, }, @@ -367,7 +367,7 @@ aes128-gcm@openssh.com,aes256-gcm@openssh.com DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor, RespectQuotes: true, SubValue: docvalues.PathValue{ - IsOptional: true, + IsOptional: true, 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. 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{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, @@ -966,7 +966,7 @@ rsa-sha2-512,rsa-sha2-256 DuplicatesExtractor: &docvalues.SimpleDuplicatesExtractor, RespectQuotes: true, SubValue: docvalues.PathValue{ - IsOptional: true, + IsOptional: true, RequiredType: docvalues.PathTypeFile, }, }, @@ -990,7 +990,7 @@ rsa-sha2-512,rsa-sha2-256 "xauthlocation": { Documentation: `Specifies the full pathname of the xauth(1) program. The default is /usr/X11R6/bin/xauth.`, Value: docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, diff --git a/server/handlers/sshd_config/fields/fields.go b/server/handlers/sshd_config/fields/fields.go index e3f19bb..d6d5f40 100644 --- a/server/handlers/sshd_config/fields/fields.go +++ b/server/handlers/sshd_config/fields/fields.go @@ -155,7 +155,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may Values: []docvalues.DeprecatedValue{ docvalues.SingleEnumValue("none"), docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, @@ -167,7 +167,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may Values: []docvalues.DeprecatedValue{ docvalues.SingleEnumValue("none"), docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, @@ -360,8 +360,8 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may }, "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.`, - Value: docvalues.PathValue{ - IsOptional: true, + Value: docvalues.PathValue{ + IsOptional: true, 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). 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{ - IsOptional: true, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, @@ -617,7 +617,7 @@ Only a subset of keywords may be used on the lines following a Match keyword. Av "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.`, Value: docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, @@ -883,7 +883,7 @@ Only a subset of keywords may be used on the lines following a Match keyword. Av "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.`, Value: docvalues.PathValue{ - IsOptional: false, + IsOptional: false, RequiredType: docvalues.PathTypeFile, }, }, diff --git a/server/handlers/sshd_config/lsp/text-document-completion.go b/server/handlers/sshd_config/lsp/text-document-completion.go index b4ead50..72a9a38 100644 --- a/server/handlers/sshd_config/lsp/text-document-completion.go +++ b/server/handlers/sshd_config/lsp/text-document-completion.go @@ -24,7 +24,6 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa entry, matchBlock := d.Config.FindOption(line) - if entry == nil || entry.Separator == nil || entry.Key == nil || diff --git a/server/utils/common.go b/server/utils/common.go index ba45c9c..e676ba0 100644 --- a/server/utils/common.go +++ b/server/utils/common.go @@ -1,6 +1,8 @@ package utils import ( + "errors" + "fmt" "os" ) @@ -13,11 +15,21 @@ func DoesPathExist(path string) bool { func IsPathDirectory(path string) bool { info, err := os.Stat(path) - return err == nil && info.IsDir() + if err != nil { + return false + } + + return info.IsDir() } 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 }