2019-07-21 02:00:04 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2019 Juan Carlos Mejías Rodríguez <juan.mejias@reduc.edu.cu>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
2019-08-03 00:25:22 +00:00
|
|
|
"github.com/greenled/portainer-stack-utils/util"
|
|
|
|
|
2019-08-02 17:10:59 +00:00
|
|
|
"github.com/greenled/portainer-stack-utils/common"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2019-07-21 02:00:04 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// endpointListCmd represents the list command
|
|
|
|
var endpointListCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List endpoints",
|
|
|
|
Aliases: []string{"ls"},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-08-02 17:10:59 +00:00
|
|
|
client, err := common.GetClient()
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(err)
|
2019-08-02 17:10:59 +00:00
|
|
|
|
|
|
|
endpoints, err := client.GetEndpoints()
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
|
|
|
|
if viper.GetString("endpoint.list.format") != "" {
|
|
|
|
// Print endpoint fields formatted
|
|
|
|
template, templateParsingErr := template.New("endpointTpl").Parse(viper.GetString("endpoint.list.format"))
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(templateParsingErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
for _, e := range endpoints {
|
|
|
|
templateExecutionErr := template.Execute(os.Stdout, e)
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(templateExecutionErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Print all endpoint fields as a table
|
2019-08-03 00:25:22 +00:00
|
|
|
writer, err := util.NewTabWriter([]string{
|
2019-07-21 02:00:04 +00:00
|
|
|
"ENDPOINT ID",
|
|
|
|
"NAME",
|
|
|
|
"TYPE",
|
|
|
|
"URL",
|
|
|
|
"PUBLIC URL",
|
|
|
|
"GROUP ID",
|
|
|
|
})
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
for _, e := range endpoints {
|
|
|
|
var endpointType string
|
|
|
|
if e.Type == 1 {
|
|
|
|
endpointType = "docker"
|
|
|
|
} else if e.Type == 2 {
|
|
|
|
endpointType = "agent"
|
|
|
|
}
|
|
|
|
_, err := fmt.Fprintln(writer, fmt.Sprintf(
|
|
|
|
"%v\t%s\t%v\t%s\t%s\t%v",
|
|
|
|
e.Id,
|
|
|
|
e.Name,
|
|
|
|
endpointType,
|
|
|
|
e.URL,
|
|
|
|
e.PublicURL,
|
|
|
|
e.GroupID,
|
|
|
|
))
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(err)
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
flushErr := writer.Flush()
|
2019-08-03 00:25:22 +00:00
|
|
|
util.CheckError(flushErr)
|
2019-07-21 02:00:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
endpointCmd.AddCommand(endpointListCmd)
|
|
|
|
|
|
|
|
endpointListCmd.Flags().String("format", "", "format output using a Go template")
|
|
|
|
viper.BindPFlag("endpoint.list.format", endpointListCmd.Flags().Lookup("format"))
|
|
|
|
}
|