mirror of https://github.com/portainer/portainer
62 lines
2.3 KiB
Go
62 lines
2.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"github.com/portainer/portainer"
|
|
|
|
"os"
|
|
"strings"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
)
|
|
|
|
// Service implements the CLIService interface
|
|
type Service struct{}
|
|
|
|
const (
|
|
errInvalidEnpointProtocol = portainer.Error("Invalid endpoint protocol: Portainer only supports unix:// or tcp://")
|
|
errSocketNotFound = portainer.Error("Unable to locate Unix socket")
|
|
)
|
|
|
|
// ParseFlags parse the CLI flags and return a portainer.Flags struct
|
|
func (*Service) ParseFlags(version string) (*portainer.CLIFlags, error) {
|
|
kingpin.Version(version)
|
|
|
|
flags := &portainer.CLIFlags{
|
|
Endpoint: kingpin.Flag("host", "Dockerd endpoint").Short('H').String(),
|
|
Logo: kingpin.Flag("logo", "URL for the logo displayed in the UI").String(),
|
|
Labels: pairs(kingpin.Flag("hide-label", "Hide containers with a specific label in the UI").Short('l')),
|
|
Addr: kingpin.Flag("bind", "Address and port to serve Portainer").Default(defaultBindAddress).Short('p').String(),
|
|
Assets: kingpin.Flag("assets", "Path to the assets").Default(defaultAssetsDirectory).Short('a').String(),
|
|
Data: kingpin.Flag("data", "Path to the folder where the data is stored").Default(defaultDataDirectory).Short('d').String(),
|
|
Templates: kingpin.Flag("templates", "URL to the templates (apps) definitions").Default(defaultTemplatesURL).Short('t').String(),
|
|
TLSVerify: kingpin.Flag("tlsverify", "TLS support").Default(defaultTLSVerify).Bool(),
|
|
TLSCacert: kingpin.Flag("tlscacert", "Path to the CA").Default(defaultTLSCACertPath).String(),
|
|
TLSCert: kingpin.Flag("tlscert", "Path to the TLS certificate file").Default(defaultTLSCertPath).String(),
|
|
TLSKey: kingpin.Flag("tlskey", "Path to the TLS key").Default(defaultTLSKeyPath).String(),
|
|
}
|
|
|
|
kingpin.Parse()
|
|
return flags, nil
|
|
}
|
|
|
|
// ValidateFlags validates the values of the flags.
|
|
func (*Service) ValidateFlags(flags *portainer.CLIFlags) error {
|
|
if *flags.Endpoint != "" {
|
|
if !strings.HasPrefix(*flags.Endpoint, "unix://") && !strings.HasPrefix(*flags.Endpoint, "tcp://") {
|
|
return errInvalidEnpointProtocol
|
|
}
|
|
|
|
if strings.HasPrefix(*flags.Endpoint, "unix://") {
|
|
socketPath := strings.TrimPrefix(*flags.Endpoint, "unix://")
|
|
if _, err := os.Stat(socketPath); err != nil {
|
|
if os.IsNotExist(err) {
|
|
return errSocketNotFound
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|