driftctl/pkg/remote/github/provider.go

60 lines
1.3 KiB
Go
Raw Normal View History

2021-02-15 13:24:20 +00:00
package github
import (
"os"
"github.com/cloudskiff/driftctl/pkg/remote/terraform"
tf "github.com/cloudskiff/driftctl/pkg/terraform"
)
type GithubTerraformProvider struct {
*terraform.TerraformProvider
}
type githubConfig struct {
Token string
Owner string `cty:"owner"`
Organization string
}
func NewGithubTerraformProvider() (*GithubTerraformProvider, error) {
p := &GithubTerraformProvider{}
providerKey := "github"
installer, err := tf.NewProviderInstaller(tf.ProviderConfig{
Key: providerKey,
Version: "4.4.0",
})
if err != nil {
return nil, err
}
tfProvider, err := terraform.NewTerraformProvider(installer, terraform.TerraformProviderConfig{
Name: providerKey,
2021-02-26 11:12:36 +00:00
DefaultAlias: p.GetConfig().getDefaultOwner(),
2021-02-15 13:24:20 +00:00
GetProviderConfig: func(owner string) interface{} {
return githubConfig{
2021-02-26 11:12:36 +00:00
Owner: p.GetConfig().getDefaultOwner(),
2021-02-15 13:24:20 +00:00
}
},
})
if err != nil {
return nil, err
}
p.TerraformProvider = tfProvider
return p, err
}
2021-02-26 11:12:36 +00:00
func (c githubConfig) getDefaultOwner() string {
if c.Organization != "" {
return c.Organization
2021-02-15 13:24:20 +00:00
}
2021-02-26 11:12:36 +00:00
return c.Owner
2021-02-15 13:24:20 +00:00
}
func (p GithubTerraformProvider) GetConfig() githubConfig {
return githubConfig{
Token: os.Getenv("GITHUB_TOKEN"),
Owner: os.Getenv("GITHUB_OWNER"),
Organization: os.Getenv("GITHUB_ORGANIZATION"),
}
}