mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add JSON output format
This commit is contained in:
parent
0cdaf49425
commit
52b11947e7
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
@ -54,17 +55,9 @@ var configListCmd = &cobra.Command{
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetString("config.list.format") != "" {
|
switch viper.GetString("config.list.format") {
|
||||||
// Print configs with a custom format
|
case "table":
|
||||||
template, templateParsingErr := template.New("configTpl").Parse(viper.GetString("config.list.format"))
|
// Print configs in a table format
|
||||||
common.CheckError(templateParsingErr)
|
|
||||||
for _, c := range configs {
|
|
||||||
templateExecutionErr := template.Execute(os.Stdout, c)
|
|
||||||
common.CheckError(templateExecutionErr)
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print configs with a table format
|
|
||||||
writer, err := common.NewTabWriter([]string{
|
writer, err := common.NewTabWriter([]string{
|
||||||
"KEY",
|
"KEY",
|
||||||
"ENV VAR",
|
"ENV VAR",
|
||||||
@ -82,6 +75,20 @@ var configListCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
flushErr := writer.Flush()
|
flushErr := writer.Flush()
|
||||||
common.CheckError(flushErr)
|
common.CheckError(flushErr)
|
||||||
|
case "json":
|
||||||
|
// Print configs in a json format
|
||||||
|
statusJsonBytes, err := json.Marshal(configs)
|
||||||
|
common.CheckError(err)
|
||||||
|
fmt.Println(string(statusJsonBytes))
|
||||||
|
default:
|
||||||
|
// Print configs in a custom format
|
||||||
|
template, templateParsingErr := template.New("configTpl").Parse(viper.GetString("config.list.format"))
|
||||||
|
common.CheckError(templateParsingErr)
|
||||||
|
for _, c := range configs {
|
||||||
|
templateExecutionErr := template.Execute(os.Stdout, c)
|
||||||
|
common.CheckError(templateExecutionErr)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -89,7 +96,7 @@ var configListCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
configCmd.AddCommand(configListCmd)
|
configCmd.AddCommand(configListCmd)
|
||||||
|
|
||||||
configListCmd.Flags().String("format", "", "Format output using a Go template.")
|
configListCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
||||||
viper.BindPFlag("config.list.format", configListCmd.Flags().Lookup("format"))
|
viper.BindPFlag("config.list.format", configListCmd.Flags().Lookup("format"))
|
||||||
|
|
||||||
configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + common.GetFormatHelp(config{}))
|
configListCmd.SetUsageTemplate(configListCmd.UsageTemplate() + common.GetFormatHelp(config{}))
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -32,17 +33,9 @@ var endpointGroupListCmd = &cobra.Command{
|
|||||||
endpointGroups, err := client.GetEndpointGroups()
|
endpointGroups, err := client.GetEndpointGroups()
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
if viper.GetString("endpoint.group.list.format") != "" {
|
switch viper.GetString("endpoint.group.list.format") {
|
||||||
// Print endpoint group fields formatted
|
case "table":
|
||||||
template, templateParsingErr := template.New("endpointGroupTpl").Parse(viper.GetString("endpoint.group.list.format"))
|
// Print endpoint groups in a table format
|
||||||
common.CheckError(templateParsingErr)
|
|
||||||
for _, g := range endpointGroups {
|
|
||||||
templateExecutionErr := template.Execute(os.Stdout, g)
|
|
||||||
common.CheckError(templateExecutionErr)
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print all endpoint group fields as a table
|
|
||||||
writer, err := common.NewTabWriter([]string{
|
writer, err := common.NewTabWriter([]string{
|
||||||
"ID",
|
"ID",
|
||||||
"NAME",
|
"NAME",
|
||||||
@ -60,6 +53,20 @@ var endpointGroupListCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
flushErr := writer.Flush()
|
flushErr := writer.Flush()
|
||||||
common.CheckError(flushErr)
|
common.CheckError(flushErr)
|
||||||
|
case "json":
|
||||||
|
// Print endpoint groups in a json format
|
||||||
|
statusJsonBytes, err := json.Marshal(endpointGroups)
|
||||||
|
common.CheckError(err)
|
||||||
|
fmt.Println(string(statusJsonBytes))
|
||||||
|
default:
|
||||||
|
// Print endpoint groups in a custom format
|
||||||
|
template, templateParsingErr := template.New("endpointGroupTpl").Parse(viper.GetString("endpoint.group.list.format"))
|
||||||
|
common.CheckError(templateParsingErr)
|
||||||
|
for _, g := range endpointGroups {
|
||||||
|
templateExecutionErr := template.Execute(os.Stdout, g)
|
||||||
|
common.CheckError(templateExecutionErr)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -67,7 +74,7 @@ var endpointGroupListCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
endpointGroupCmd.AddCommand(endpointGroupListCmd)
|
endpointGroupCmd.AddCommand(endpointGroupListCmd)
|
||||||
|
|
||||||
endpointGroupListCmd.Flags().String("format", "", "Format output using a Go template.")
|
endpointGroupListCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
||||||
viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format"))
|
viper.BindPFlag("endpoint.group.list.format", endpointGroupListCmd.Flags().Lookup("format"))
|
||||||
|
|
||||||
endpointGroupListCmd.SetUsageTemplate(endpointGroupListCmd.UsageTemplate() + common.GetFormatHelp(portainer.EndpointGroup{}))
|
endpointGroupListCmd.SetUsageTemplate(endpointGroupListCmd.UsageTemplate() + common.GetFormatHelp(portainer.EndpointGroup{}))
|
||||||
|
@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -48,17 +49,9 @@ var endpointListCmd = &cobra.Command{
|
|||||||
endpoints, err := client.GetEndpoints()
|
endpoints, err := client.GetEndpoints()
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
if viper.GetString("endpoint.list.format") != "" {
|
switch viper.GetString("endpoint.list.format") {
|
||||||
// Print endpoint fields formatted
|
case "table":
|
||||||
template, templateParsingErr := template.New("endpointTpl").Parse(viper.GetString("endpoint.list.format"))
|
// Print endpoints in a table format
|
||||||
common.CheckError(templateParsingErr)
|
|
||||||
for _, e := range endpoints {
|
|
||||||
templateExecutionErr := template.Execute(os.Stdout, e)
|
|
||||||
common.CheckError(templateExecutionErr)
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print all endpoint fields as a table
|
|
||||||
writer, err := common.NewTabWriter([]string{
|
writer, err := common.NewTabWriter([]string{
|
||||||
"ID",
|
"ID",
|
||||||
"NAME",
|
"NAME",
|
||||||
@ -88,6 +81,20 @@ var endpointListCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
flushErr := writer.Flush()
|
flushErr := writer.Flush()
|
||||||
common.CheckError(flushErr)
|
common.CheckError(flushErr)
|
||||||
|
case "json":
|
||||||
|
// Print endpoints in a json format
|
||||||
|
statusJsonBytes, err := json.Marshal(endpoints)
|
||||||
|
common.CheckError(err)
|
||||||
|
fmt.Println(string(statusJsonBytes))
|
||||||
|
default:
|
||||||
|
// Print endpoints in a custom format
|
||||||
|
template, templateParsingErr := template.New("endpointTpl").Parse(viper.GetString("endpoint.list.format"))
|
||||||
|
common.CheckError(templateParsingErr)
|
||||||
|
for _, e := range endpoints {
|
||||||
|
templateExecutionErr := template.Execute(os.Stdout, e)
|
||||||
|
common.CheckError(templateExecutionErr)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -95,7 +102,7 @@ var endpointListCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
endpointCmd.AddCommand(endpointListCmd)
|
endpointCmd.AddCommand(endpointListCmd)
|
||||||
|
|
||||||
endpointListCmd.Flags().String("format", "", "Format output using a Go template.")
|
endpointListCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
||||||
viper.BindPFlag("endpoint.list.format", endpointListCmd.Flags().Lookup("format"))
|
viper.BindPFlag("endpoint.list.format", endpointListCmd.Flags().Lookup("format"))
|
||||||
|
|
||||||
endpointListCmd.SetUsageTemplate(endpointListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Endpoint{}))
|
endpointListCmd.SetUsageTemplate(endpointListCmd.UsageTemplate() + common.GetFormatHelp(portainer.Endpoint{}))
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/greenled/portainer-stack-utils/client"
|
"github.com/greenled/portainer-stack-utils/client"
|
||||||
|
|
||||||
portainer "github.com/portainer/portainer/api"
|
portainer "github.com/portainer/portainer/api"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -64,17 +64,9 @@ var stackListCmd = &cobra.Command{
|
|||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if viper.GetString("stack.list.format") != "" {
|
switch viper.GetString("stack.list.format") {
|
||||||
// Print stack fields formatted
|
case "table":
|
||||||
template, templateParsingErr := template.New("stackTpl").Parse(viper.GetString("stack.list.format"))
|
// Print stacks in a table format
|
||||||
common.CheckError(templateParsingErr)
|
|
||||||
for _, s := range stacks {
|
|
||||||
templateExecutionErr := template.Execute(os.Stdout, s)
|
|
||||||
common.CheckError(templateExecutionErr)
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Print all stack fields as a table
|
|
||||||
writer, err := common.NewTabWriter([]string{
|
writer, err := common.NewTabWriter([]string{
|
||||||
"ID",
|
"ID",
|
||||||
"NAME",
|
"NAME",
|
||||||
@ -94,6 +86,20 @@ var stackListCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
flushErr := writer.Flush()
|
flushErr := writer.Flush()
|
||||||
common.CheckError(flushErr)
|
common.CheckError(flushErr)
|
||||||
|
case "json":
|
||||||
|
// Print stacks in a json format
|
||||||
|
stacksJsonBytes, err := json.Marshal(stacks)
|
||||||
|
common.CheckError(err)
|
||||||
|
fmt.Println(string(stacksJsonBytes))
|
||||||
|
default:
|
||||||
|
// Print stacks in a custom format
|
||||||
|
template, templateParsingErr := template.New("stackTpl").Parse(viper.GetString("stack.list.format"))
|
||||||
|
common.CheckError(templateParsingErr)
|
||||||
|
for _, s := range stacks {
|
||||||
|
templateExecutionErr := template.Execute(os.Stdout, s)
|
||||||
|
common.CheckError(templateExecutionErr)
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -102,7 +108,7 @@ func init() {
|
|||||||
stackCmd.AddCommand(stackListCmd)
|
stackCmd.AddCommand(stackListCmd)
|
||||||
|
|
||||||
stackListCmd.Flags().Int("endpoint", 0, "Filter by endpoint ID.")
|
stackListCmd.Flags().Int("endpoint", 0, "Filter by endpoint ID.")
|
||||||
stackListCmd.Flags().String("format", "", "Format output using a Go template.")
|
stackListCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
||||||
viper.BindPFlag("stack.list.endpoint", stackListCmd.Flags().Lookup("endpoint"))
|
viper.BindPFlag("stack.list.endpoint", stackListCmd.Flags().Lookup("endpoint"))
|
||||||
viper.BindPFlag("stack.list.format", stackListCmd.Flags().Lookup("format"))
|
viper.BindPFlag("stack.list.format", stackListCmd.Flags().Lookup("format"))
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -28,15 +29,9 @@ var statusCmd = &cobra.Command{
|
|||||||
respBody, err := client.GetStatus()
|
respBody, err := client.GetStatus()
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
if viper.GetString("status.format") != "" {
|
switch viper.GetString("status.format") {
|
||||||
// Print stack fields formatted
|
case "table":
|
||||||
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
|
// Print status in a table format
|
||||||
common.CheckError(templateParsingErr)
|
|
||||||
templateExecutionErr := template.Execute(os.Stdout, respBody)
|
|
||||||
common.CheckError(templateExecutionErr)
|
|
||||||
fmt.Println()
|
|
||||||
} else {
|
|
||||||
// Print status fields as a table
|
|
||||||
writer, newTabWriterErr := common.NewTabWriter([]string{
|
writer, newTabWriterErr := common.NewTabWriter([]string{
|
||||||
"VERSION",
|
"VERSION",
|
||||||
"AUTHENTICATION",
|
"AUTHENTICATION",
|
||||||
@ -56,6 +51,18 @@ var statusCmd = &cobra.Command{
|
|||||||
|
|
||||||
flushErr := writer.Flush()
|
flushErr := writer.Flush()
|
||||||
common.CheckError(flushErr)
|
common.CheckError(flushErr)
|
||||||
|
case "json":
|
||||||
|
// Print status in a json format
|
||||||
|
statusJsonBytes, err := json.Marshal(respBody)
|
||||||
|
common.CheckError(err)
|
||||||
|
fmt.Println(string(statusJsonBytes))
|
||||||
|
default:
|
||||||
|
// Print status in a custom format
|
||||||
|
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
|
||||||
|
common.CheckError(templateParsingErr)
|
||||||
|
templateExecutionErr := template.Execute(os.Stdout, respBody)
|
||||||
|
common.CheckError(templateExecutionErr)
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -63,7 +70,7 @@ var statusCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(statusCmd)
|
rootCmd.AddCommand(statusCmd)
|
||||||
|
|
||||||
statusCmd.Flags().String("format", "", "Format output using a Go template.")
|
statusCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
||||||
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
|
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
|
||||||
|
|
||||||
statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{}))
|
statusCmd.SetUsageTemplate(statusCmd.UsageTemplate() + common.GetFormatHelp(portainer.Status{}))
|
||||||
|
Loading…
Reference in New Issue
Block a user