/
command.go
55 lines (46 loc) · 1.1 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cmd
import (
"context"
"fmt"
"os"
"github.com/loft-sh/devpod-provider-terraform/pkg/terraform"
"github.com/loft-sh/devpod/pkg/log"
"github.com/loft-sh/devpod/pkg/provider"
"github.com/spf13/cobra"
)
// CommandCmd holds the cmd flags
type CommandCmd struct{}
// NewCommandCmd defines a command
func NewCommandCmd() *cobra.Command {
cmd := &CommandCmd{}
commandCmd := &cobra.Command{
Use: "command",
Short: "Command an instance",
RunE: func(_ *cobra.Command, args []string) error {
terraformProvider, err := terraform.NewProvider(log.Default)
if err != nil {
return err
}
return cmd.Run(
context.Background(),
terraformProvider,
provider.FromEnvironment(),
log.Default,
)
},
}
return commandCmd
}
// Run runs the command logic
func (cmd *CommandCmd) Run(
ctx context.Context,
providerTerraform *terraform.TerraformProvider,
machine *provider.Machine,
logs log.Logger,
) error {
command := os.Getenv("COMMAND")
if command == "" {
return fmt.Errorf("command environment variable is missing")
}
return terraform.Command(providerTerraform, command)
}