2019-07-21 02:00:04 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
2019-08-02 16:03:43 +00:00
|
|
|
|
|
|
|
"github.com/greenled/portainer-stack-utils/common"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2019-07-21 02:00:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// statusCmd represents the status command
|
|
|
|
var statusCmd = &cobra.Command{
|
|
|
|
Use: "status",
|
|
|
|
Short: "Check Portainer status",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-08-02 16:03:43 +00:00
|
|
|
client, err := common.GetClient()
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
2019-08-02 17:10:59 +00:00
|
|
|
respBody, err := client.GetStatus()
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
|
|
|
if viper.GetString("status.format") != "" {
|
|
|
|
// Print stack fields formatted
|
|
|
|
template, templateParsingErr := template.New("statusTpl").Parse(viper.GetString("status.format"))
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(templateParsingErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
templateExecutionErr := template.Execute(os.Stdout, respBody)
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(templateExecutionErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
fmt.Println()
|
|
|
|
} else {
|
|
|
|
// Print status fields as a table
|
2019-08-06 04:04:10 +00:00
|
|
|
writer, newTabWriterErr := common.NewTabWriter([]string{
|
2019-07-21 02:00:04 +00:00
|
|
|
"VERSION",
|
|
|
|
"AUTHENTICATION",
|
|
|
|
"ENDPOINT MANAGEMENT",
|
|
|
|
"ANALYTICS",
|
|
|
|
})
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(newTabWriterErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
|
|
|
_, printingErr := fmt.Fprintln(writer, fmt.Sprintf(
|
|
|
|
"%s\t%v\t%v\t%v",
|
|
|
|
respBody.Version,
|
|
|
|
respBody.Authentication,
|
|
|
|
respBody.EndpointManagement,
|
|
|
|
respBody.Analytics,
|
|
|
|
))
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(printingErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
|
|
|
flushErr := writer.Flush()
|
2019-08-03 13:51:11 +00:00
|
|
|
common.CheckError(flushErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(statusCmd)
|
|
|
|
|
|
|
|
statusCmd.Flags().String("format", "", "format output using a Go template")
|
|
|
|
viper.BindPFlag("status.format", statusCmd.Flags().Lookup("format"))
|
|
|
|
}
|