driftctl/pkg/remote/github/provider.go

75 lines
1.6 KiB
Go
Raw Normal View History

2021-02-15 13:24:20 +00:00
package github
import (
"os"
"github.com/cloudskiff/driftctl/pkg/output"
2021-02-15 13:24:20 +00:00
"github.com/cloudskiff/driftctl/pkg/remote/terraform"
tf "github.com/cloudskiff/driftctl/pkg/terraform"
)
type GithubTerraformProvider struct {
*terraform.TerraformProvider
2021-06-28 09:00:59 +00:00
name string
version string
2021-02-15 13:24:20 +00:00
}
type githubConfig struct {
Token string
Owner string `cty:"owner"`
Organization string
}
func NewGithubTerraformProvider(version string, progress output.Progress, configDir string) (*GithubTerraformProvider, error) {
2021-06-28 09:00:59 +00:00
p := &GithubTerraformProvider{
version: version,
name: "github",
}
2021-02-15 13:24:20 +00:00
installer, err := tf.NewProviderInstaller(tf.ProviderConfig{
2021-06-28 09:00:59 +00:00
Key: p.name,
Version: version,
ConfigDir: configDir,
2021-02-15 13:24:20 +00:00
})
if err != nil {
return nil, err
}
tfProvider, err := terraform.NewTerraformProvider(installer, terraform.TerraformProviderConfig{
2021-06-28 09:00:59 +00:00
Name: p.name,
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
}
},
}, progress)
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"),
}
}
2021-06-28 09:00:59 +00:00
func (p *GithubTerraformProvider) Name() string {
return p.name
}
func (p *GithubTerraformProvider) Version() string {
return p.version
}