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 builtin netrc helper #82

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
132 changes: 132 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
@@ -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"},
},
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ 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=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
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=
127 changes: 2 additions & 125 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"log"
"os"
"time"

"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
Expand All @@ -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)
Expand Down
77 changes: 77 additions & 0 deletions netrc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"

"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",
6543 marked this conversation as resolved.
Show resolved Hide resolved
Flags: []cli.Flag{&cli.StringFlag{
Name: "home",
Usage: "Change home directory",
EnvVars: []string{"PLUGIN_HOME"},
}},
Comment on lines +17 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this already added because you have the app.Flags = globalFlags statement which adds the flags globally?

Copy link
Member Author

Choose a reason for hiding this comment

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

need to test ... but as it's mostly used only internally I dont think we do need it anyway ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Tested this, working as I expected.

You can try with this:

package main

import (
	"fmt"
	"os"

	"github.com/urfave/cli/v2"
)

func main() {
	app := cli.NewApp()
	app.Flags = []cli.Flag{
		&cli.StringFlag{
			Name: "testflag",
		},
	}
	app.Commands = []*cli.Command{
		&cli.Command{
			Name: "cmd",
			Action: func(ctx *cli.Context) error {
				fmt.Println(ctx.String("testflag"))
				return nil
			},
		},
	}
	fmt.Println(app.Run(os.Args))
}

Copy link
Contributor

Choose a reason for hiding this comment

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

That means just apply this:

Suggested change
Flags: []cli.Flag{&cli.StringFlag{
Name: "home",
Usage: "Change home directory",
EnvVars: []string{"PLUGIN_HOME"},
}},

Copy link
Contributor

Choose a reason for hiding this comment

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

@6543 @qwerty287 What is the status here?

Copy link
Member Author

Choose a reason for hiding this comment

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

as per lable > wip till i personally do dedicate time to it or it gets priorised at work ...

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"+
6543 marked this conversation as resolved.
Show resolved Hide resolved
"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
}

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...)
}
Loading