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

Add Windows support #20

Merged
merged 4 commits into from
Feb 6, 2025
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
24 changes: 17 additions & 7 deletions pkg/netrc/netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/GoogleCloudPlatform/artifact-registry-go-tools/pkg/auth"
Expand All @@ -43,6 +44,14 @@ password %s
`, host, base64key)
}

func getNetrcFileName() string {
// return _netrc for Windows and .netrc for Unix
if runtime.GOOS == "windows" {
return "_netrc"
}
return ".netrc"
}

// Load loads the path and contents of the .netrc file into memory.
func Load() (string, string, error) {
netrcPath := os.Getenv("NETRC")
Expand All @@ -54,15 +63,16 @@ func Load() (string, string, error) {
netrcPath = h
}

if !strings.HasSuffix(netrcPath, ".netrc") {
netrcPath = path.Join(netrcPath, ".netrc")
netrcFileName := getNetrcFileName()
if !strings.HasSuffix(netrcPath, netrcFileName) {
netrcPath = filepath.Join(netrcPath, netrcFileName)
}

if _, err := os.Stat(path.Dir(netrcPath)); err != nil {
if _, err := os.Stat(filepath.Dir(netrcPath)); err != nil {
if os.IsNotExist(err) {
return "", "", fmt.Errorf(".netrc directory does not exist: %w", err)
return "", "", fmt.Errorf("%s directory does not exist: %w", netrcFileName, err)
}
return "", "", fmt.Errorf("failed to load .netrc directory: %w", err)
return "", "", fmt.Errorf("failed to load %s directory: %w", netrcFileName, err)
}

data, err := ioutil.ReadFile(netrcPath)
Expand All @@ -71,7 +81,7 @@ func Load() (string, string, error) {
return netrcPath, "", nil
}
if err != nil {
return "", "", fmt.Errorf("cannot load .netrc file: %v", err)
return "", "", fmt.Errorf("cannot load %s file: %v", netrcFileName, err)
}
return netrcPath, string(data), nil
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/netrc/netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
_ "embed"
"errors"
"os"
"path"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ password <oauth2accesstoken>
jsonKeyPath: "testdata/key.json",
wantNetrc: `machine us-west1-go.pkg.dev
login _json_key_base64
password ewogICAgInRlc3Qta2V5IjogInRlc3QtdmFsdWUiCn0=
password eyJ0ZXN0LWtleSI6ICJ0ZXN0LXZhbHVlIn0=
`,
},
{
Expand Down Expand Up @@ -236,7 +236,10 @@ func TestLoad(t *testing.T) {
if err != nil {
t.Fatalf("os.Getwd() = %v", err)
}
testDataDir := path.Join(currentDir, "testdata", "load_test")

netrcFileName := getNetrcFileName()

testDataDir := filepath.Join(currentDir, "testdata", "load_test")

cases := []struct {
name string
Expand All @@ -247,29 +250,29 @@ func TestLoad(t *testing.T) {
}{
{
name: "NETRC env points to existing directory",
netrcPath: path.Join(testDataDir, "empty_dir"),
wantNetrcPath: path.Join(testDataDir, "empty_dir", ".netrc"),
netrcPath: filepath.Join(testDataDir, "empty_dir"),
wantNetrcPath: filepath.Join(testDataDir, "empty_dir", netrcFileName),
wantNetrcContent: "",
wantErr: nil,
},
{
name: "NETRC env points to non-existent directory",
netrcPath: path.Join(testDataDir, "non_existent_dir"),
netrcPath: filepath.Join(testDataDir, "non_existent_dir"),
wantNetrcPath: "",
wantNetrcContent: "",
wantErr: os.ErrNotExist,
},
{
name: "NETRC env points to existing file",
netrcPath: path.Join(testDataDir, "has_netrc_file_dir", ".netrc"),
wantNetrcPath: path.Join(testDataDir, "has_netrc_file_dir", ".netrc"),
netrcPath: filepath.Join(testDataDir, "has_netrc_file_dir", netrcFileName),
wantNetrcPath: filepath.Join(testDataDir, "has_netrc_file_dir", netrcFileName),
wantNetrcContent: hasNetrcFileDirNetrcContent,
wantErr: nil,
},
{
name: "NETRC env points to non-existent file",
netrcPath: path.Join(testDataDir, "empty_dir", ".netrc"),
wantNetrcPath: path.Join(testDataDir, "empty_dir", ".netrc"),
netrcPath: filepath.Join(testDataDir, "empty_dir", netrcFileName),
wantNetrcPath: filepath.Join(testDataDir, "empty_dir", netrcFileName),
wantNetrcContent: "",
wantErr: nil,
},
Expand Down
4 changes: 1 addition & 3 deletions pkg/netrc/testdata/key.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"test-key": "test-value"
}
{"test-key": "test-value"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we can revert this change? This is unrelated to the fix and the original key.json wasn't incorrectly formatted either.

Copy link
Contributor Author

@cagataygurturk cagataygurturk Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually related to the fix. Without this change, tests in Windows are not passing due to new line differences. It was a quick fix to mitigate that. Should I revert and leave the tests broken in Windows or leave like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain more? I thought this file was only used in the add the first location with json key test case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see what you mean. Is it because \n and \r\n leading to different base64 encoding of json key? If so, this change LGTM

Copy link
Contributor Author

@cagataygurturk cagataygurturk Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! It took me quite some time to understand why tests were passing on Mac/Linux and not on Windows, then I figured out that was the reason. And I just got rid of newlines since they are not too related to what we are trying to test.

3 changes: 3 additions & 0 deletions pkg/netrc/testdata/load_test/has_netrc_file_dir/_netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
machine example.com
login username
password password