Skip to content
Merged
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
82 changes: 58 additions & 24 deletions cmd/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"time"
Expand All @@ -18,6 +19,12 @@ import (
"github.com/elastic/elastic-package/internal/profile"
)

// jsonFormat is the format for JSON output
const jsonFormat = "json"

// tableFormat is the format for table output
const tableFormat = "table"

// profileNameEnvVar is the name of the environment variable to set the default profile
const profileNameEnvVar = "ELASTIC_PACKAGE_PROFILE"

Expand Down Expand Up @@ -97,38 +104,65 @@ User profiles are not overwritten on upgrade of elastic-stack, and can be freely
return errors.Wrap(err, "error listing all profiles")
}

table := tablewriter.NewWriter(os.Stdout)
var profilestable = profileToList(profileList)

table.SetHeader([]string{"Name", "Date Created", "User", "Version", "Path"})
table.SetHeaderColor(
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
)
table.SetColumnColor(
twColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor}),
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
)

table.SetAutoMergeCells(false)
table.SetRowLine(true)
table.AppendBulk(profilestable)
table.Render()
format, err := cmd.Flags().GetString(cobraext.ProfileFormatFlagName)
if err != nil {
return cobraext.FlagParsingError(err, cobraext.ProfileFromFlagName)
}

return nil
switch format {
case tableFormat:
return formatTable(profileList)
case jsonFormat:
return formatJSON(profileList)
default:
return fmt.Errorf("format %s not supported", format)
}
},
}
profileListCommand.Flags().String(cobraext.ProfileFormatFlagName, tableFormat, cobraext.ProfileFormatFlagDescription)

profileCommand.AddCommand(profileNewCommand, profileDeleteCommand, profileListCommand)

return cobraext.NewCommand(profileCommand, cobraext.ContextGlobal)
}

func formatJSON(profileList []profile.Metadata) error {
data, err := json.Marshal(profileList)
if err != nil {
return errors.Wrap(err, "error listing all profiles in JSON format")
}

fmt.Print(string(data))

return nil
}

func formatTable(profileList []profile.Metadata) error {
table := tablewriter.NewWriter(os.Stdout)
var profilesTable = profileToList(profileList)

table.SetHeader([]string{"Name", "Date Created", "User", "Version", "Path"})
table.SetHeaderColor(
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
twColor(tablewriter.Colors{tablewriter.Bold}),
)
table.SetColumnColor(
twColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgCyanColor}),
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
tablewriter.Colors{},
)

table.SetAutoMergeCells(false)
table.SetRowLine(true)
table.AppendBulk(profilesTable)
table.Render()

return nil
}

func profileToList(profiles []profile.Metadata) [][]string {
Expand Down
3 changes: 3 additions & 0 deletions internal/cobraext/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (
ProfileFromFlagName = "from"
ProfileFromFlagDescription = "copy profile from the specified existing profile"

ProfileFormatFlagName = "format"
ProfileFormatFlagDescription = "format of the profiles list (table | json)"

NewestOnlyFlagName = "newest-only"
NewestOnlyFlagDescription = "promote newest packages and remove old ones"

Expand Down