Skip to content

Commit fa20f77

Browse files
committed
list machines
1 parent b52a701 commit fa20f77

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

Dockerfile.Debug

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# use ubuntu image as base and copy go code
2-
FROM ubuntu
3-
RUN apt update
4-
RUN apt install -y git golang
1+
FROM golang:buster
52
RUN mkdir /app
63
COPY ./pkg/ /app/pkg
74
COPY . /app
85
WORKDIR /app
6+
ENV CGO_ENABLED=0
7+
ENV GOOS=linux
8+
ENV GOARCH=amd64
99
RUN go build -o /app/main /app/main.go
1010
CMD ["/bin/bash"]

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func main() {
3737
Name: "challenge-response",
3838
Action: actions.ChallengeResponse,
3939
},
40+
{
41+
Name: "list-machines",
42+
Action: actions.ListMachines,
43+
},
4044
{
4145
Name: "remove-machine",
4246
Action: actions.RemoveMachine,

pkg/actions/list-machines.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package actions
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
9+
"github.com/therealpaulgg/ssh-sync/pkg/dto"
10+
"github.com/therealpaulgg/ssh-sync/pkg/utils"
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
func ListMachines(c *cli.Context) error {
15+
setup, err := checkIfSetup()
16+
if err != nil {
17+
return err
18+
}
19+
if !setup {
20+
fmt.Fprintln(os.Stderr, "ssh-sync has not been set up on this system. Please set up before continuing.")
21+
return nil
22+
}
23+
profile, err := utils.GetProfile()
24+
if err != nil {
25+
return err
26+
}
27+
url := profile.ServerUrl
28+
url.Path = "/api/v1/machines/"
29+
req, err := http.NewRequest("GET", url.String(), nil)
30+
if err != nil {
31+
return err
32+
}
33+
token, err := utils.GetToken()
34+
if err != nil {
35+
return err
36+
}
37+
req.Header.Set("Authorization", "Bearer "+token)
38+
req.Header.Set("Content-Type", "application/json")
39+
resp, err := http.DefaultClient.Do(req)
40+
if err != nil {
41+
return err
42+
}
43+
if resp.StatusCode != http.StatusOK {
44+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
45+
}
46+
machines := []dto.MachineDto{}
47+
err = json.NewDecoder(resp.Body).Decode(&machines)
48+
if err != nil {
49+
return err
50+
}
51+
for _, machine := range machines {
52+
fmt.Println(machine.Name)
53+
}
54+
return nil
55+
}

0 commit comments

Comments
 (0)