command/push: integrate atlas push client
This commit is contained in:
parent
1c0ffbd7bf
commit
19c1771438
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/atlas-go/archive"
|
"github.com/hashicorp/atlas-go/archive"
|
||||||
|
"github.com/hashicorp/atlas-go/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PushCommand struct {
|
type PushCommand struct {
|
||||||
|
@ -99,6 +100,21 @@ func (c *PushCommand) Run(args []string) int {
|
||||||
name = config.Atlas.Name
|
name = config.Atlas.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize the client if it isn't given.
|
||||||
|
if c.client == nil {
|
||||||
|
// Make sure to nil out our client so our token isn't sitting around
|
||||||
|
defer func() { c.client = nil }()
|
||||||
|
|
||||||
|
// Initialize it to the default client, we set custom settings later
|
||||||
|
client := atlas.DefaultClient()
|
||||||
|
|
||||||
|
if atlasToken != "" {
|
||||||
|
client.Token = atlasToken
|
||||||
|
}
|
||||||
|
|
||||||
|
c.client = &atlasPushClient{Client: client}
|
||||||
|
}
|
||||||
|
|
||||||
// Get the variables we might already have
|
// Get the variables we might already have
|
||||||
vars, err := c.client.Get(name)
|
vars, err := c.client.Get(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -200,6 +216,43 @@ type pushUpsertOptions struct {
|
||||||
Variables map[string]string
|
Variables map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type atlasPushClient struct {
|
||||||
|
Client *atlas.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *atlasPushClient) Get(name string) (map[string]string, error) {
|
||||||
|
user, name, err := atlas.ParseSlug(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := c.Client.TerraformConfigLatest(user, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return version.Variables, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *atlasPushClient) Upsert(opts *pushUpsertOptions) (int, error) {
|
||||||
|
user, name, err := atlas.ParseSlug(opts.Name)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := &atlas.TerraformConfigVersion{
|
||||||
|
Variables: opts.Variables,
|
||||||
|
}
|
||||||
|
|
||||||
|
version, err := c.Client.CreateTerraformConfigVersion(
|
||||||
|
user, name, data, opts.Archive, opts.Archive.Size)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return version, nil
|
||||||
|
}
|
||||||
|
|
||||||
type mockPushClient struct {
|
type mockPushClient struct {
|
||||||
File string
|
File string
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue