Skip to content

Commit

Permalink
feat: command add more
Browse files Browse the repository at this point in the history
- cmd add init command
- root cmd add more flag and ailas
- config add version
  • Loading branch information
zeromake committed Mar 20, 2019
1 parent 8382421 commit 778f649
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -73,6 +73,7 @@ config_default = "default"
- [x] add changelog
- [x] add README_CN.md
- [ ] add brew package
- [ ] docker-debug version manage config file

## Details
1. find image docker is has, not has pull the image.
Expand Down
27 changes: 21 additions & 6 deletions internal/command/cli.go
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/zeromake/docker-debug/internal/config"
"github.com/zeromake/docker-debug/pkg/tty"
"github.com/zeromake/docker-debug/pkg/opts"
"github.com/zeromake/moby/api/types"
"github.com/zeromake/moby/api/types/container"
"github.com/zeromake/moby/api/types/filters"
Expand All @@ -17,7 +19,7 @@ import (
"strings"
"time"

"github.com/zeromake/docker-debug/cmd/version"
"github.com/zeromake/docker-debug/version"
"github.com/zeromake/docker-debug/pkg/stream"
"github.com/zeromake/moby/client"
"github.com/zeromake/moby/pkg/term"
Expand Down Expand Up @@ -110,18 +112,26 @@ func WithClientConfig(dockerConfig config.DockerConfig) DebugCliOption {
return errors.WithStack(err)
}
}
opts := []func(*client.Client) error{
client.WithHost(dockerConfig.Host),
var (
host string
err error
)
host, err = opts.ValidateHost(dockerConfig.Host)
if err != nil {
return err
}
clientOpts := []func(*client.Client) error {
client.WithHost(host),
client.WithVersion(""),
}
if dockerConfig.TLS {
opts = append(opts, client.WithTLSClientConfig(
clientOpts = append(clientOpts, client.WithTLSClientConfig(
fmt.Sprintf("%s%s%s", dockerConfig.CertDir, config.PathSeparator, caKey),
fmt.Sprintf("%s%s%s", dockerConfig.CertDir, config.PathSeparator, certKey),
fmt.Sprintf("%s%s%s", dockerConfig.CertDir, config.PathSeparator, keyKey),
))
}
dockerClient, err := client.NewClientWithOpts(opts...)
dockerClient, err := client.NewClientWithOpts(clientOpts...)
if err != nil {
return errors.WithStack(err)
}
Expand Down Expand Up @@ -189,7 +199,12 @@ func (cli *DebugCli) PullImage(image string) error {
return err
}

defer responseBody.Close()
defer func() {
err = responseBody.Close()
if err != nil {
logrus.Debugf("%+v", err)
}
}()
return jsonmessage.DisplayJSONMessagesToStream(responseBody, cli.out, nil)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/command/info.go
Expand Up @@ -3,7 +3,7 @@ package command
import (
"fmt"
"github.com/spf13/cobra"
"github.com/zeromake/docker-debug/cmd/version"
"github.com/zeromake/docker-debug/version"
)

func init() {
Expand All @@ -20,4 +20,4 @@ func init() {
},
}
rootCmd.AddCommand(cmd)
}
}
19 changes: 19 additions & 0 deletions internal/command/init.go
@@ -0,0 +1,19 @@
package command

import (
"github.com/spf13/cobra"
"github.com/zeromake/docker-debug/internal/config"
)

func init() {
cmd := &cobra.Command{
Use: "init",
Short: "docker-debug init config",
Args: RequiresMinArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
_, err := config.InitConfig()
return err
},
}
rootCmd.AddCommand(cmd)
}
16 changes: 11 additions & 5 deletions internal/command/root.go
Expand Up @@ -19,6 +19,7 @@ type execOptions struct {
privileged bool
workdir string
container string
certDir string
command []string
}

