From 3c2ef909e863fae50e71c9e23d4d7bab9cc23b7e Mon Sep 17 00:00:00 2001 From: Tomy Guichard Date: Fri, 9 Aug 2024 14:08:07 +0200 Subject: [PATCH] Add livebox-cli (#4) --- .github/workflows/go.yml | 46 +++++++++++++++++++++++++++++ README.md | 27 +++++++++++++++++ cmd/livebox-cli/main.go | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 .github/workflows/go.yml create mode 100644 cmd/livebox-cli/main.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..bde77d1 --- /dev/null +++ b/.github/workflows/go.yml @@ -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/* \ No newline at end of file diff --git a/README.md b/README.md index 77bffe6..ee4343c 100644 --- a/README.md +++ b/README.md @@ -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. | | diff --git a/cmd/livebox-cli/main.go b/cmd/livebox-cli/main.go new file mode 100644 index 0000000..64c8a09 --- /dev/null +++ b/cmd/livebox-cli/main.go @@ -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), ¶meters); err != nil { + return nil, fmt.Errorf("failed to unmarshal params: %w", err) + } + } + + return &request.Request{ + Service: service, + Method: method, + Parameters: parameters, + }, nil +}