Skip to content

Commit

Permalink
Add livebox-cli (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomy2e authored Aug 9, 2024
1 parent 60bf640 commit 3c2ef90
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Go

on:
push:
branches: [ "main" ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
goos: ["linux"]
goarch: ["amd64", "arm64"]
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
run: go build -o "livebox-cli-${GOOS}-${GOARCH}" cmd/livebox-cli/main.go
- uses: actions/upload-artifact@v4
with:
name: livebox-cli-${{ matrix.goos }}-${{ matrix.goarch }}
path: livebox-cli-${{ matrix.goos }}-${{ matrix.goarch }}
retention-days: 1
upload:
runs-on: ubuntu-latest
needs: build
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/download-artifact@v4
with:
path: artifacts
merge-multiple: true
- name: Release
uses: softprops/action-gh-release@v2
with:
files: |
artifacts/*
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,30 @@ _ = client.Request(

fmt.Println(string(r))
```

## Livebox CLI Usage

The `livebox-cli` tool allows to easily send requests to the Livebox API. It writes the JSON responses to stdout.

Pre-built binaries are available in the [Releases](https://github.com/Tomy2e/livebox-api-client/releases) section.
If you have Go installed, you can run it with:

```console
go run github.com/Tomy2e/livebox-api-client/cmd/livebox-cli
```

### Options

The tool accepts the following command-line options:

| Name | Description | Default value |
| -------- | ---------------------------- | ------------- |
| -service | Livebox service | |
| -method | Method to use | |
| -params | Optional JSON-encoded params | |

The tool reads the following environment variables:

| Name | Description | Default value |
| -------------- | ------------------------------------- | ------------- |
| ADMIN_PASSWORD | Password of the Livebox "admin" user. | |
63 changes: 63 additions & 0 deletions cmd/livebox-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"

"github.com/Tomy2e/livebox-api-client"
"github.com/Tomy2e/livebox-api-client/api/request"
)

func main() {
var (
service = flag.String("service", "", "service")
method = flag.String("method", "", "method")
params = flag.String("params", "", "JSON-encoded params")
)
flag.Parse()

client, err := livebox.NewClient(os.Getenv("ADMIN_PASSWORD"))
if err != nil {
log.Fatalf("failed to create livebox client: %s", err)
}

req, err := newRequest(*service, *method, *params)
if err != nil {
log.Fatalf("failed to create request: %s", err)
}

out := json.RawMessage{}
if err := client.Request(context.Background(), req, &out); err != nil {
log.Fatalf("request failed: %s", err)
}

fmt.Println(string(out))
}

func newRequest(service, method, params string) (*request.Request, error) {
if service == "" {
return nil, errors.New("-service is missing")
}

if method == "" {
return nil, errors.New("-method is missing")
}

var parameters request.Parameters
if params != "" {
if err := json.Unmarshal([]byte(params), &parameters); err != nil {
return nil, fmt.Errorf("failed to unmarshal params: %w", err)
}
}

return &request.Request{
Service: service,
Method: method,
Parameters: parameters,
}, nil
}

0 comments on commit 3c2ef90

Please sign in to comment.