-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the code to their corresponding go file
Signed-off-by: cmoulliard <[email protected]>
- Loading branch information
1 parent
358c83b
commit 3a2c119
Showing
3 changed files
with
125 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cnoe-io/idpbuilder/pkg/entity" | ||
"io" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type ClusterPrinter struct { | ||
Clusters []entity.Cluster | ||
OutWriter io.Writer | ||
} | ||
|
||
func (cp ClusterPrinter) PrintOutput(format string) error { | ||
switch format { | ||
case "json": | ||
return PrintDataAsJson(cp.Clusters, cp.OutWriter) | ||
case "yaml": | ||
return PrintDataAsYaml(cp.Clusters, cp.OutWriter) | ||
case "table": | ||
return PrintDataAsTable(cp.generateClusterTable(cp.Clusters), cp.OutWriter) | ||
default: | ||
return fmt.Errorf("output format %s is not supported", format) | ||
} | ||
} | ||
|
||
func (cp ClusterPrinter) generateClusterTable(input []entity.Cluster) metav1.Table { | ||
table := &metav1.Table{} | ||
table.ColumnDefinitions = []metav1.TableColumnDefinition{ | ||
{Name: "Name", Type: "string"}, | ||
{Name: "External-Port", Type: "string"}, | ||
{Name: "Kube-Api", Type: "string"}, | ||
{Name: "TLS", Type: "string"}, | ||
{Name: "Kube-Port", Type: "string"}, | ||
{Name: "Nodes", Type: "string"}, | ||
} | ||
|
||
for _, cluster := range input { | ||
row := metav1.TableRow{ | ||
Cells: []interface{}{ | ||
cluster.Name, | ||
cluster.ExternalPort, | ||
cluster.URLKubeApi, | ||
cluster.TlsCheck, | ||
cluster.KubePort, | ||
generateNodeData(cluster.Nodes), | ||
}, | ||
} | ||
table.Rows = append(table.Rows, row) | ||
} | ||
return *table | ||
} | ||
|
||
func generateNodeData(nodes []entity.Node) string { | ||
var result string | ||
for i, aNode := range nodes { | ||
result += aNode.Name | ||
if i < len(nodes)-1 { | ||
result += "," | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cnoe-io/idpbuilder/pkg/entity" | ||
"io" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"strings" | ||
) | ||
|
||
type SecretPrinter struct { | ||
Secrets []entity.Secret | ||
OutWriter io.Writer | ||
} | ||
|
||
func (sp SecretPrinter) PrintOutput(format string) error { | ||
switch format { | ||
case "json": | ||
return PrintDataAsJson(sp.Secrets, sp.OutWriter) | ||
case "yaml": | ||
return PrintDataAsYaml(sp.Secrets, sp.OutWriter) | ||
case "table": | ||
return PrintDataAsTable(sp.generateSecretTable(sp.Secrets), sp.OutWriter) | ||
default: | ||
return fmt.Errorf("output format %s is not supported", format) | ||
} | ||
} | ||
|
||
func (sp SecretPrinter) generateSecretTable(secretTable []entity.Secret) metav1.Table { | ||
table := &metav1.Table{} | ||
table.ColumnDefinitions = []metav1.TableColumnDefinition{ | ||
{Name: "Name", Type: "string"}, | ||
{Name: "Namespace", Type: "string"}, | ||
{Name: "Username", Type: "string"}, | ||
{Name: "Password", Type: "string"}, | ||
{Name: "Token", Type: "string"}, | ||
{Name: "Data", Type: "string"}, | ||
} | ||
for _, secret := range secretTable { | ||
var dataEntries []string | ||
|
||
if !secret.IsCore { | ||
for key, value := range secret.Data { | ||
dataEntries = append(dataEntries, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
} | ||
dataString := strings.Join(dataEntries, ", ") | ||
row := metav1.TableRow{ | ||
Cells: []interface{}{ | ||
secret.Name, | ||
secret.Namespace, | ||
secret.Username, | ||
secret.Password, | ||
secret.Token, | ||
dataString, | ||
}, | ||
} | ||
table.Rows = append(table.Rows, row) | ||
} | ||
return *table | ||
} |