Skip to content

Commit

Permalink
introduce cli and gh actions for executables
Browse files Browse the repository at this point in the history
Signed-off-by: Jeeva Kandasamy <[email protected]>
  • Loading branch information
jkandasa committed Jun 15, 2024
1 parent 18233af commit 14a83ab
Show file tree
Hide file tree
Showing 20 changed files with 1,008 additions and 23 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: golangci-lint
on:
push:
branches: [main]
tags: ["v*"]
pull_request:

jobs:
golang-lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ^1.21

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
skip-go-installation: true
# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true
62 changes: 62 additions & 0 deletions .github/workflows/publish_executables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: publish executables
on:
push:
branches: [main]
tags: ["v*"]

jobs:
setup:
runs-on: ubuntu-latest

steps:
- name: checkout the source code
uses: actions/checkout@v2

- uses: actions/setup-go@v2
with:
go-version: ^1.21

- name: Cache go modules
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: build executable bundles
run: ./scripts/generate_executables.sh

- name: generate a build timestamp and sha256sum files
run: |
cd builds
echo `date -u +'%Y%m%d%H%M%S'` > ./build_timestamp.txt
echo `date -u +'%Y-%m-%dT%H:%M:%S%:z'` >> ./build_timestamp.txt
sha256sum *.tar.gz > ./SHA256SUMS.txt
sha256sum *.zip >> ./SHA256SUMS.txt
- name: update release notes and executables
if: startsWith(github.ref, 'refs/tags/') # executes only for new release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
with:
files: |
builds/*.tar.gz
builds/*.zip
builds/build_timestamp.txt
builds/SHA256SUMS.txt
- name: Update executables for main branch changes
if: startsWith(github.ref, 'refs/heads/main') # executes only for changes in main
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GH_TOKEN }}"
automatic_release_tag: development
prerelease: true
title: "Development Build - Pre Release"
files: |
builds/*.tar.gz
builds/*.zip
builds/build_timestamp.txt
builds/SHA256SUMS.txt
78 changes: 78 additions & 0 deletions cli/command/device/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package device

import (
"fmt"
"strings"

rootCmd "github.com/mycontroller-org/esphome_api/cli/command/root"
"github.com/mycontroller-org/server/v2/pkg/utils/printer"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(deviceContextCmd)
rootCmd.AddCommand(getDevicesCmd)
}

var deviceContextCmd = &cobra.Command{
Use: "device",
Short: "Switch or set a device",
Example: ` # set a node
esphomectl device my-device-1
# get the active device
esphomectl device
`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
if rootCmd.CONFIG.Active == "" {
fmt.Fprintln(cmd.OutOrStdout(), "No resource found")
return
}
fmt.Fprintf(cmd.ErrOrStderr(), "Active node '%s'\n", rootCmd.CONFIG.Active)
return
}
rootCmd.CONFIG.Active = strings.TrimSpace(args[0])
client, err := rootCmd.GetActiveClient(nil)
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "Error on login", err)
return
}
if client != nil {
rootCmd.WriteConfigFile()
fmt.Fprintf(cmd.OutOrStdout(), "Switched to '%s'\n", rootCmd.CONFIG.Active)
}
},
}

var getDevicesCmd = &cobra.Command{
Use: "devices",
Short: "Display configured devices",
Example: ` # display configured devices
esphomectl devices
`,
Run: func(cmd *cobra.Command, args []string) {
if len(rootCmd.CONFIG.Devices) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No resource found")
return
}
headers := []printer.Header{
{Title: "address", ValuePath: "address"},
{Title: "name", ValuePath: "info.name"},
{Title: "model", ValuePath: "info.model"},
{Title: "mac address", ValuePath: "info.macAddress"},
{Title: "version", ValuePath: "info.esphomeVersion"},
{Title: "compilation time", ValuePath: "info.compilationTime"},
{Title: "uses password", ValuePath: "info.usesPassword"},
{Title: "has deep sleep", ValuePath: "info.hasDeepSleep"},
{Title: "timeout", ValuePath: "timeout", IsWide: true},
{Title: "status on", ValuePath: "info.statusOn", DisplayStyle: printer.DisplayStyleRelativeTime},
}
data := make([]interface{}, 0)
for _, device := range rootCmd.CONFIG.Devices {
data = append(data, device)
}
printer.Print(cmd.OutOrStdout(), headers, data, rootCmd.HideHeader, rootCmd.OutputFormat, rootCmd.Pretty)
},
}
15 changes: 15 additions & 0 deletions cli/command/device/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package device

import (
rootCmd "github.com/mycontroller-org/esphome_api/cli/command/root"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(getCmd)
}

var getCmd = &cobra.Command{
Use: "get",
Short: "List resources",
}
117 changes: 117 additions & 0 deletions cli/command/device/get_entities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package device

import (
"fmt"
"strings"
"time"

rootCmd "github.com/mycontroller-org/esphome_api/cli/command/root"
"github.com/mycontroller-org/esphome_api/pkg/api"
"github.com/mycontroller-org/server/v2/pkg/utils/convertor"
filterUtils "github.com/mycontroller-org/server/v2/pkg/utils/filter_sort"
"github.com/mycontroller-org/server/v2/pkg/utils/printer"
"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"
)

var (
entitiesTimeout time.Duration
)

func init() {
getCmd.AddCommand(getEntitiesCmd)
getEntitiesCmd.Flags().DurationVar(&entitiesTimeout, "timeout", 5*time.Second, "Timeout to wait for entities")
}

var getEntitiesCmd = &cobra.Command{
Use: "entity",
Short: "Lists available entities",
Aliases: []string{"entities"},
Example: ` # lists available entities
esphomectl get entities
# list entities with a timeout
esphomectl get entities --timeout 10s
`,
Run: func(cmd *cobra.Command, args []string) {

entitiesCollectionDone := false
entities := map[string][]interface{}{}
collectEntities := func(msg proto.Message) {
switch entity := msg.(type) {
case *api.ListEntitiesDoneResponse:
entitiesCollectionDone = true

default:
_, _deviceClass, err := filterUtils.GetValueByKeyPath(entity, "deviceClass")
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "error:", err)
return
}
deviceClass := convertor.ToString(_deviceClass)
_sensors, found := entities[deviceClass]
if !found {
_sensors = make([]interface{}, 0)
}
_sensors = append(_sensors, entity)
entities[deviceClass] = _sensors
}
}

client, err := rootCmd.GetActiveClient(collectEntities)
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "error:", err.Error())
return
}

err = client.ListEntities()
if err != nil {
fmt.Fprintln(cmd.ErrOrStderr(), "error:", err.Error())
return
}

ticker := time.NewTicker(200 * time.Millisecond)
timeoutTime := time.Now().Add(entitiesTimeout)
for range ticker.C {
if entitiesCollectionDone || time.Now().Before(timeoutTime) {
break
}
}

if len(entities) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No resource found")
return
}

for k, _sensors := range entities {
fmt.Fprintln(cmd.OutOrStdout())
fmt.Fprintln(cmd.OutOrStdout(), strings.ToUpper(k))

switch k {
case "light":
headers := []printer.Header{
{Title: "name", ValuePath: "name"},
{Title: "object id", ValuePath: "objectId"},
{Title: "key", ValuePath: "key"},
{Title: "unique id", ValuePath: "uniqueId"},
{Title: "effects", ValuePath: "effects"},
{Title: "icon", ValuePath: "icon"},
}
printer.Print(cmd.OutOrStdout(), headers, _sensors, rootCmd.HideHeader, rootCmd.OutputFormat, rootCmd.Pretty)

default:
headers := []printer.Header{
{Title: "name", ValuePath: "name"},
{Title: "object id", ValuePath: "objectId"},
{Title: "key", ValuePath: "key"},
{Title: "unique id", ValuePath: "uniqueId"},
{Title: "device class", ValuePath: "deviceClass"},
}
printer.Print(cmd.OutOrStdout(), headers, _sensors, rootCmd.HideHeader, rootCmd.OutputFormat, rootCmd.Pretty)

}

}

},
}
15 changes: 15 additions & 0 deletions cli/command/device/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package device

import (
rootCmd "github.com/mycontroller-org/esphome_api/cli/command/root"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(setCmd)
}

var setCmd = &cobra.Command{
Use: "set",
Short: "update resource state",
}
28 changes: 28 additions & 0 deletions cli/command/device/set_entities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package device

import (
"github.com/spf13/cobra"
)

func init() {
setCmd.AddCommand(setEntitiesCmd)
}

var setEntitiesCmd = &cobra.Command{
Use: "entity",
Short: "Updates entity value",
Example: ` # lists available entities
esphomectl set entities unique_id --payload test=on
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// _client, err := rootCmd.GetActiveClient(nil)
// if err != nil {
// fmt.Fprintln(cmd.ErrOrStderr(), "error:", err.Error())
// return
// }
//
// var request proto.Message

},
}
Loading

0 comments on commit 14a83ab

Please sign in to comment.