Skip to content

Commit

Permalink
X
Browse files Browse the repository at this point in the history
  • Loading branch information
trajano committed Nov 14, 2023
1 parent b4d2e92 commit c9758fc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 31 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Wrapped Docker CLI
This wraps the Docker CLI so that it establishes saner defaults for my own needs.

## What this does

* `docker run` will set to logging to none and `-it` by default
* `docker ps` will actually call `docker inspect` then render the data using github.com/jedib0t/go-pretty.
* Primarily because I want to know WHEN the bloody container started,
* how long it took to start rather than about a minute ago.
* `docker service restart` maps to `docker service update --force`
* `docker service push <service> <image>` replaces the image of the service if image is not provided it pulls and then does the update to ensure it is the latest copy. It will also add `--with-registry-auth` as appropriate
* `docker service --help` should call `docker service --help` but append the extra commands

## Architecture

* Command design pattern
* The invoker uses a Composite design pattern to collect a list of commands which are in priority order. Each command has a "canHandle" method which if true will make the invoker set the command as the command to execute
* There's only one receiver though it can be mocked for testing
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
go build -o $HOME/.local/bin/docker-cli
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.4

require (
github.com/akamensky/argparse v1.4.0 // indirect
github.com/cristalhq/acmd v0.11.1 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.9 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc=
github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
github.com/cristalhq/acmd v0.11.1 h1:DJ4fh2Pv0nPKmqT646IU/0Vh5FNdGblxvF+3/W3NAUI=
github.com/cristalhq/acmd v0.11.1/go.mod h1:LG5oa43pE/BbxtfMoImHCQN++0Su7dzipdgBjMCBVDQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jedib0t/go-pretty/v6 v6.4.9 h1:vZ6bjGg2eBSrJn365qlxGcaWu09Id+LHtrfDWlB2Usc=
Expand Down
86 changes: 57 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"context"
"fmt"
"os"

"github.com/akamensky/argparse"
"github.com/cristalhq/acmd"
)

type CommandReceiver interface {
Expand Down Expand Up @@ -38,35 +39,62 @@ func ServiceCommandGroup() bool {
}

func main() {
parser := argparse.NewParser("docker-cli", "Wraps the Docker command")
var firstArgument = parser.StringPositional(&argparse.Options{Required: true})
err := parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
cmds := []acmd.Command{
{
Name: "ps",
Description: "docker ps with better formatting",
ExecFunc: func(ctx context.Context, args []string) error {
RunDockerCommand("ps", "--format", "table {{.Names}}\t{{.Image}}\t{{.Status}}")
return nil
},
},
{
Name: "ptag",
Description: "docker ps with better formatting",
ExecFunc: func(ctx context.Context, args []string) error {
RunDockerCommand("tag", args[0])
RunDockerCommand("push", args[1])
return nil
},
},
}
r := acmd.RunnerOf(cmds, acmd.Config{})
if err := r.Run(); err != nil {
RunDockerCommand(os.Args[1:]...)
} else {
fmt.Println("something" + *firstArgument)
//r.Exit(err)
}
// args := os.Args[1:]
fmt.Println("Bar")
}

// // load up the commands here
// github.com/cristalhq/acmd parser := argparse.NewParser("docker-cli", "Wraps the Docker command")
// var firstArgument = parser.StringPositional(&argparse.Options{Required: true})
// err := parser.Parse(os.Args)
// if err != nil {
// fmt.Print(parser.Usage(err))
// RunDockerCommand(os.Args[1:]...)
// } else {
// fmt.Println("something" + *firstArgument)
// }
// // args := os.Args[1:]

// switch {
// case len(args) == 0:
//
// fmt.Println("No command provided")
// os.Exit(1)
//
// case args[0] == "service" && ServiceCommandGroup():
//
// // Do nothing, as the service command is already handled in ServiceCommandGroup
//
// case args[0] == "ps":
//
// CmdPs(args...)
//
// default:
//
// RunDockerCommand(args...)
// }
}
// // // load up the commands here

// // switch {
// // case len(args) == 0:
// //
// // fmt.Println("No command provided")
// // os.Exit(1)
// //
// // case args[0] == "service" && ServiceCommandGroup():
// //
// // // Do nothing, as the service command is already handled in ServiceCommandGroup
// //
// // case args[0] == "ps":
// //
// // CmdPs(args...)
// //
// // default:
// //
// // RunDockerCommand(args...)
// // }
//}
8 changes: 6 additions & 2 deletions run_docker_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ func RunDockerCommand(args ...string) {

err := cmd.Run()
if err != nil {
fmt.Printf("Error running Docker command: %v\n", err)
os.Exit(1)
if exitErr, ok := err.(*exec.ExitError); ok {
os.Exit(exitErr.ExitCode())
} else {
fmt.Printf("Error running Docker command: %v\n", err)
os.Exit(1)
}
}
os.Exit(0)
}

0 comments on commit c9758fc

Please sign in to comment.