forked from hashicorp/tfc-workflows-tooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
91 lines (75 loc) · 2.55 KB
/
cli.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"flag"
"log"
"os"
"github.com/hashicorp/tfci/internal/cloud"
"github.com/hashicorp/tfci/internal/writer"
"github.com/hashicorp/tfci/version"
cmd "github.com/hashicorp/tfci/internal/command"
"github.com/mitchellh/cli"
)
var (
hostnameFlag = flag.String("hostname", "", "The hostname of a Terraform Enterprise installation, if using Terraform Enterprise. Defaults to Terraform Cloud (app.terraform.io)")
tokenFlag = flag.String("token", "", "The token used to authenticate with Terraform Cloud. Defaults to reading `TF_API_TOKEN` environment variable")
organizationFlag = flag.String("organization", "", "Terraform Cloud Organization Name")
)
func newCliRunner() (*cli.CLI, error) {
args := os.Args[1:]
log.Printf("[DEBUG] Command argument count: %d", len(args))
err := flag.CommandLine.Parse(args)
if err != nil {
return nil, err
}
newArgs := flag.CommandLine.Args()
cliRunner := cli.NewCLI("tfc", version.GetVersion())
cliRunner.Args = newArgs
writer := writer.NewWriter(Ui)
orgEnv := os.Getenv("TF_CLOUD_ORGANIZATION")
if *organizationFlag == "" && orgEnv != "" {
*organizationFlag = orgEnv
}
log.Printf("[DEBUG] Subcommand arg count: %d for organization: %s", len(newArgs), orgEnv)
tfe, err := cloud.NewTfeClient(*hostnameFlag, *tokenFlag, string(env.PlatformType))
if err != nil {
log.Printf("[ERROR] Could not initialize terraform cloud client, error: %#v", err)
return nil, err
}
cloudService := cloud.NewCloud(tfe, writer)
meta := cmd.NewMetaOpts(
appCtx,
cloudService,
env,
cmd.WithOrg(*organizationFlag),
cmd.WithWriter(writer),
)
cliRunner.Commands = map[string]cli.CommandFactory{
"upload": func() (cli.Command, error) {
return &cmd.UploadConfigurationCommand{Meta: meta}, nil
},
"run create": func() (cli.Command, error) {
return &cmd.CreateRunCommand{Meta: meta}, nil
},
"run apply": func() (cli.Command, error) {
return &cmd.ApplyRunCommand{Meta: meta}, nil
},
"run show": func() (cli.Command, error) {
return &cmd.ShowRunCommand{Meta: meta}, nil
},
"run discard": func() (cli.Command, error) {
return &cmd.DiscardRunCommand{Meta: meta}, nil
},
"run cancel": func() (cli.Command, error) {
return &cmd.CancelRunCommand{Meta: meta}, nil
},
"plan output": func() (cli.Command, error) {
return &cmd.OutputPlanCommand{Meta: meta}, nil
},
"workspace output list": func() (cli.Command, error) {
return &cmd.WorkspaceOutputCommand{Meta: meta}, nil
},
}
return cliRunner, nil
}