2019-08-17 20:29:39 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/greenled/portainer-stack-utils/common"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// endpointInspectCmd represents the endpoint inspect command
|
|
|
|
var endpointInspectCmd = &cobra.Command{
|
2019-08-17 21:20:31 +00:00
|
|
|
Use: "inspect <name>",
|
2019-08-17 20:29:39 +00:00
|
|
|
Short: "Inspect an endpoint",
|
2019-08-17 21:18:44 +00:00
|
|
|
Example: " psu endpoint inspect primary",
|
2019-08-17 20:29:39 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
var endpoint portainer.Endpoint
|
|
|
|
if len(args) == 1 {
|
|
|
|
var endpointRetrievalErr error
|
|
|
|
endpoint, endpointRetrievalErr = common.GetEndpointByName(args[0])
|
|
|
|
common.CheckError(endpointRetrievalErr)
|
|
|
|
} else {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"implications": "Command will fail if there is not exactly one endpoint available",
|
|
|
|
}).Warning("Endpoint not set")
|
|
|
|
var endpointRetrievalErr error
|
|
|
|
endpoint, endpointRetrievalErr = common.GetDefaultEndpoint()
|
|
|
|
common.CheckError(endpointRetrievalErr)
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"endpoint": endpoint.Name,
|
|
|
|
}).Debug("Using the only available endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch viper.GetString("endpoint.inspect.format") {
|
|
|
|
case "table":
|
|
|
|
// Print endpoint in a table format
|
|
|
|
writer, err := common.NewTabWriter([]string{
|
|
|
|
"ID",
|
|
|
|
"NAME",
|
|
|
|
"TYPE",
|
|
|
|
"URL",
|
|
|
|
"PUBLIC URL",
|
|
|
|
"GROUP ID",
|
|
|
|
})
|
|
|
|
common.CheckError(err)
|
|
|
|
var endpointType string
|
2019-08-17 23:13:13 +00:00
|
|
|
if endpoint.Type == portainer.DockerEnvironment {
|
2019-08-17 20:29:39 +00:00
|
|
|
endpointType = "docker"
|
2019-08-17 23:13:13 +00:00
|
|
|
} else if endpoint.Type == portainer.AgentOnDockerEnvironment {
|
|
|
|
endpointType = "agent on docker"
|
|
|
|
} else if endpoint.Type == portainer.AzureEnvironment {
|
|
|
|
endpointType = "azure"
|
|
|
|
} else if endpoint.Type == portainer.EdgeAgentEnvironment {
|
|
|
|
endpointType = "edge agent"
|
2019-08-17 20:29:39 +00:00
|
|
|
}
|
|
|
|
_, err = fmt.Fprintln(writer, fmt.Sprintf(
|
|
|
|
"%v\t%s\t%v\t%s\t%s\t%v",
|
|
|
|
endpoint.ID,
|
|
|
|
endpoint.Name,
|
|
|
|
endpointType,
|
|
|
|
endpoint.URL,
|
|
|
|
endpoint.PublicURL,
|
|
|
|
endpoint.GroupID,
|
|
|
|
))
|
|
|
|
common.CheckError(err)
|
|
|
|
err = writer.Flush()
|
|
|
|
common.CheckError(err)
|
|
|
|
case "json":
|
|
|
|
// Print endpoint in a json format
|
2019-08-23 16:11:03 +00:00
|
|
|
endpointJSONBytes, err := json.Marshal(endpoint)
|
2019-08-17 20:29:39 +00:00
|
|
|
common.CheckError(err)
|
2019-08-23 16:11:03 +00:00
|
|
|
fmt.Println(string(endpointJSONBytes))
|
2019-08-17 20:29:39 +00:00
|
|
|
default:
|
|
|
|
// Print endpoint in a custom format
|
|
|
|
template, err := template.New("endpointTpl").Parse(viper.GetString("endpoint.inspect.format"))
|
|
|
|
common.CheckError(err)
|
|
|
|
err = template.Execute(os.Stdout, endpoint)
|
|
|
|
common.CheckError(err)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
endpointCmd.AddCommand(endpointInspectCmd)
|
|
|
|
|
|
|
|
endpointInspectCmd.Flags().String("format", "table", `Output format. Can be "table", "json" or a Go template.`)
|
|
|
|
viper.BindPFlag("endpoint.inspect.format", endpointInspectCmd.Flags().Lookup("format"))
|
|
|
|
|
|
|
|
endpointInspectCmd.SetUsageTemplate(endpointInspectCmd.UsageTemplate() + common.GetFormatHelp(portainer.Endpoint{}))
|
|
|
|
}
|