Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix windows path in aztfmigrate #113

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions internal/langserver/handlers/command/aztfmigrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -66,13 +67,7 @@ func (c AztfMigrateCommand) Handle(ctx context.Context, arguments []json.RawMess
}

// creating temp workspace
workingDirectory := path.Dir(strings.TrimPrefix(string(params.TextDocument.URI), "file://"))
if runtime.GOOS == "windows" {
workingDirectory = strings.ReplaceAll(workingDirectory, "%3A", ":")
workingDirectory = strings.ReplaceAll(workingDirectory, "/", "\\")
workingDirectory = strings.TrimPrefix(workingDirectory, "\\")
}
log.Printf("[INFO] working directory: %s", workingDirectory)
workingDirectory := getWorkingDirectory(string(params.TextDocument.URI), runtime.GOOS)
tempDir := filepath.Join(workingDirectory, tempFolderName)
if err := os.MkdirAll(tempDir, 0750); err != nil {
return nil, fmt.Errorf("creating temp workspace %q: %+v", tempDir, err)
Expand Down Expand Up @@ -416,3 +411,13 @@ func reportProgress(ctx context.Context, message string, percentage uint32) {
})
}
}

func getWorkingDirectory(uri string, os string) string {
workingDirectory := path.Dir(strings.TrimPrefix(uri, "file://"))
if os == "windows" {
workingDirectory, _ = url.QueryUnescape(workingDirectory)
workingDirectory = strings.ReplaceAll(workingDirectory, "/", "\\")
workingDirectory = strings.TrimPrefix(workingDirectory, "\\")
}
return workingDirectory
}
49 changes: 49 additions & 0 deletions internal/langserver/handlers/command/aztfmigrate_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package command

import (
"testing"
)

func Test_GetWorkingDirectory(t *testing.T) {
tests := []struct {
name string
os string
input string
want string
}{
{
name: "basic on darwin",
os: "darwin",
input: "file:///Users/username/go/src/github.com/Azure/azapi-lsp/main.tf",
want: "/Users/username/go/src/github.com/Azure/azapi-lsp",
},

{
name: "basic on windows",
os: "windows",
input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/main.tf",
want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp",
},

{
name: "path with spaces on windows",
os: "windows",
input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/terraform%20files/main.tf",
want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp\\terraform files",
},

{
name: "path with Chinese characters on windows",
os: "windows",
input: "file:///c%3A/Users/username/go/src/github.com/Azure/azapi-lsp/%E4%B8%AD%E6%96%87/main.tf",
want: "c:\\Users\\username\\go\\src\\github.com\\Azure\\azapi-lsp\\中文",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getWorkingDirectory(tt.input, tt.os); got != tt.want {
t.Errorf("getWorkingDirectory() = %v, want %v", got, tt.want)
}
})
}
}
Loading