This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Cedric Kienzler
committed
Mar 9, 2021
1 parent
c9c631e
commit c6c11da
Showing
7 changed files
with
161 additions
and
12 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
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,15 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// getCmd represents the get command | ||
var describeCmd = &cobra.Command{ | ||
Use: "describe", | ||
Short: "Describe lets you describe a KKP object in more detail.", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(describeCmd) | ||
} |
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,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cedi/kkpctl/pkg/client" | ||
"github.com/cedi/kkpctl/pkg/describe" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// projectsCmd represents the projects command | ||
var describeProjectsCmd = &cobra.Command{ | ||
Use: "project [projectid]", | ||
Short: "Describe a project.", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
kkp, err := client.NewClient(baseURL, apiToken) | ||
if err != nil { | ||
return errors.New("Could not initialize Kubermatic API client") | ||
} | ||
|
||
project, err := kkp.GetProject(args[0]) | ||
if err != nil { | ||
return errors.Wrap(err, "Error fetching project") | ||
} | ||
|
||
parsed, err := describe.Object(project) | ||
if err != nil { | ||
return errors.Wrap(err, "Error describing project") | ||
} | ||
fmt.Println(parsed) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
describeCmd.AddCommand(describeProjectsCmd) | ||
} |
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,19 @@ | ||
package describe | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/kubermatic/go-kubermatic/models" | ||
) | ||
|
||
// Object takes any KKP Object as an input and then describes it | ||
func Object(object interface{}) (string, error) { | ||
// this is ugly and long, but it makes things kinda nicer to handle outside of the package | ||
project, ok := object.(models.Project) | ||
if ok { | ||
return describeProject(project) | ||
} | ||
|
||
return fmt.Sprintf("%v\n", object), errors.New("Unable to parse proper type of object") | ||
} |
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,77 @@ | ||
package describe | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/kubermatic/go-kubermatic/models" | ||
"github.com/lensesio/tableprinter" | ||
) | ||
|
||
// projectRender is a intermediate struct to make use of lensesio/tableprinter, which relies on the header anotation | ||
type projectMetaStruct struct { | ||
ID string `header:"ProjectID"` | ||
Name string `header:"Name"` | ||
Status string `header:"Status"` | ||
CreationTimestamp string `header:"Created"` | ||
} | ||
|
||
type ownerStruct struct { | ||
ID string `header:"UserID"` | ||
Name string `header:"Name"` | ||
Email string `header:"Email"` | ||
CreationTimestamp string `header:"Created"` | ||
} | ||
|
||
// describeProject takes any KKP Project and describes it | ||
func describeProject(project models.Project) (string, error) { | ||
projectMeta := projectMetaStruct{ | ||
ID: project.ID, | ||
Name: project.Name, | ||
CreationTimestamp: project.CreationTimestamp.String(), | ||
Status: project.Status, | ||
} | ||
|
||
var projectRenderBuf io.ReadWriter | ||
projectRenderBuf = new(bytes.Buffer) | ||
tableprinter.Print(projectRenderBuf, projectMeta) | ||
projectRenderBytes, err := ioutil.ReadAll(projectRenderBuf) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ownerMeta := make([]ownerStruct, len(project.Owners)) | ||
for idx, owner := range project.Owners { | ||
ownerMeta[idx] = ownerStruct{ | ||
ID: owner.ID, | ||
Name: owner.Name, | ||
Email: owner.Email, | ||
CreationTimestamp: owner.CreationTimestamp.String(), | ||
} | ||
} | ||
|
||
var ownerRenderBuf io.ReadWriter | ||
ownerRenderBuf = new(bytes.Buffer) | ||
tableprinter.Print(ownerRenderBuf, ownerMeta) | ||
ownerRenderBytes, err := ioutil.ReadAll(ownerRenderBuf) | ||
|
||
labels := make([]string, 0) | ||
for key, value := range project.Labels { | ||
labels = append(labels, fmt.Sprintf("%s=%s", key, value)) | ||
} | ||
if len(labels) == 0 { | ||
labels = append(labels, "[None]") | ||
} | ||
|
||
result := fmt.Sprintf("Project:\n%s\n\nOwners:\n%s\n\nLabels:\n%s\n\nClusters in this Project: %d", | ||
string(projectRenderBytes), | ||
string(ownerRenderBytes), | ||
strings.Join(labels, "; "), | ||
project.ClustersNumber, | ||
) | ||
|
||
return result, err | ||
} |
File renamed without changes.