From bda5f228002195f6f4f9c1d8814dd56e96ec7f1b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 24 Aug 2023 18:19:30 +0200 Subject: [PATCH 1/6] draft --- flags.go | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 127 +---------------------------------------------- netrc.go | 63 +++++++++++++++++++++++ netrc/netrc.go | 122 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+), 125 deletions(-) create mode 100644 flags.go create mode 100644 netrc.go create mode 100644 netrc/netrc.go diff --git a/flags.go b/flags.go new file mode 100644 index 0000000..af3e261 --- /dev/null +++ b/flags.go @@ -0,0 +1,132 @@ +package main + +import ( + "time" + + "github.com/urfave/cli/v2" +) + +var globalFlags = []cli.Flag{ + &cli.StringFlag{ + Name: "remote", + Usage: "git remote url", + EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"}, + }, + &cli.StringFlag{ + Name: "path", + Usage: "git clone path", + EnvVars: []string{"PLUGIN_PATH", "CI_WORKSPACE"}, + }, + &cli.StringFlag{ + Name: "sha", + Usage: "git commit sha", + EnvVars: []string{"PLUGIN_SHA", "CI_COMMIT_SHA"}, + }, + &cli.StringFlag{ + Name: "ref", + Value: "refs/heads/master", + Usage: "git commit ref", + EnvVars: []string{"PLUGIN_REF", "CI_COMMIT_REF"}, + }, + &cli.StringFlag{ + Name: "event", + Value: "push", + Usage: "pipeline event", + EnvVars: []string{"CI_PIPELINE_EVENT"}, + }, + &cli.StringFlag{ + Name: "netrc.machine", + Usage: "netrc machine", + EnvVars: []string{"CI_NETRC_MACHINE"}, + }, + &cli.StringFlag{ + Name: "netrc.username", + Usage: "netrc username", + EnvVars: []string{"CI_NETRC_USERNAME"}, + }, + &cli.StringFlag{ + Name: "netrc.password", + Usage: "netrc password", + EnvVars: []string{"CI_NETRC_PASSWORD"}, + }, + &cli.IntFlag{ + Name: "depth", + Usage: "clone depth", + EnvVars: []string{"PLUGIN_DEPTH"}, + }, + &cli.BoolFlag{ + Name: "recursive", + Usage: "clone submodules", + EnvVars: []string{"PLUGIN_RECURSIVE"}, + Value: true, + }, + &cli.BoolFlag{ + Name: "tags", + Usage: "clone tags, if not explicitly set and event is tag its default is true else false", + EnvVars: []string{"PLUGIN_TAGS"}, + }, + &cli.BoolFlag{ + Name: "skip-verify", + Usage: "skip tls verification", + EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, + }, + &cli.StringFlag{ + Name: "custom-cert", + Usage: "path or url to custom cert", + EnvVars: []string{"PLUGIN_CUSTOM_SSL_PATH", "PLUGIN_CUSTOM_SSL_URL"}, + }, + &cli.BoolFlag{ + Name: "submodule-update-remote", + Usage: "update remote submodules", + EnvVars: []string{"PLUGIN_SUBMODULES_UPDATE_REMOTE", "PLUGIN_SUBMODULE_UPDATE_REMOTE"}, + }, + &cli.GenericFlag{ + Name: "submodule-override", + Usage: "json map of submodule overrides", + EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"}, + Value: &MapFlag{}, + }, + &cli.DurationFlag{ + Name: "backoff", + Usage: "backoff duration", + EnvVars: []string{"PLUGIN_BACKOFF"}, + Value: 5 * time.Second, + }, + &cli.IntFlag{ + Name: "backoff-attempts", + Usage: "backoff attempts", + EnvVars: []string{"PLUGIN_ATTEMPTS"}, + Value: 5, + }, + &cli.BoolFlag{ + Name: "lfs", + Usage: "whether to retrieve LFS content if available", + EnvVars: []string{"PLUGIN_LFS"}, + Value: true, + }, + &cli.StringFlag{ + Name: "env-file", + Usage: "source env file", + }, + &cli.StringFlag{ + Name: "branch", + Usage: "Change branch name", + EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"}, + }, + &cli.BoolFlag{ + Name: "partial", + Usage: "Enable/Disable Partial clone", + EnvVars: []string{"PLUGIN_PARTIAL"}, + Value: true, + }, + &cli.StringFlag{ + Name: "home", + Usage: "Change home directory", + EnvVars: []string{"PLUGIN_HOME"}, + }, + &cli.StringFlag{ + Name: "safe-directory", + Usage: "Define safe directories", + EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"}, + }, +} diff --git a/main.go b/main.go index ee979ff..fe790a5 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "log" "os" - "time" "github.com/joho/godotenv" "github.com/urfave/cli/v2" @@ -17,130 +16,8 @@ func main() { app.Usage = "git plugin" app.Action = run app.Version = version - app.Flags = []cli.Flag{ - &cli.StringFlag{ - Name: "remote", - Usage: "git remote url", - EnvVars: []string{"PLUGIN_REMOTE", "CI_REPO_CLONE_URL"}, - }, - &cli.StringFlag{ - Name: "path", - Usage: "git clone path", - EnvVars: []string{"PLUGIN_PATH", "CI_WORKSPACE"}, - }, - &cli.StringFlag{ - Name: "sha", - Usage: "git commit sha", - EnvVars: []string{"PLUGIN_SHA", "CI_COMMIT_SHA"}, - }, - &cli.StringFlag{ - Name: "ref", - Value: "refs/heads/master", - Usage: "git commit ref", - EnvVars: []string{"PLUGIN_REF", "CI_COMMIT_REF"}, - }, - &cli.StringFlag{ - Name: "event", - Value: "push", - Usage: "pipeline event", - EnvVars: []string{"CI_PIPELINE_EVENT"}, - }, - &cli.StringFlag{ - Name: "netrc.machine", - Usage: "netrc machine", - EnvVars: []string{"CI_NETRC_MACHINE"}, - }, - &cli.StringFlag{ - Name: "netrc.username", - Usage: "netrc username", - EnvVars: []string{"CI_NETRC_USERNAME"}, - }, - &cli.StringFlag{ - Name: "netrc.password", - Usage: "netrc password", - EnvVars: []string{"CI_NETRC_PASSWORD"}, - }, - &cli.IntFlag{ - Name: "depth", - Usage: "clone depth", - EnvVars: []string{"PLUGIN_DEPTH"}, - }, - &cli.BoolFlag{ - Name: "recursive", - Usage: "clone submodules", - EnvVars: []string{"PLUGIN_RECURSIVE"}, - Value: true, - }, - &cli.BoolFlag{ - Name: "tags", - Usage: "clone tags, if not explicitly set and event is tag its default is true else false", - EnvVars: []string{"PLUGIN_TAGS"}, - }, - &cli.BoolFlag{ - Name: "skip-verify", - Usage: "skip tls verification", - EnvVars: []string{"PLUGIN_SKIP_VERIFY"}, - }, - &cli.StringFlag{ - Name: "custom-cert", - Usage: "path or url to custom cert", - EnvVars: []string{"PLUGIN_CUSTOM_SSL_PATH", "PLUGIN_CUSTOM_SSL_URL"}, - }, - &cli.BoolFlag{ - Name: "submodule-update-remote", - Usage: "update remote submodules", - EnvVars: []string{"PLUGIN_SUBMODULES_UPDATE_REMOTE", "PLUGIN_SUBMODULE_UPDATE_REMOTE"}, - }, - &cli.GenericFlag{ - Name: "submodule-override", - Usage: "json map of submodule overrides", - EnvVars: []string{"PLUGIN_SUBMODULE_OVERRIDE"}, - Value: &MapFlag{}, - }, - &cli.DurationFlag{ - Name: "backoff", - Usage: "backoff duration", - EnvVars: []string{"PLUGIN_BACKOFF"}, - Value: 5 * time.Second, - }, - &cli.IntFlag{ - Name: "backoff-attempts", - Usage: "backoff attempts", - EnvVars: []string{"PLUGIN_ATTEMPTS"}, - Value: 5, - }, - &cli.BoolFlag{ - Name: "lfs", - Usage: "whether to retrieve LFS content if available", - EnvVars: []string{"PLUGIN_LFS"}, - Value: true, - }, - &cli.StringFlag{ - Name: "env-file", - Usage: "source env file", - }, - &cli.StringFlag{ - Name: "branch", - Usage: "Change branch name", - EnvVars: []string{"PLUGIN_BRANCH", "CI_COMMIT_BRANCH", "CI_REPO_DEFAULT_BRANCH"}, - }, - &cli.BoolFlag{ - Name: "partial", - Usage: "Enable/Disable Partial clone", - EnvVars: []string{"PLUGIN_PARTIAL"}, - Value: true, - }, - &cli.StringFlag{ - Name: "home", - Usage: "Change home directory", - EnvVars: []string{"PLUGIN_HOME"}, - }, - &cli.StringFlag{ - Name: "safe-directory", - Usage: "Define safe directories", - EnvVars: []string{"PLUGIN_SAFE_DIRECTORY", "CI_WORKSPACE"}, - }, - } + app.Commands = []*cli.Command{netrcCommand} + app.Flags = globalFlags if err := app.Run(os.Args); err != nil { log.Fatal(err) diff --git a/netrc.go b/netrc.go new file mode 100644 index 0000000..31ce47a --- /dev/null +++ b/netrc.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "os" + + "github.com/urfave/cli/v2" + "github.com/woodpecker-ci/plugin-git/netrc" +) + +var netrcCommand = &cli.Command{ + Name: "netrc", + Usage: "built in credentials helper to read netrc", + Flags: []cli.Flag{&cli.StringFlag{ + Name: "home", + Usage: "Change home directory", + EnvVars: []string{"PLUGIN_HOME"}, + }}, + Action: netrcGet, +} + +func netrcGet(c *cli.Context) error { + if c.Args().Len() == 0 { + curExec, err := os.Executable() + if err != nil { + return err + } + fmt.Printf("built in credentials helper to read netrc\n"+ + "exec \"git config --global credential.helper '%s netrc'\" to use it\n", curExec) + return nil + } + + // set custom home + if c.IsSet("home") { + os.Setenv("HOME", c.String("home")) + } + + // implement custom git credentials helper + // https://git-scm.com/docs/gitcredentials + switch c.Args().First() { + case "get": + netRC, err := netrc.Read() + if err != nil { + return err + } + if netRC != nil { + fmt.Printf("username=%s\n", netRC.Login) + fmt.Printf("password=%s\n", netRC.Password) + fmt.Println("quit=true") + } + case "store": + // TODO: netrc.Save() + case "erase": + _, err := netrc.Delete() + if err != nil { + return err + } + default: + return fmt.Errorf("got unknown helper arg '%s'", c.Args().First()) + } + + return nil +} diff --git a/netrc/netrc.go b/netrc/netrc.go new file mode 100644 index 0000000..b9ea097 --- /dev/null +++ b/netrc/netrc.go @@ -0,0 +1,122 @@ +package netrc + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "strings" +) + +type NetRC struct { + Machine, + Login, + Password string +} + +func getFilePath() (string, error) { + // get home + homeDir := getHomeDir() + + // calc netrc file path + file := filepath.Join(homeDir, ".netrc") + if runtime.GOOS == "windows" { + file = filepath.Join(homeDir, "_netrc") + } + + stats, err := os.Stat(file) + if err != nil { + return "", nil + } + if !stats.Mode().IsRegular() { + return "", fmt.Errorf("'%s' exist but is a %s", file, stats.Mode().Type().String()) + } + return file, nil +} + +// Delete delete the netrc if file exist +func Delete() (bool, error) { + file, err := getFilePath() + if err != nil || file == "" { + return false, err + } + + return true, os.Remove(file) +} + +// Save save a netrc +func Save(n *NetRC) error { + if n == nil { + return nil + } + + // get home + homeDir := getHomeDir() + + // calc netrc file path + file := filepath.Join(homeDir, ".netrc") + if runtime.GOOS == "windows" { + file = filepath.Join(homeDir, "_netrc") + } + + content := fmt.Sprintf(` +machine %s +login %s +password %s +`, + n.Machine, + n.Login, + n.Password, + ) + + return os.WriteFile(file, []byte(content), 0o600) +} + +// Read return netrc if file or env var exist +func Read() (*NetRC, error) { + // try to read from env var + netRC := &NetRC{ + Machine: os.Getenv("CI_NETRC_MACHINE"), + Login: os.Getenv("CI_NETRC_USERNAME"), + Password: os.Getenv("CI_NETRC_PASSWORD"), + } + // if we get at least user and password from env we can return + if netRC.Login != "" && netRC.Password != "" { + return netRC, nil + } + + file, err := getFilePath() + if err != nil || file == "" { + return nil, err + } + raw, err := os.ReadFile(file) + if err != nil { + return nil, fmt.Errorf("error while reading file '%s': %w", file, err) + } + + for _, v := range strings.Split(string(raw), "\n") { + v = strings.TrimSpace(v) + if strings.HasPrefix(v, "machine") { + netRC.Machine = strings.TrimSpace(strings.TrimPrefix(v, "machine")) + } + if strings.HasPrefix(v, "login") { + netRC.Login = strings.TrimSpace(strings.TrimPrefix(v, "login")) + } + if strings.HasPrefix(v, "password") { + netRC.Password = strings.TrimSpace(strings.TrimPrefix(v, "password")) + } + } + + return nil, nil +} + +func getHomeDir() string { + if homeDir := os.Getenv("HOME"); homeDir != "" { + return homeDir + } + if homeDir, _ := os.UserHomeDir(); homeDir != "" { + return homeDir + } + pwd, _ := os.Getwd() + return pwd +} From d4dbc409cdd2ad28bc4d99af81e37d0c00e1e247 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 24 Aug 2023 19:46:24 +0200 Subject: [PATCH 2/6] parsin into own func --- netrc.go | 1 + netrc/netrc.go | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/netrc.go b/netrc.go index 31ce47a..9ac6179 100644 --- a/netrc.go +++ b/netrc.go @@ -5,6 +5,7 @@ import ( "os" "github.com/urfave/cli/v2" + "github.com/woodpecker-ci/plugin-git/netrc" ) diff --git a/netrc/netrc.go b/netrc/netrc.go index b9ea097..fb13913 100644 --- a/netrc/netrc.go +++ b/netrc/netrc.go @@ -94,7 +94,23 @@ func Read() (*NetRC, error) { return nil, fmt.Errorf("error while reading file '%s': %w", file, err) } - for _, v := range strings.Split(string(raw), "\n") { + return parseNetRC(string(raw)) +} + +func getHomeDir() string { + if homeDir := os.Getenv("HOME"); homeDir != "" { + return homeDir + } + if homeDir, _ := os.UserHomeDir(); homeDir != "" { + return homeDir + } + pwd, _ := os.Getwd() + return pwd +} + +func parseNetRC(raw string) (*NetRC, error) { + netRC := &NetRC{} + for _, v := range strings.Split(raw, "\n") { v = strings.TrimSpace(v) if strings.HasPrefix(v, "machine") { netRC.Machine = strings.TrimSpace(strings.TrimPrefix(v, "machine")) @@ -107,16 +123,9 @@ func Read() (*NetRC, error) { } } - return nil, nil -} - -func getHomeDir() string { - if homeDir := os.Getenv("HOME"); homeDir != "" { - return homeDir + if netRC.Login == "" && netRC.Password == "" { + return nil, fmt.Errorf("parsing netrc failed, got empty result") } - if homeDir, _ := os.UserHomeDir(); homeDir != "" { - return homeDir - } - pwd, _ := os.Getwd() - return pwd + + return netRC, nil } From 42852ff400f17d6370e365893d8ef5e11e903fc5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 24 Aug 2023 19:51:35 +0200 Subject: [PATCH 3/6] start with tests --- go.mod | 4 ++++ go.sum | 8 ++++++-- netrc/netrc_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 netrc/netrc_test.go diff --git a/go.mod b/go.mod index 171d265..d50225b 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,16 @@ go 1.20 require ( github.com/adrg/xdg v0.4.0 github.com/joho/godotenv v1.5.1 + github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect golang.org/x/sys v0.11.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5093134..f43b109 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,9 @@ github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls= github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E= github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -11,8 +12,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= @@ -20,6 +22,8 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsr golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/netrc/netrc_test.go b/netrc/netrc_test.go new file mode 100644 index 0000000..22f298c --- /dev/null +++ b/netrc/netrc_test.go @@ -0,0 +1,26 @@ +package netrc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseNetRC(t *testing.T) { + n, err := parseNetRC(``) + assert.Error(t, err) + assert.Nil(t, n) + + n, err = parseNetRC(`machine example.org`) + assert.Error(t, err) + assert.Nil(t, n) + + n, err = parseNetRC(` +machine test.com +password someTestPWD + +login awesomeUser +`) + assert.NoError(t, err) + assert.EqualValues(t, &NetRC{Machine: "test.com", Login: "awesomeUser", Password: "someTestPWD"}, n) +} From 4b262e65e538e864794eae27ddbd18f80b4c732f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 24 Aug 2023 19:59:22 +0200 Subject: [PATCH 4/6] use build-in netrc helper on windows by default --- netrc.go | 13 +++++++++++++ plugin.go | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/netrc.go b/netrc.go index 9ac6179..5453070 100644 --- a/netrc.go +++ b/netrc.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "log" "os" + "os/exec" "github.com/urfave/cli/v2" @@ -62,3 +64,14 @@ func netrcGet(c *cli.Context) error { return nil } + +func setNetRCHelper() *exec.Cmd { + curExec, err := os.Executable() + if err != nil { + log.Fatal(err) + } + + credHelper := fmt.Sprintf("%s netrc", curExec) + + return appendEnv(exec.Command("git", "config", "--global", "credential.helper", credHelper), defaultEnvVars...) +} diff --git a/plugin.go b/plugin.go index e0656ba..2f91796 100644 --- a/plugin.go +++ b/plugin.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "time" ) @@ -51,6 +52,11 @@ func (p Plugin) Exec() error { var cmds []*exec.Cmd + // windows don't understand netrc so we use our build-in helper + if runtime.GOOS == "windows" { + cmds = append(cmds, setNetRCHelper()) + } + if p.Config.SkipVerify { cmds = append(cmds, skipVerify()) } else if p.Config.CustomCert != "" { From 5a608d1cad3b23f113291a22956dd295efd1c921 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 25 Aug 2023 16:17:48 +0200 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com> --- netrc.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netrc.go b/netrc.go index 5453070..3cb7bcf 100644 --- a/netrc.go +++ b/netrc.go @@ -13,7 +13,7 @@ import ( var netrcCommand = &cli.Command{ Name: "netrc", - Usage: "built in credentials helper to read netrc", + Usage: "built-in credentials helper to read netrc", Flags: []cli.Flag{&cli.StringFlag{ Name: "home", Usage: "Change home directory", @@ -28,7 +28,7 @@ func netrcGet(c *cli.Context) error { if err != nil { return err } - fmt.Printf("built in credentials helper to read netrc\n"+ + fmt.Printf("built-in credentials helper to read netrc\n"+ "exec \"git config --global credential.helper '%s netrc'\" to use it\n", curExec) return nil } From a622645df1d3e225c230d70939beea0eb90ce373 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 26 Aug 2023 03:24:23 +0200 Subject: [PATCH 6/6] Update plugin.go Co-authored-by: Anbraten --- plugin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.go b/plugin.go index 2f91796..382b280 100644 --- a/plugin.go +++ b/plugin.go @@ -52,7 +52,7 @@ func (p Plugin) Exec() error { var cmds []*exec.Cmd - // windows don't understand netrc so we use our build-in helper + // windows doesn't understand netrc, so we use our build-in helper if runtime.GOOS == "windows" { cmds = append(cmds, setNetRCHelper()) }