Skip to content

Commit 2ff6af1

Browse files
committed
feat(docker): list & start
1 parent 38d404e commit 2ff6af1

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

cmd/docker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ This application is a tool to generate the needed files
1919
to quickly create a Cobra application.`,
2020
Run: func(cmd *cobra.Command, args []string) {
2121
switch args[0] {
22+
case "list":
23+
unraidClient.Docker.ListContainers(cmd.Context())
2224
case "stop":
2325
unraidClient.Docker.StopContainer(cmd.Context(), args[1])
2426
case "start":
25-
// startContainer(cmd, args)
26-
case "restart":
27-
// restartContainer(cmd, args)
27+
unraidClient.Docker.StartContainer(cmd.Context(), args[1])
2828
}
2929
},
3030
}
3131

3232
func init() {
3333
rootCmd.AddCommand(dockerCmd)
3434

35-
dockerCmd.Args = cobra.MinimumNArgs(2)
36-
dockerCmd.ValidArgs = []string{"stop", "restart", "start"}
35+
dockerCmd.Args = cobra.MinimumNArgs(1)
36+
dockerCmd.ValidArgs = []string{"stop", "restart", "start", "list"}
3737
}

pkg/client/docker.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78

89
"github.com/machinebox/graphql"
910
)
@@ -20,6 +21,79 @@ func NewDockerClient(apiKey string, graphqlClient *graphql.Client) *DockerClient
2021
}
2122
}
2223

24+
func (c *DockerClient) ListContainers(ctx context.Context) {
25+
query := `
26+
query Query {
27+
docker {
28+
containers {
29+
id
30+
image
31+
state
32+
}
33+
}
34+
}`
35+
36+
req := graphql.NewRequest(query)
37+
req.Header.Set(ApiKeyHeader, c.ApiKey)
38+
39+
var respData struct {
40+
Docker struct {
41+
Containers []struct {
42+
ID string `json:"id"`
43+
Image string `json:"image"`
44+
State string `json:"state"`
45+
} `json:"containers"`
46+
} `json:"docker"`
47+
}
48+
49+
if err := c.GraphQLClient.Run(ctx, req, &respData); err != nil {
50+
log.Printf("Error query list containers: %v", err)
51+
return
52+
}
53+
54+
for _, container := range respData.Docker.Containers {
55+
idParts := strings.Split(container.ID, ":")
56+
compactID := idParts[len(idParts)-1]
57+
58+
if len(compactID) > 12 {
59+
compactID = compactID[:12]
60+
}
61+
62+
fmt.Printf("ID: %s | Image: %s | State: %s\n", compactID, container.Image, container.State)
63+
}
64+
}
65+
66+
func (c *DockerClient) StartContainer(ctx context.Context, startId string) {
67+
mutation := `
68+
mutation Mutation($startId: PrefixedID!) {
69+
docker {
70+
start(id: $startId) {
71+
id
72+
}
73+
}
74+
}`
75+
76+
req := graphql.NewRequest(mutation)
77+
req.Header.Set(ApiKeyHeader, c.ApiKey)
78+
79+
req.Var("startId", startId)
80+
81+
var respData struct {
82+
Docker struct {
83+
Start struct {
84+
ID string `json:"id"`
85+
} `json:"start"`
86+
} `json:"docker"`
87+
}
88+
89+
if err := c.GraphQLClient.Run(ctx, req, &respData); err != nil {
90+
log.Printf("Error mutation start: %v", err)
91+
return
92+
}
93+
94+
fmt.Printf("Container started with ID: %s\n", respData.Docker.Start.ID)
95+
}
96+
2397
func (c *DockerClient) StopContainer(ctx context.Context, stopId string) {
2498
mutation := `
2599
mutation Stop($stopId: PrefixedID!) {

0 commit comments

Comments
 (0)