Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get packages sub-command #303

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/cmd/get/packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package get

import (
"github.com/spf13/cobra"
)

const (
packageTemplatePath = "templates/package.tmpl"
)

var PackagesCmd = &cobra.Command{
Use: "packages",
Short: "retrieve package info from the cluster",
Long: ``,
RunE: getPackagesE,
}

type PackageTemplateData struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Category string `json:"category"`
PackageType string `json:"packageType"`
Comment on lines +21 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly do not know if we can get this kind of data right now.

Spec PackageSpecTemplateData `json:"spec"`
}

type PackageSpecTemplateData struct {
Description string `json:"description"`
URL string `json:"url"`
Credentials map[string]string `json:"credentials"`
}

func getPackagesE(cmd *cobra.Command, args []string) error {
return nil
}
1 change: 1 addition & 0 deletions pkg/cmd/get/packages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package get
4 changes: 4 additions & 0 deletions pkg/cmd/get/root.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package get

import (
"embed"
"fmt"

"github.com/spf13/cobra"
)

//go:embed templates
var templates embed.FS

var GetCmd = &cobra.Command{
Use: "get",
Short: "get information from the cluster",
Expand Down
10 changes: 3 additions & 7 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package get

import (
"context"
"embed"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -31,9 +30,6 @@ const (
giteaAdminSecretName = "gitea-credential"
)

//go:embed templates
var templates embed.FS

var SecretsCmd = &cobra.Command{
Use: "secrets",
Short: "retrieve secrets from the cluster",
Expand All @@ -47,7 +43,7 @@ var corePkgSecrets = map[string][]string{
"gitea": []string{giteaAdminSecretName},
}

type TemplateData struct {
type SecretTemplateData struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Data map[string]string `json:"data"`
Expand Down Expand Up @@ -197,8 +193,8 @@ func printOutput(templatePath string, outWriter io.Writer, data []any, format st
}
}

func secretToTemplateData(s v1.Secret) TemplateData {
data := TemplateData{
func secretToTemplateData(s v1.Secret) SecretTemplateData {
data := SecretTemplateData{
Name: s.Name,
Namespace: s.Namespace,
Data: make(map[string]string),
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/get/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestOutput(t *testing.T) {
ctx := context.Background()
r, _ := labels.NewRequirement(v1alpha1.CLISecretLabelKey, selection.Equals, []string{v1alpha1.CLISecretLabelValue})

corePkgData := map[string]TemplateData{
corePkgData := map[string]SecretTemplateData{
argoCDInitialAdminSecretName: {
Name: argoCDInitialAdminSecretName,
Namespace: "argocd",
Expand All @@ -163,7 +163,7 @@ func TestOutput(t *testing.T) {
},
}

packageData := map[string]TemplateData{
packageData := map[string]SecretTemplateData{
"name1": {
Name: "name1",
Namespace: "ns1",
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestOutput(t *testing.T) {
assert.Nil(t, err)

// verify received json data
var received []TemplateData
var received []SecretTemplateData
err = json.Unmarshal(buffer.Bytes(), &received)
assert.Nil(t, err)
assert.Equal(t, 4, len(received))
Expand All @@ -243,7 +243,7 @@ func TestOutput(t *testing.T) {
assert.Equal(t, 0, len(packageData))
}

func templateDataToSecret(data TemplateData) v1.Secret {
func templateDataToSecret(data SecretTemplateData) v1.Secret {
d := make(map[string][]byte)
for k := range data.Data {
d[k] = []byte(data.Data[k])
Expand Down
12 changes: 12 additions & 0 deletions pkg/cmd/get/templates/package.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: {{ .Name }}
namespace: {{ .Namespace }}
category: {{ .Category }}
package-type: {{ .PackageType }}
spec:
description: {{ .Description }}
url: {{ .URL }}
credentials:
{{- range $key, $value := .Credentials }}
{{ $key }}: {{ $value }}
{{- end }}
10 changes: 5 additions & 5 deletions pkg/cmd/get/templates/secrets.tmpl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. This template file is not meant to be YAML parseable though. I know it looks like one but it's meant for humans to read the outputs easily. We have a yaml output option for yaml parsing purposes:

func printOutput(templatePath string, outWriter io.Writer, data []any, format string) error {
switch format {
case "json":
enc := json.NewEncoder(outWriter)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
return enc.Encode(data)
case "yaml":
b, err := yaml.Marshal(data)
if err != nil {
return err
}
_, err = outWriter.Write(b)
return err
case "":
return renderTemplate(templatePath, outWriter, data)
default:
return fmt.Errorf("output format %s is not supported", format)
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---------------------------
Name: {{ .Name }}
Namespace: {{ .Namespace }}
Data:
---
name: {{ .Name }}
namespace: {{ .Namespace }}
data:
{{- range $key, $value := .Data }}
{{ $key }} : {{ $value }}
{{ $key }}: {{ $value }}
{{- end }}
Loading