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+
2397func (c * DockerClient ) StopContainer (ctx context.Context , stopId string ) {
2498 mutation := `
2599 mutation Stop($stopId: PrefixedID!) {
0 commit comments