Skip to content

Commit 2cf965a

Browse files
dploegerDennis Ploeger
authored andcommitted
feat: Better support for podman and optimized argument parsing
1 parent d3fbb07 commit 2cf965a

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

cmd/ccmanager.go

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,24 @@ import (
66
"ccmanager/internal/adapters"
77
"ccmanager/internal/models"
88
"fmt"
9-
"github.com/akamensky/argparse"
9+
"github.com/alexflint/go-arg"
1010
"github.com/charmbracelet/bubbles/list"
1111
tea "github.com/charmbracelet/bubbletea"
12-
"log"
1312
"os"
14-
"strings"
1513
)
1614

1715
func main() {
18-
parser := argparse.NewParser("ccmanager", "CloudControl instance manager")
19-
basePath := parser.StringList("b", "basepath", &argparse.Options{
20-
Help: "Paths where to find CloudControl docker compose folders. Can be set using a comma separated list in CCMANAGER_BASEPATH",
21-
Required: false,
22-
})
23-
24-
if err := parser.Parse(os.Args); err != nil {
25-
log.Fatal(parser.Usage(err))
26-
}
27-
28-
if len(*basePath) == 0 {
29-
if e, found := os.LookupEnv("CCMANAGER_BASEPATH"); found {
30-
*basePath = strings.Split(e, ",")
31-
}
32-
}
33-
34-
if len(*basePath) == 0 {
35-
log.Fatal(parser.Usage("No basepath given. Please use CCMANAGER_BASEPATH or the --basepath argument"))
16+
var args struct {
17+
BasePath []string `arg:"required,env:CCMANAGER_BASEPATH,separate" help:"Paths where to find CloudControl docker compose folders"`
18+
ContainerSeparator string `default:"-" arg:"env:CCMANAGER_SEP" help:"Separator used in docker compose container names"`
3619
}
20+
arg.MustParse(&args)
3721

3822
var items []list.Item
3923

4024
d := adapters.DockerAdapter{}
4125

42-
program := tea.NewProgram(models.NewMainModel(&d, *basePath, items))
26+
program := tea.NewProgram(models.NewMainModel(&d, args.BasePath, items))
4327
if _, err := program.Run(); err != nil {
4428
fmt.Println("Error running program:", err)
4529
os.Exit(1)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ require (
3636
github.com/Masterminds/semver/v3 v3.2.1 // indirect
3737
github.com/Microsoft/go-winio v0.6.1 // indirect
3838
github.com/Microsoft/hcsshim v0.11.4 // indirect
39+
github.com/alexflint/go-arg v1.5.1 // indirect
40+
github.com/alexflint/go-scalar v1.2.0 // indirect
3941
github.com/atotto/clipboard v0.1.4 // indirect
4042
github.com/aws/aws-sdk-go-v2 v1.17.6 // indirect
4143
github.com/aws/aws-sdk-go-v2/config v1.18.16 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn
2929
github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
3030
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
3131
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
32+
github.com/alexflint/go-arg v1.5.1 h1:nBuWUCpuRy0snAG+uIJ6N0UvYxpxA0/ghA/AaHxlT8Y=
33+
github.com/alexflint/go-arg v1.5.1/go.mod h1:A7vTJzvjoaSTypg4biM5uYNTkJ27SkNTArtYXnlqVO8=
34+
github.com/alexflint/go-scalar v1.2.0 h1:WR7JPKkeNpnYIOfHRa7ivM21aWAdHD0gEWHCx+WQBRw=
35+
github.com/alexflint/go-scalar v1.2.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
3236
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092 h1:aM1rlcoLz8y5B2r4tTLMiVTrMtpfY0O8EScKJxaSaEc=
3337
github.com/anchore/go-struct-converter v0.0.0-20221118182256-c68fdcfa2092/go.mod h1:rYqSE9HbjzpHTI74vwPvae4ZVYZd1lue2ta6xHPdblA=
3438
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=

internal/adapters/docker.go

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type DockerAdapter struct {
3737
}
3838

3939
func (d *DockerAdapter) GetContainerStatus(basePath string, name string) (CloudControlStatus, error) {
40-
containerName := fmt.Sprintf("%s_cli_1", name)
40+
containerName := fmt.Sprintf("%s%scli%s1", name, api.Separator, api.Separator)
4141
c := d.getClient()
4242
notFound := regexp.MustCompile("No such container")
4343
if i, err := c.ContainerInspect(context.Background(), containerName); err != nil {
@@ -86,7 +86,7 @@ func (d *DockerAdapter) GetContainerStatus(basePath string, name string) (CloudC
8686
}
8787

8888
func (d *DockerAdapter) RunCloudControl(_ string, name string, consoleWidth uint, consoleHeight uint) (*ContainerExec, error) {
89-
containerName := fmt.Sprintf("%s_cli_1", name)
89+
containerName := fmt.Sprintf("%s%scli%s1", name, api.Separator, api.Separator)
9090
consoleSize := [2]uint{consoleHeight, consoleWidth}
9191
return &ContainerExec{
9292
exec: func(stdin io.Reader, stdout io.Writer) error {
@@ -135,65 +135,74 @@ func (d *DockerAdapter) RunCloudControl(_ string, name string, consoleWidth uint
135135
}
136136

137137
go func(c net.Conn) {
138+
active := true
138139
s := bufio.NewReader(c)
139140
for {
140141
select {
141142
case <-quitWriter:
142143
return
143144
default:
144-
if b, err := s.ReadByte(); err != nil {
145-
logrus.Errorf("error reading from Docker: %s", err)
146-
return
147-
} else {
148-
if _, err := stdout.Write([]byte{b}); err != nil {
149-
logrus.Errorf("error writing to stdout: %s", err)
145+
if active {
146+
if b, err := s.ReadByte(); err != nil {
147+
logrus.Errorf("error reading from Docker: %s", err)
148+
active = false
149+
} else {
150+
if _, err := stdout.Write([]byte{b}); err != nil {
151+
logrus.Errorf("error writing to stdout: %s", err)
152+
}
150153
}
151154
}
152155
}
153156
}
154157
}(execResponse.Conn)
155158

156159
go func(c net.Conn) {
160+
active := true
157161
s := bufio.NewReader(stdin)
158162
for {
159163
select {
160164
case <-quitReader:
161165
return
162166
default:
163-
if b, err := s.ReadByte(); err != nil {
164-
logrus.Errorf("error reading from Docker: %s", err)
165-
return
166-
} else {
167-
if _, err := c.Write([]byte{b}); err != nil {
168-
logrus.Errorf("error writing to Docker: %s", err)
169-
return
167+
if active {
168+
if b, err := s.ReadByte(); err != nil {
169+
logrus.Errorf("error reading from Docker: %s", err)
170+
active = false
171+
} else {
172+
if _, err := c.Write([]byte{b}); err != nil {
173+
logrus.Errorf("error writing to Docker: %s", err)
174+
active = false
175+
}
170176
}
171177
}
172178
}
173179
}
174180
}(execResponse.Conn)
175181

176182
go func() {
183+
active := true
177184
width := consoleWidth
178185
height := consoleHeight
179186
for {
180187
select {
181188
case <-quitTermResize:
182189
return
183190
default:
184-
if w, err := term.GetWinsize(fd); err != nil {
185-
logrus.Errorf("error getting terminal size: %s", err)
186-
return
187-
} else {
188-
if w.Width != uint16(width) || w.Height != uint16(height) {
189-
width = uint(w.Width)
190-
height = uint(w.Height)
191-
if err := dockerCli.ContainerExecResize(context.Background(), executeID, types.ResizeOptions{
192-
Width: width,
193-
Height: height,
194-
}); err != nil {
195-
logrus.Errorf("error resizing container terminal: %s", err)
196-
return
191+
if active {
192+
if w, err := term.GetWinsize(fd); err != nil {
193+
logrus.Errorf("error getting terminal size: %s", err)
194+
active = false
195+
} else {
196+
if w.Width != uint16(width) || w.Height != uint16(height) {
197+
width = uint(w.Width)
198+
height = uint(w.Height)
199+
if err := dockerCli.ContainerExecResize(context.Background(), executeID, types.ResizeOptions{
200+
Width: width,
201+
Height: height,
202+
}); err != nil {
203+
logrus.Errorf("error resizing container terminal: %s", err)
204+
active = false
205+
}
197206
}
198207
}
199208
}
@@ -270,8 +279,6 @@ func (d *DockerAdapter) getClient() client.Client {
270279
func (d *DockerAdapter) getComposeBackend() api.Service {
271280
if d.composeBackend == nil {
272281
cl := d.getClient()
273-
// ensure old docker-compose compatibility
274-
api.Separator = "_"
275282
if c, err := command.NewDockerCli(command.WithAPIClient(&cl), command.WithDefaultContextStoreConfig()); err != nil {
276283
panic(fmt.Sprintf("Can not connect to Docker API: %s", err.Error()))
277284
} else {

0 commit comments

Comments
 (0)