diff --git a/cmd/cli.go b/cmd/cli.go index 6eb3123..6019f47 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -6,9 +6,9 @@ import ( "strconv" "time" - mapper "github.com/datahearth/config-mapper/internal" - "github.com/datahearth/config-mapper/internal/configuration" - "github.com/datahearth/config-mapper/internal/git" + mapper "gitea.antoine-langlois.net/datahearth/config-mapper/internal" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/git" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" diff --git a/go.mod b/go.mod index a385945..db3bde5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/datahearth/config-mapper +module gitea.antoine-langlois.net/datahearth/config-mapper go 1.17 @@ -6,6 +6,7 @@ require ( github.com/fatih/color v1.13.0 github.com/gernest/wow v0.1.0 github.com/go-git/go-git/v5 v5.4.2 + github.com/mitchellh/mapstructure v1.5.0 github.com/spf13/cobra v1.3.0 github.com/spf13/viper v1.10.1 golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 @@ -28,7 +29,6 @@ require ( github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/pelletier/go-toml v1.9.4 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/spf13/afero v1.6.0 // indirect diff --git a/go.sum b/go.sum index 50c8e50..78b09f1 100644 --- a/go.sum +++ b/go.sum @@ -313,8 +313,9 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= diff --git a/internal/configuration/definition.go b/internal/configuration/definition.go index 62b5fb5..1c744ff 100644 --- a/internal/configuration/definition.go +++ b/internal/configuration/definition.go @@ -22,7 +22,7 @@ type Git struct { Name string `mapstructure:"name" yaml:"name"` Email string `mapstructure:"email" yaml:"email"` BasicAuth BasicAuth `mapstructure:"basic-auth" yaml:"basic-auth"` - SSH Ssh `mapstructure:"ssh" yaml:"ssh"` + SSH interface{} `mapstructure:"ssh" yaml:"ssh"` } type BasicAuth struct { diff --git a/internal/configuration/ssh.go b/internal/configuration/ssh.go index 7e10deb..0de9902 100644 --- a/internal/configuration/ssh.go +++ b/internal/configuration/ssh.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "os" osUser "os/user" "strings" @@ -156,7 +155,7 @@ func getUriContent(uri string) (string, string, error) { func createPubKeyAuth(key string) (ssh.AuthMethod, error) { var signer ssh.Signer - privateKey, err := ioutil.ReadFile(key) + privateKey, err := os.ReadFile(key) if err != nil { return nil, err } diff --git a/internal/git/git.go b/internal/git/git.go index a662649..5ce7e57 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -2,16 +2,18 @@ package git import ( "errors" + "fmt" "os" "time" - "github.com/datahearth/config-mapper/internal/configuration" - "github.com/datahearth/config-mapper/internal/misc" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/ssh" + "github.com/mitchellh/mapstructure" ) var ( @@ -48,25 +50,45 @@ func NewRepository(config configuration.Git, repoPath string) (RepositoryActions return nil, err } - if config.SSH.Passphrase != "" && config.SSH.PrivateKey != "" { - privateKey, err := misc.AbsolutePath(config.SSH.PrivateKey) - if err != nil { + switch sshConfig := config.SSH.(type) { + case map[string]interface{}: + var outConfig configuration.Ssh + if err := mapstructure.Decode(sshConfig, &outConfig); err != nil { return nil, err } - if _, err := os.Stat(privateKey); err != nil { - return nil, err - } - - auth, err = ssh.NewPublicKeysFromFile("git", privateKey, config.SSH.Passphrase) + auth, err = getSSHAuthMethod(outConfig) if err != nil { return nil, err } - } else { - auth = &http.BasicAuth{ - Username: config.BasicAuth.Username, - Password: config.BasicAuth.Password, + case []interface{}: + for i, c := range sshConfig { + if _, ok := c.(map[interface{}]interface{}); !ok { + fmt.Printf("invalid format for configuration n°%d", i) + continue + } + + var outConfig configuration.Ssh + if err := mapstructure.Decode(c, &outConfig); err != nil { + fmt.Printf("failed to decode ssh configuration n°%d: %v\n", i, err) + continue + } + + auth, err = getSSHAuthMethod(outConfig) + if err != nil { + fmt.Printf("failed to create SSH authentication method for configuration n°%d: %v\n", i, err) + continue + } } + + if auth == nil { + auth = &http.BasicAuth{ + Username: config.BasicAuth.Username, + Password: config.BasicAuth.Password, + } + } + default: + return nil, errors.New("git ssh configuration canno't be unmarshaled. Please, pass a valid configuration") } repo := &Repository{ @@ -170,3 +192,25 @@ func (r *Repository) GetAuthor() *object.Signature { When: time.Now(), } } + +func getSSHAuthMethod(config configuration.Ssh) (transport.AuthMethod, error) { + if config.Passphrase == "" && config.PrivateKey == "" { + return nil, errors.New("passphrase and private are empty") + } + + privateKey, err := misc.AbsolutePath(config.PrivateKey) + if err != nil { + return nil, err + } + + if _, err := os.Stat(privateKey); err != nil { + return nil, err + } + + auth, err := ssh.NewPublicKeysFromFile("git", privateKey, config.Passphrase) + if err != nil { + return nil, err + } + + return auth, nil +} diff --git a/internal/index.go b/internal/index.go index 818829f..2aaa1d1 100644 --- a/internal/index.go +++ b/internal/index.go @@ -6,7 +6,7 @@ import ( "os" "strings" - "github.com/datahearth/config-mapper/internal/misc" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc" ) type Index struct { diff --git a/internal/items.go b/internal/items.go index 34106b3..3c514a5 100644 --- a/internal/items.go +++ b/internal/items.go @@ -7,9 +7,9 @@ import ( "path" "strings" - "github.com/datahearth/config-mapper/internal/configuration" - "github.com/datahearth/config-mapper/internal/git" - "github.com/datahearth/config-mapper/internal/misc" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/git" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/misc" "github.com/fatih/color" "github.com/spf13/viper" ) @@ -57,7 +57,7 @@ func (e *Items) Action(action string) { continue } if storagePath == "" && systemPath == "" { - fmt.Printf("⛔ Skipping %s\n", src) + fmt.Println("⛔ Skipping") continue } diff --git a/internal/misc/tools.go b/internal/misc/tools.go index 25fac7e..91c4b03 100644 --- a/internal/misc/tools.go +++ b/internal/misc/tools.go @@ -9,7 +9,7 @@ import ( "runtime" "strings" - "github.com/datahearth/config-mapper/internal/configuration" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration" ) func AbsolutePath(p string) (string, error) { diff --git a/internal/pkgs.go b/internal/pkgs.go index 059688b..b5a3155 100644 --- a/internal/pkgs.go +++ b/internal/pkgs.go @@ -7,7 +7,7 @@ import ( "runtime" "strings" - "github.com/datahearth/config-mapper/internal/configuration" + "gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration" "github.com/gernest/wow" "github.com/gernest/wow/spin" "github.com/spf13/viper" diff --git a/main.go b/main.go index 6d84da3..9d4b530 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,6 @@ package main -import "github.com/datahearth/config-mapper/cmd" +import "gitea.antoine-langlois.net/datahearth/config-mapper/cmd" func main() { cmd.Execute() diff --git a/release.py b/release.py index 79da2f6..2c5f249 100644 --- a/release.py +++ b/release.py @@ -113,8 +113,8 @@ if __name__ == "__main__": api_token: str if len(sys.argv) > 1: api_token = sys.argv.pop() - elif os.getenv("GIT_CFG_MAPPER_TOKEN"): - api_token = os.getenv("GIT_CFG_MAPPER_TOKEN") + elif "GIT_CFG_MAPPER_TOKEN" in os.environ: + api_token = os.environ["GIT_CFG_MAPPER_TOKEN"] else: log("no gitea api token found in CLI params nor in ENV", LogLevel.ERROR) sys.exit(1)