Expand All @@ -43,18 +44,19 @@ func newExecCommand() *cobra.Command {
flags := cmd.Flags()
flags.SetInterspersed(false)

flags.StringVarP(&options.image, "image", "", "", "use this image")
flags.StringVarP(&options.host, "host", "", "", "conn this host's docker (format: tcp://192.168.99.100:2376)")
flags.StringVarP(&options.detachKeys, "detach-keys", "", "", "Override the key sequence for detaching a container")
flags.StringVarP(&options.image, "image", "i", "", "use this image")
flags.StringVarP(&options.host, "host", "H", "", "connection host's docker (format: tcp://192.168.99.100:2376)")
flags.StringVarP(&options.certDir, "cert-dir", "c", "", "cert dir use tls")
flags.StringVarP(&options.detachKeys, "detach-keys", "d", "", "Override the key sequence for detaching a container")
flags.StringVarP(&options.user, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
flags.BoolVarP(&options.privileged, "privileged", "", false, "Give extended privileges to the command")
flags.BoolVarP(&options.privileged, "privileged", "p", false, "Give extended privileges to the command")
flags.StringVarP(&options.workdir, "workdir", "w", "", "Working directory inside the container")
_ = flags.SetAnnotation("workdir", "version", []string{"1.35"})
return cmd
}

func runExec(options execOptions) error {
//logrus.SetLevel(logrus.DebugLevel)
logrus.SetLevel(logrus.ErrorLevel)
var containerId string
conf, err := config.LoadConfig()
opts := []DebugCliOption{
Expand All @@ -67,6 +69,10 @@ func runExec(options execOptions) error {
dockerConfig := config.DockerConfig{
Host: options.host,
}
if options.certDir != "" {
dockerConfig.TLS = true
dockerConfig.CertDir = options.certDir
}
opts = append(opts, WithClientConfig(dockerConfig))
} else {
opts = append(opts, WithClientName(conf.DockerConfigDefault))
Expand Down
22 changes: 10 additions & 12 deletions internal/config/config.go
Expand Up @@ -3,12 +3,13 @@ package config
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/zeromake/docker-debug/pkg/opts"
"github.com/zeromake/docker-debug/version"
"os"
"strings"
"time"
"github.com/mitchellh/go-homedir"
)

var configDir = ".docker-debug"
Expand All @@ -29,7 +30,7 @@ var ConfigFile = fmt.Sprintf(
func init() {
var (
home string
err error
err error
)
home, err = homedir.Dir()
if err != nil {
Expand All @@ -43,21 +44,21 @@ func init() {
// DockerConfig docker 配置
type DockerConfig struct {
Host string `toml:"host"`
TLS bool `toml:"tls"`
TLS bool `toml:"tls"`
CertDir string `toml:"cert_dir"`
CertPassword string `toml:"cert_password"`
}

// Config 配置
type Config struct {
Image string `toml:"image"`
//Command []string
Version string `toml:"version"`
Image string `toml:"image"`
Timeout time.Duration `toml:"timeout"`
DockerConfigDefault string `toml:"config_default"`
DockerConfig map[string]DockerConfig `toml:"config"`
}

func pathExists(path string) bool {
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
Expand All @@ -69,7 +70,7 @@ func pathExists(path string) bool {
}

func LoadConfig() (*Config, error) {
if !pathExists(ConfigFile) {
if !PathExists(ConfigFile) {
return InitConfig()
}
config := &Config{}
Expand All @@ -82,7 +83,7 @@ func InitConfig() (*Config, error) {
if err != nil {
return nil, errors.WithStack(err)
}
if !pathExists(configDir) {
if !PathExists(configDir) {
err = os.Mkdir(configDir, 0755)
if err != nil {
return nil, errors.WithStack(err)
Expand All @@ -102,11 +103,8 @@ func InitConfig() (*Config, error) {
dc.CertDir = strings.Join(paths, PathSeparator)
}
config := &Config{
Version: version.Version,
Image: "nicolaka/netshoot:latest",
//Command: []string{
// "sleep",
// "24h",gaodingx_mysql_1
//},frapsoft/htop
Timeout: time.Second * 10,
DockerConfigDefault: "default",
DockerConfig: map[string]DockerConfig{
Expand Down
13 changes: 7 additions & 6 deletions pkg/opts/hosts.go
Expand Up @@ -2,6 +2,7 @@ package opts

import (
"fmt"
"github.com/pkg/errors"
"net"
"net/url"
"strconv"
Expand Down Expand Up @@ -87,7 +88,7 @@ func parseDockerDaemonHost(addr string) (string, error) {
case "ssh":
return addr, nil
default:
return "", fmt.Errorf("Invalid bind address format: %s", addr)
return "", errors.Errorf("Invalid bind address format: %s", addr)
}
}

Expand All @@ -98,7 +99,7 @@ func parseDockerDaemonHost(addr string) (string, error) {
func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) {
addr = strings.TrimPrefix(addr, proto+"://")
if strings.Contains(addr, "://") {
return "", fmt.Errorf("Invalid proto, expected %s: %s", proto, addr)
return "", errors.Errorf("Invalid proto, expected %s: %s", proto, addr)
}
if addr == "" {
addr = defaultAddr
Expand All @@ -117,13 +118,13 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
}
addr := strings.TrimPrefix(tryAddr, "tcp://")
if strings.Contains(addr, "://") || addr == "" {
return "", fmt.Errorf("Invalid proto, expected tcp: %s", tryAddr)
return "", errors.Errorf("Invalid proto, expected tcp: %s", tryAddr)
}

defaultAddr = strings.TrimPrefix(defaultAddr, "tcp://")
defaultHost, defaultPort, err := net.SplitHostPort(defaultAddr)
if err != nil {
return "", err
return "", errors.WithStack(err)
}
// url.Parse fails for trailing colon on IPv6 brackets on Go 1.5, but
// not 1.4. See https://github.com/golang/go/issues/12200 and
Expand All @@ -134,7 +135,7 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {

u, err := url.Parse("tcp://" + addr)
if err != nil {
return "", err
return "", errors.WithStack(err)
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
Expand All @@ -153,7 +154,7 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
}
p, err := strconv.Atoi(port)
if err != nil && p == 0 {
return "", fmt.Errorf("Invalid bind address format: %s", tryAddr)
return "", errors.Errorf("Invalid bind address format: %s", tryAddr)
}

return fmt.Sprintf("tcp://%s%s", net.JoinHostPort(host, port), u.Path), nil
Expand Down
8 changes: 4 additions & 4 deletions scripts/variables.env
Expand Up @@ -5,16 +5,16 @@ BUILDTIME=${BUILDTIME:-$(date +'%Y-%m-%d %H:%M:%S %z')}

PLATFORM_LDFLAGS=
if test -n "${PLATFORM}"; then
PLATFORM_LDFLAGS="-X \"github.com/zeromake/docker-debug/cmd/version.PlatformName=${PLATFORM}\""
PLATFORM_LDFLAGS="-X \"github.com/zeromake/docker-debug/version.PlatformName=${PLATFORM}\""
fi

export LDFLAGS="\
-s \
-w \
${PLATFORM_LDFLAGS} \
-X \"github.com/zeromake/docker-debug/cmd/version.GitCommit=${GITCOMMIT}\" \
-X \"github.com/zeromake/docker-debug/cmd/version.BuildTime=${BUILDTIME}\" \
-X \"github.com/zeromake/docker-debug/cmd/version.Version=${VERSION}\" \
-X \"github.com/zeromake/docker-debug/version.GitCommit=${GITCOMMIT}\" \
-X \"github.com/zeromake/docker-debug/version.BuildTime=${BUILDTIME}\" \
-X \"github.com/zeromake/docker-debug/version.Version=${VERSION}\" \
${LDFLAGS:-} \
"

Expand Down
File renamed without changes.

0 comments on commit 778f649

Please sign in to comment.