github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/lsp/util.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package lsp
    18  
    19  import (
    20  	"context"
    21  	"net/url"
    22  	"strings"
    23  
    24  	"go.lsp.dev/uri"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
    27  )
    28  
    29  const gitPrefix = "git:/"
    30  
    31  func uriToFilename(v uri.URI) string {
    32  	s := string(v)
    33  	fixed, ok := fixURI(s)
    34  
    35  	if !ok {
    36  		unescaped, err := url.PathUnescape(s)
    37  		if err != nil {
    38  			log.Entry(context.TODO()).Warnf("Unsupported URI (not a filepath): %q\n", s)
    39  		} else {
    40  			log.Entry(context.TODO()).Warnf("Unsupported URI (not a filepath): %q\n", unescaped)
    41  		}
    42  		return ""
    43  	}
    44  	v = uri.URI(fixed)
    45  
    46  	return v.Filename()
    47  }
    48  
    49  // workaround for unsupported file paths (git + invalid file://-prefix )
    50  func fixURI(s string) (string, bool) {
    51  	if strings.HasPrefix(s, gitPrefix) {
    52  		return "file:///" + s[len(gitPrefix):], true
    53  	}
    54  	if !strings.HasPrefix(s, "file:///") {
    55  		// VS Code sends URLs with only two slashes, which are invalid. golang/go#39789.
    56  		if strings.HasPrefix(s, "file://") {
    57  			return "file:///" + s[len("file://"):], true
    58  		}
    59  		return "", false
    60  	}
    61  	return s, true
    62  }