diff --git a/Dockerfile.Debug b/Dockerfile.Debug index a7a62f2..02ec0d5 100644 --- a/Dockerfile.Debug +++ b/Dockerfile.Debug @@ -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"] \ No newline at end of file diff --git a/main.go b/main.go index e96c8ca..9cffe0e 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,10 @@ func main() { Name: "challenge-response", Action: actions.ChallengeResponse, }, + { + Name: "list-machines", + Action: actions.ListMachines, + }, { Name: "remove-machine", Action: actions.RemoveMachine, diff --git a/pkg/actions/list-machines.go b/pkg/actions/list-machines.go new file mode 100644 index 0000000..15ae026 --- /dev/null +++ b/pkg/actions/list-machines.go @@ -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 +}