Skip to content

Commit

Permalink
list machines
Browse files Browse the repository at this point in the history
  • Loading branch information
therealpaulgg committed Mar 15, 2023
1 parent b52a701 commit fa20f77
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Dockerfile.Debug
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# use ubuntu image as base and copy go code
FROM ubuntu
RUN apt update
RUN apt install -y git golang
FROM golang:buster
RUN mkdir /app
COPY ./pkg/ /app/pkg
COPY . /app
WORKDIR /app
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
RUN go build -o /app/main /app/main.go
CMD ["/bin/bash"]
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func main() {
Name: "challenge-response",
Action: actions.ChallengeResponse,
},
{
Name: "list-machines",
Action: actions.ListMachines,
},
{
Name: "remove-machine",
Action: actions.RemoveMachine,
Expand Down
55 changes: 55 additions & 0 deletions pkg/actions/list-machines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package actions

import (
"encoding/json"
"fmt"
"net/http"
"os"

"github.com/therealpaulgg/ssh-sync/pkg/dto"
"github.com/therealpaulgg/ssh-sync/pkg/utils"
"github.com/urfave/cli/v2"
)

func ListMachines(c *cli.Context) error {
setup, err := checkIfSetup()
if err != nil {
return err
}
if !setup {
fmt.Fprintln(os.Stderr, "ssh-sync has not been set up on this system. Please set up before continuing.")
return nil
}
profile, err := utils.GetProfile()
if err != nil {
return err
}
url := profile.ServerUrl
url.Path = "/api/v1/machines/"
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return err
}
token, err := utils.GetToken()
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
machines := []dto.MachineDto{}
err = json.NewDecoder(resp.Body).Decode(&machines)
if err != nil {
return err
}
for _, machine := range machines {
fmt.Println(machine.Name)
}
return nil
}

0 comments on commit fa20f77

Please sign in to comment.