-
Notifications
You must be signed in to change notification settings - Fork 21
/
plan.go
135 lines (110 loc) · 3.08 KB
/
plan.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/awslabs/tecli/cobra/aid"
"github.com/awslabs/tecli/cobra/dao"
"github.com/awslabs/tecli/helper"
"github.com/hashicorp/go-tfe"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var planValidArgs = []string{"read", "logs"}
// PlanCmd command to display tecli current version
func PlanCmd() *cobra.Command {
man, err := helper.GetManual("plan", planValidArgs)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cmd := &cobra.Command{
Use: man.Use,
Short: man.Short,
Long: man.Long,
Example: man.Example,
ValidArgs: planValidArgs,
Args: cobra.OnlyValidArgs,
PreRunE: planPreRun,
RunE: planRun,
SilenceUsage: true,
}
aid.SetPlanFlags(cmd)
return cmd
}
func planPreRun(cmd *cobra.Command, args []string) error {
if err := helper.ValidateCmdArgs(cmd, args, "plan"); err != nil {
return err
}
fArg := args[0]
switch fArg {
case "read", "logs":
if err := helper.ValidateCmdArgAndFlag(cmd, args, "plan", fArg, "id"); err != nil {
return err
}
}
return nil
}
func planRun(cmd *cobra.Command, args []string) error {
token := dao.GetTeamToken(profile)
client := aid.GetTFEClient(token)
fArg := args[0]
switch fArg {
case "read":
id, err := cmd.Flags().GetString("id")
if err != nil {
return fmt.Errorf("unable to get flag id\n%v", err)
}
plan, err := planRead(client, id)
if err == nil {
fmt.Println(aid.ToJSON(plan))
} else {
return fmt.Errorf("plan %s not found\n%v", id, err)
}
case "logs":
id, err := cmd.Flags().GetString("id")
if err != nil {
return fmt.Errorf("unable to get flag id\n%v", err)
}
logs, err := planLogs(client, id)
if err != nil {
logrus.Fatalf("unable to read plan logs\n%v", err)
}
fmt.Println(StreamToString(logs))
}
return nil
}
// Read a plan by its ID.
func planRead(client *tfe.Client, planID string) (*tfe.Plan, error) {
return client.Plans.Read(context.Background(), planID)
}
// Logs retrieves the logs of a plan.
func planLogs(client *tfe.Client, planID string) (io.Reader, error) {
return client.Plans.Logs(context.Background(), planID)
}
// StreamToByte converts io.Reader to []byte
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
// StreamToString convert io.Reader to string
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.String()
}