-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: list all known profiles with additional info (#11)
Signed-off-by: Mario Constanti <github@constanti.de>
- Loading branch information
1 parent
ef687eb
commit 626d44a
Showing
7 changed files
with
111 additions
and
6 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
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ var ( | |
flagProfileName string | ||
flagImage string | ||
flagDebug bool | ||
flagVerboseList bool | ||
) |
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,70 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
package command | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fatih/color" | ||
"github.com/rodaine/table" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/bavarianbidi/kubectl-dpm/pkg/config" | ||
"github.com/bavarianbidi/kubectl-dpm/pkg/profile" | ||
) | ||
|
||
func List() *cobra.Command { | ||
listCmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "list all profiles", | ||
|
||
RunE: func(c *cobra.Command, args []string) error { | ||
if err := config.GenerateConfig(); err != nil { | ||
return err | ||
} | ||
|
||
if err := generateListOutput(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
listCmd.Flags().BoolVarP(&flagVerboseList, "wide", "w", false, "show more information about profiles") | ||
|
||
return listCmd | ||
} | ||
|
||
func generateListOutput() error { | ||
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() | ||
columnFmt := color.New(color.FgYellow).SprintfFunc() | ||
|
||
var tbl table.Table | ||
tbl = table.New("Name", "Profile") | ||
|
||
if flagVerboseList { | ||
tbl = table.New("Name", "Profile", "Image", "Namespace", "MatchLabels") | ||
} | ||
tbl. | ||
WithHeaderFormatter(headerFmt). | ||
WithFirstColumnFormatter(columnFmt) | ||
|
||
for _, p := range profile.Config.Profiles { | ||
var matchLabels string | ||
for label, value := range p.MatchLabels { | ||
matchLabels += fmt.Sprintf("%s=%s, ", label, value) | ||
} | ||
tbl.AddRow( | ||
p.ProfileName, // Name | ||
p.CustomProfileFile, // Profile | ||
p.Image, // Image | ||
p.Namespace, // Namespace | ||
matchLabels, // MatchLabels | ||
) | ||
} | ||
|
||
tbl.Print() | ||
|
||
return nil | ||
} |
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,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
package profile | ||
|
||
import ( | ||
"cmp" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
func SortProfiles() { | ||
// sort profiles by name | ||
slices.SortFunc(Config.Profiles, func(a, b Profile) int { | ||
return cmp.Compare(strings.ToLower(a.ProfileName), strings.ToLower(b.ProfileName)) | ||
}) | ||
} |
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