-
Notifications
You must be signed in to change notification settings - Fork 10
/
helper.go
165 lines (138 loc) · 5.1 KB
/
helper.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright (C) 2022 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
terraformv1alpha1 "github.com/appvia/terranetes-controller/pkg/apis/terraform/v1alpha1"
)
// NewTableWriter returns a default table writer
func NewTableWriter(out io.Writer) *tablewriter.Table {
tw := tablewriter.NewWriter(out)
tw.SetAutoWrapText(false)
tw.SetAutoFormatHeaders(true)
tw.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
tw.SetAlignment(tablewriter.ALIGN_LEFT)
tw.SetCenterSeparator("")
tw.SetColumnSeparator("")
tw.SetRowSeparator("")
tw.SetHeaderLine(false)
tw.SetBorder(false)
tw.SetTablePadding("\t")
tw.SetNoWhiteSpace(true)
return tw
}
// AutoCompletionFunc is a function that returns a list of completions
type AutoCompletionFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
// MarkHidden marks a command as hidden and panics if an error is thrown
func MarkHidden(cmd *cobra.Command, name string) {
if err := cmd.Flags().MarkHidden(name); err != nil {
panic(errors.New("missing argument: " + name))
}
}
// RegisterFlagCompletionFunc registers a completion function for a flag
func RegisterFlagCompletionFunc(cmd *cobra.Command, name string, fn AutoCompletionFunc) {
if err := cmd.RegisterFlagCompletionFunc(name, fn); err != nil {
panic(errors.New("missing argument: " + name))
}
}
// AutoCompleteWithList registers a completion function for a flag
func AutoCompleteWithList(list []string) AutoCompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return list, cobra.ShellCompDirectiveNoFileComp
}
}
// AutoCompleteConfigurations registers a completion function for a flag
func AutoCompleteConfigurations(factory Factory) AutoCompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
namespace, _ := cmd.Flags().GetString("namespace")
if namespace == "" {
return nil, cobra.ShellCompDirectiveNoFileComp
}
cc, err := factory.GetClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
list := &terraformv1alpha1.ConfigurationList{}
if err := cc.List(context.Background(), list, client.InNamespace(namespace)); err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var resources []string
for _, resource := range list.Items {
resources = append(resources, resource.GetName())
}
return resources, cobra.ShellCompDirectiveNoFileComp
}
}
// AutoCompletionAvailableProviders returns a list of available providers in the cluster
func AutoCompletionAvailableProviders(factory Factory) AutoCompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := ListAvailableProviders(cmd.Context(), factory)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return list, cobra.ShellCompDirectiveNoFileComp
}
}
// AutoCompleteNamespaces registers a completion function for a flag
func AutoCompleteNamespaces(factory Factory) AutoCompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
cc, err := factory.GetClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
list := &v1.NamespaceList{}
if err := cc.List(context.Background(), list); err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
var namespaces []string
for _, namespace := range list.Items {
namespaces = append(namespaces, namespace.GetName())
}
return namespaces, cobra.ShellCompDirectiveNoFileComp
}
}
// ConfigPath returns the path to the config file
func ConfigPath() string {
if os.Getenv(ConfigPathEnvName) != "" {
return os.Getenv(ConfigPathEnvName)
}
return filepath.Join(os.Getenv("HOME"), ".tnctl", "config.yaml")
}
// ListAvailableProviders returns a list of available providers in the cluster
func ListAvailableProviders(ctx context.Context, factory Factory) ([]string, error) {
cc, err := factory.GetClient()
if err != nil {
return nil, err
}
list := &terraformv1alpha1.ProviderList{}
if err := cc.List(ctx, list); err != nil {
return nil, err
}
var providers []string
for _, provider := range list.Items {
providers = append(providers, string(provider.Spec.Provider))
}
return providers, nil
}