Compare commits

...

130 Commits

Author SHA1 Message Date
dd1ea7c276 Make everything build strings instead of printing 2021-04-23 22:56:58 +00:00
8c9af693e7 Update go modules 2021-04-23 22:56:32 +00:00
23210af0c3 Add a command for validateColor API 2021-04-21 01:37:29 +00:00
8133953bb1 Add short flag for breathe color 2021-04-16 21:11:09 +00:00
8cc556024f Add revisions to packages 2021-04-16 21:09:38 +00:00
c01c342945 Fix errors in man page 2021-04-16 21:07:37 +00:00
8b232c5ef5 Add man page to packages
Refs #6
2021-04-15 02:01:28 +00:00
d6ca2d7921 Install man page
Refs #6
2021-04-15 01:54:18 +00:00
f79de85d43 Add a man page
Refs #6
2021-04-15 01:41:52 +00:00
05f445ddf2 Make output format flags boolean 2021-04-11 02:39:58 +00:00
baf7daa1bb Remove unused compat file 2021-04-10 07:25:00 +00:00
43b0c0a399 Add copyright to Debian package 2021-04-10 07:23:57 +00:00
7ac2cab082 Fix Debian package warnings 2021-04-10 07:23:25 +00:00
cee0af195e Running dpkg-source is not necessary 2021-04-10 04:03:01 +00:00
01a0601652 Suppress make clean rm errors when files are missing 2021-04-10 04:03:00 +00:00
ffe2c08a35 Ignore the build directory 2021-04-10 04:03:00 +00:00
5b1bdefacd Make RPM package 2021-04-10 04:02:59 +00:00
d38a5df36f Clean up Debian artifacts 2021-04-10 04:02:59 +00:00
55dd8d743d Move Debian packages to root of build directory 2021-04-10 04:02:59 +00:00
7e6249bc3c Improve Debian packaging by creating a source package 2021-04-10 03:02:08 +00:00
adbda33078 Separate make and build in Debian package 2021-04-10 02:54:50 +00:00
693ac6e677 Generate Debian package from template
Refs #7
2021-04-09 00:19:56 +00:00
953b1009d3 Fix PREFIX AND DESTDIR
Refs #7
2021-04-09 00:19:50 +00:00
11053571d5 Move debian files
Refs #7
2021-04-09 00:19:38 +00:00
86720c72f9 Initial Debian package
Refs #7
2021-04-08 05:38:02 +00:00
0df1524976 Use PREFIX and DESTDIR for make install
Refs #7
2021-04-08 05:37:54 +00:00
965b1b4339 Don't get LDFLAGS from the environment
Refs #7
2021-04-08 05:37:37 +00:00
a9936a0f1b Add make install 2021-04-07 01:38:00 +00:00
c229df8506 Change Makefile variable make 2021-04-07 01:37:19 +00:00
9c92d7945d Add visual power indicator character 2021-04-04 23:34:52 +00:00
a4c305c2e9 Fix bug where configuration is being overridden by a default configuration 2021-04-04 22:53:35 +00:00
e66f6358a9 Remove debug output from ls 2021-04-04 03:09:59 +00:00
100b585663 Add ~/.config/lume/lume.conf to configuration file search paths 2021-03-31 00:19:03 -05:00
f0cf3e12b6 Declare variables at the top of function 2021-03-31 00:18:00 -05:00
ce5f14db5d Fix access token validation
The access token was not be validated when there were additional
validations errors with the named colors
2021-03-31 00:13:49 -05:00
9b20413d64 Compile binary to ./bin 2021-03-30 23:57:52 -05:00
9c024454f2 Refactor global flags 2021-03-30 23:54:05 -05:00
05db35cdfe Remove global flags 2021-03-30 21:04:57 -05:00
bb95abdec6 Ignore lume binary 2021-03-30 21:01:07 -05:00
de93716b20 Fix context variable name 2021-03-30 13:33:36 -05:00
53bc7fa320 format 2021-03-30 13:31:26 -05:00
77c5f5ce18 Fix typo in Makefile 2021-03-30 13:27:58 -05:00
cf30eaae9b Update go modules 2021-03-29 17:37:00 -05:00
9338631de9 Add debug output support 2021-03-29 17:22:22 -05:00
a4638db773 Make configuration global, add helper functions 2021-03-29 17:21:12 -05:00
3c0254c9a1 Fix go.sum 2021-03-28 21:38:04 -05:00
b1a5844b6b Add support for parsing global flags 2021-03-28 21:33:43 -05:00
1ef900c44c Add breathe effect command 2021-03-25 09:46:58 -05:00
29552f7a69 Update dependencies 2021-03-22 23:49:43 -05:00
57983b5a52 s/CmdArgs/Context/g 2021-03-16 19:23:50 -05:00
88d7d7a0b4 Adding strings is not necessary 2021-03-16 19:21:59 -05:00
1f87c72e34 Fix syntax error 2021-03-16 19:20:19 -05:00
454bfbf61d Merge environment variables into Config 2021-03-16 19:09:53 -05:00
d5db68dbfa Change version format 2021-03-15 23:18:19 -05:00
dfa5d41a6d Add a default configuration 2021-03-15 22:26:19 -05:00
32bce4eaba Add functions to load configuration 2021-03-14 22:11:12 -05:00
e86ce1aeaf Add VSCode workspace 2021-03-14 17:08:02 -05:00
b3a6dfbe07 Move more configuration related code 2021-03-14 13:51:55 -05:00
804ec99021 Move configuration code to seperate file 2021-03-13 23:51:08 -06:00
ff05f8e2f3 Add printer object to format output 2021-03-13 16:20:18 -06:00
94cc596afa Add git commit to version output 2021-03-10 21:39:47 -06:00
1a7b665376 Use tablewriter for output formatting 2021-03-10 20:57:05 -06:00
2d2efd4431 Move print functions 2021-03-07 10:45:56 -06:00
3c9137816d make make clean silent 2021-03-07 10:33:28 -06:00
40851c6a62 Use terminal color library 2021-03-07 10:32:05 -06:00
518e304948 Add support for nil flag sets 2021-03-03 23:31:27 -06:00
78c7572139 Use parentheses 2021-03-03 00:29:40 -06:00
5b17adb071 Make make output quiet 2021-03-02 20:50:21 -06:00
38ab315a9b Add build date to LDFLAGS 2021-03-02 20:50:21 -06:00
de0bc2e133 Show rumtime information in version output 2021-03-02 00:44:38 -06:00
4d3074fa14 Use LDFLAGS to set build version 2021-03-02 00:44:18 -06:00
c8d0d306e4 Add quiet flag to del 2021-03-02 00:40:22 -06:00
8a7e99234f Add Windows support to Makefile 2021-03-01 22:59:11 -06:00
bfb949ca5f Ignore VSCode logs files 2021-03-01 22:58:25 -06:00
0fac4a3c14 Add version command 2021-02-27 22:42:09 -06:00
c9e4d9af80 Improve command loading 2021-02-16 23:24:32 -06:00
dd0bf763a3 Add PrintfWithIndent function 2021-02-16 20:20:18 -06:00
01086e3c22 Formatting changes for the show command 2021-02-15 19:46:13 -06:00
63bbbcecf7 Add pre-release tag 2021-02-15 19:30:25 -06:00
862b70316f Add MPL-2 licesne file 2021-02-14 23:17:02 -06:00
272ae7cd01 Add show command 2021-02-14 23:06:12 -06:00
cf8b34fe01 Move client library to separate repo
git.kill0.net/chill9/lifx-go
2021-02-14 19:02:26 -06:00
761e38fd8e Add version to user agent 2021-02-13 18:42:47 -06:00
e0a658edf7 Minor change to library user agent 2021-02-13 18:42:07 -06:00
a7fab72a1e Use functional options for the client constructor 2021-02-11 09:16:24 -06:00
b643635cf4 Make the user agent configurable 2021-02-10 22:16:15 -06:00
8e7ff7f39a Validate flags before running the command 2021-02-07 17:38:49 -06:00
8c6567bc8a Using os.Args is not necessary 2021-02-07 17:31:57 -06:00
d8be978ba8 Pass command name to CmdArgs struct 2021-02-07 17:31:19 -06:00
9360df727a Consolidate subcommand registration 2021-02-02 20:25:19 -06:00
4e25579718 Sort commands in help output
Closes #5
2021-02-02 20:24:57 -06:00
0cbd256b86 Print help if no subcommand is given.
Closes #4
2021-01-31 20:31:00 -06:00
9321095e11 Clean up subcommand registrations 2021-01-31 19:40:27 -06:00
320815293d ExitFailure matches stdlib.h 2021-01-31 11:08:24 -06:00
ec673a6fde Return usage error if no color is provided 2021-01-24 20:50:10 -06:00
f2da3dadd7 Whitespace fix 2021-01-24 20:49:22 -06:00
211de24485 Add helper to print subcommand help 2021-01-24 20:48:38 -06:00
743d0eed7c Show error from TOML library when parsing fails 2021-01-24 19:21:54 -06:00
b1894b38de Validate colors from configuration file 2021-01-24 19:21:44 -06:00
1ac9487c76 Add sample colors to sample .lumerc 2021-01-24 19:21:17 -06:00
8364201d7b Add helper method to exit with error code 2021-01-20 23:26:33 -06:00
c3495ac57a Add method to validate configuration 2021-01-20 23:26:33 -06:00
b9e98679a1 Refactor CLI exit code and error handling 2021-01-20 23:26:33 -06:00
c816ee43e1 Fix import alias 2021-01-20 23:26:33 -06:00
2da3bcc2ff Return error codes instead of exiting from Main() 2021-01-20 23:26:33 -06:00
4c6c3744c9 Move lume main into the lumecmd package 2021-01-20 23:26:33 -06:00
84c1be5054 Print error if .lumerc cannot be found 2021-01-20 23:26:32 -06:00
1e4a5f988e Use snake case in the configuration file 2021-01-20 23:26:32 -06:00
e8abc45377 Add poweron and poweroff commands 2021-01-20 23:26:32 -06:00
6e1863f374 Add CLI output sort functions 2021-01-20 23:26:32 -06:00
d0691362d8 Use a map to track column widths 2021-01-20 23:26:32 -06:00
715fb435e1 Refactor configuration file loading 2021-01-20 23:26:31 -06:00
63631be598 Fix composite literal uses unkeyed fields 2021-01-20 23:26:31 -06:00
30fae38ad1 Move Windows console hack into constrained file to fix Linux build 2021-01-20 23:26:31 -06:00
c838c28e49 Add .lumerc to .gitignore 2021-01-20 23:26:31 -06:00
af2721720d Remove __debug_bin 2021-01-20 23:26:27 -06:00
1397fbe4a3 Fix help usage 2021-01-10 21:10:08 -06:00
cd3653deef fix ls command usage text 2021-01-10 21:10:03 -06:00
095c7bd995 Move configuration loading to separate function 2021-01-10 21:09:59 -06:00
9988800431 Enable terminal colors on Windows 10 2021-01-10 21:09:54 -06:00
91478dd20e Add vscode stuff 2021-01-10 21:09:50 -06:00
bd1939e546 Create go module 2021-01-10 21:09:47 -06:00
b834bf84b2 fix syntax 2021-01-10 21:09:36 -06:00
cb87159237 add help command 2020-08-07 22:11:33 -05:00
491870e247 add short and long descriptions of the commands 2020-08-07 22:09:40 -05:00
617c2fccf9 add usage to every command 2020-08-07 18:44:36 -05:00
4325398ca8 add usage information to Command struct 2020-08-07 18:11:42 -05:00
39d889ecb6 add name to Command struct 2020-08-07 18:09:54 -05:00
e373e7e273 add error helpers 2020-04-05 18:54:40 -05:00
7fd366823f remove unused import 2020-04-04 15:30:52 -05:00
40 changed files with 2139 additions and 1143 deletions

9
.gitignore vendored
View File

@ -6,6 +6,8 @@
*.so
*.dylib
/bin
# Test binary, built with `go test -c`
*.test
@ -15,3 +17,10 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.lumerc
.vscode/configurationCache.log
.vscode/dryrun.log
.vscode/targets.log
# packaging
/build

11
.lumerc.sample Normal file
View File

@ -0,0 +1,11 @@
access_token = "token"
[colors]
purple_candy = [ 280.0, 0.29, 0.71 ]
wasabi = [ 120.0, 1.0, 0.7 ]
honeydew = [ 120.0, 1.0, 0.97 ]
green_mist = [ 92.0, 0.72, 0.75 ]
pea = [ 90.0, 0.42, 0.47 ]
cat_eye = [ 76.0, 0.74, 0.61 ]
seagreen = [ 160.0, 1.0, 0.50 ]
blue_mist = [ 202.0, 0.97, 0.75 ]

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/cmd/lume"
}
]
}

23
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,23 @@
{
"version": "2.0.0",
"type": "shell",
"command": "go",
"cwd": "${workspaceFolder}",
"tasks": [
{
"label": "install",
"args": ["install", "-v", "./..."],
"group": "build",
},
{
"label": "run",
"args": ["run", "${file}"],
"group": "build",
},
{
"label": "test",
"args": ["test", "-v", "./..."],
"group": "test",
},
],
}

312
LICENSE Normal file
View File

@ -0,0 +1,312 @@
Mozilla Public License Version 2.0
1. Definitions
1.1. "Contributor" means each individual or legal entity that creates, contributes
to the creation of, or owns Covered Software.
1.2. "Contributor Version" means the combination of the Contributions of others
(if any) used by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution" means Covered Software of a particular Contributor.
1.4. "Covered Software" means Source Code Form to which the initial Contributor
has attached the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case including portions
thereof.
1.5. "Incompatible With Secondary Licenses" means
(a) that the initial Contributor has attached the notice described in Exhibit
B to the Covered Software; or
(b) that the Covered Software was made available under the terms of version
1.1 or earlier of the License, but not also under the terms of a Secondary
License.
1.6. "Executable Form" means any form of the work other than Source Code Form.
1.7. "Larger Work" means a work that combines Covered Software with other
material, in a separate file or files, that is not Covered Software.
1.8. "License" means this document.
1.9. "Licensable" means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and all of the
rights conveyed by this License.
1.10. "Modifications" means any of the following:
(a) any file in Source Code Form that results from an addition to, deletion
from, or modification of the contents of Covered Software; or
(b) any new file in Source Code Form that contains any Covered Software.
1.11. "Patent Claims" of a Contributor means any patent claim(s), including
without limitation, method, process, and apparatus claims, in any patent Licensable
by such Contributor that would be infringed, but for the grant of the License,
by the making, using, selling, offering for sale, having made, import, or
transfer of either its Contributions or its Contributor Version.
1.12. "Secondary License" means either the GNU General Public License, Version
2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those licenses.
1.13. "Source Code Form" means the form of the work preferred for making modifications.
1.14. "You" (or "Your") means an individual or a legal entity exercising rights
under this License. For legal entities, "You" includes any entity that controls,
is controlled by, or is under common control with You. For purposes of this
definition, "control" means (a) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or otherwise,
or (b) ownership of more than fifty percent (50%) of the outstanding shares
or beneficial ownership of such entity.
2. License Grants and Conditions
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive
license:
(a) under intellectual property rights (other than patent or trademark) Licensable
by such Contributor to use, reproduce, make available, modify, display, perform,
distribute, and otherwise exploit its Contributions, either on an unmodified
basis, with Modifications, or as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer for
sale, have made, import, and otherwise transfer either its Contributions or
its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution become
effective for each Contribution on the date the Contributor first distributes
such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under this
License. No additional rights or licenses will be implied from the distribution
or licensing of Covered Software under this License. Notwithstanding Section
2.1(b) above, no patent license is granted by a Contributor:
(a) for any code that a Contributor has removed from Covered Software; or
(b) for infringements caused by: (i) Your and any other third party's modifications
of Covered Software, or (ii) the combination of its Contributions with other
software (except as part of its Contributor Version); or
(c) under Patent Claims infringed by Covered Software in the absence of its
Contributions.
This License does not grant any rights in the trademarks, service marks, or
logos of any Contributor (except as may be necessary to comply with the notice
requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to distribute
the Covered Software under a subsequent version of this License (see Section
10.2) or under the terms of a Secondary License (if permitted under the terms
of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its Contributions
are its original creation(s) or it has sufficient rights to grant the rights
to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under applicable
copyright doctrines of fair use, fair dealing, or other equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
Section 2.1.
3. Responsibilities
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any Modifications
that You create or to which You contribute, must be under the terms of this
License. You must inform recipients that the Source Code Form of the Covered
Software is governed by the terms of this License, and how they can obtain
a copy of this License. You may not attempt to alter or restrict the recipients'
rights in the Source Code Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code Form,
as described in Section 3.1, and You must inform recipients of the Executable
Form how they can obtain a copy of such Source Code Form by reasonable means
in a timely manner, at a charge no more than the cost of distribution to the
recipient; and
(b) You may distribute such Executable Form under the terms of this License,
or sublicense it under different terms, provided that the license for the
Executable Form does not attempt to limit or alter the recipients' rights
in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice, provided
that You also comply with the requirements of this License for the Covered
Software. If the Larger Work is a combination of Covered Software with a work
governed by one or more Secondary Licenses, and the Covered Software is not
Incompatible With Secondary Licenses, this License permits You to additionally
distribute such Covered Software under the terms of such Secondary License(s),
so that the recipient of the Larger Work may, at their option, further distribute
the Covered Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices (including
copyright notices, patent notices, disclaimers of warranty, or limitations
of liability) contained within the Source Code Form of the Covered Software,
except that You may alter any license notices to the extent required to remedy
known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support, indemnity
or liability obligations to one or more recipients of Covered Software. However,
You may do so only on Your own behalf, and not on behalf of any Contributor.
You must make it absolutely clear that any such warranty, support, indemnity,
or liability obligation is offered by You alone, and You hereby agree to indemnify
every Contributor for any liability incurred by such Contributor as a result
of warranty, support, indemnity or liability terms You offer. You may include
additional disclaimers of warranty and limitations of liability specific to
any jurisdiction.
4. Inability to Comply Due to Statute or Regulation
If it is impossible for You to comply with any of the terms of this License
with respect to some or all of the Covered Software due to statute, judicial
order, or regulation then You must: (a) comply with the terms of this License
to the maximum extent possible; and (b) describe the limitations and the code
they affect. Such description must be placed in a text file included with
all distributions of the Covered Software under this License. Except to the
extent prohibited by statute or regulation, such description must be sufficiently
detailed for a recipient of ordinary skill to be able to understand it.
5. Termination
5.1. The rights granted under this License will terminate automatically if
You fail to comply with any of its terms. However, if You become compliant,
then the rights granted under this License from a particular Contributor are
reinstated (a) provisionally, unless and until such Contributor explicitly
and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor
fails to notify You of the non-compliance by some reasonable means prior to
60 days after You have come back into compliance. Moreover, Your grants from
a particular Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the first
time You have received notice of non-compliance with this License from such
Contributor, and You become compliant prior to 30 days after Your receipt
of the notice.
5.2. If You initiate litigation against any entity by asserting a patent infringement
claim (excluding declaratory judgment actions, counter-claims, and cross-claims)
alleging that a Contributor Version directly or indirectly infringes any patent,
then the rights granted to You by any and all Contributors for the Covered
Software under Section 2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end
user license agreements (excluding distributors and resellers) which have
been validly granted by You or Your distributors under this License prior
to termination shall survive termination.
6. Disclaimer of Warranty
Covered Software is provided under this License on an "as is" basis, without
warranty of any kind, either expressed, implied, or statutory, including,
without limitation, warranties that the Covered Software is free of defects,
merchantable, fit for a particular purpose or non-infringing. The entire risk
as to the quality and performance of the Covered Software is with You. Should
any Covered Software prove defective in any respect, You (not any Contributor)
assume the cost of any necessary servicing, repair, or correction. This disclaimer
of warranty constitutes an essential part of this License. No use of any Covered
Software is authorized under this License except under this disclaimer.
7. Limitation of Liability
Under no circumstances and under no legal theory, whether tort (including
negligence), contract, or otherwise, shall any Contributor, or anyone who
distributes Covered Software as permitted above, be liable to You for any
direct, indirect, special, incidental, or consequential damages of any character
including, without limitation, damages for lost profits, loss of goodwill,
work stoppage, computer failure or malfunction, or any and all other commercial
damages or losses, even if such party shall have been informed of the possibility
of such damages. This limitation of liability shall not apply to liability
for death or personal injury resulting from such party's negligence to the
extent applicable law prohibits such limitation. Some jurisdictions do not
allow the exclusion or limitation of incidental or consequential damages,
so this exclusion and limitation may not apply to You.
8. Litigation
Any litigation relating to this License may be brought only in the courts
of a jurisdiction where the defendant maintains its principal place of business
and such litigation shall be governed by laws of that jurisdiction, without
reference to its conflict-of-law provisions. Nothing in this Section shall
prevent a party's ability to bring cross-claims or counter-claims.
9. Miscellaneous
This License represents the complete agreement concerning the subject matter
hereof. If any provision of this License is held to be unenforceable, such
provision shall be reformed only to the extent necessary to make it enforceable.
Any law or regulation which provides that the language of a contract shall
be construed against the drafter shall not be used to construe this License
against a Contributor.
10. Versions of the License
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section 10.3,
no one other than the license steward has the right to modify or publish new
versions of this License. Each version will be given a distinguishing version
number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version of
the License under which You originally received the Covered Software, or under
the terms of any subsequent version published by the license steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to create
a new license for such software, you may create and use a modified version
of this License if you rename the license and remove any references to the
name of the license steward (except to note that such modified license differs
from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
If You choose to distribute Source Code Form that is Incompatible With Secondary
Licenses under the terms of this version of the License, the notice described
in Exhibit B of this License must be attached. Exhibit A - Source Code Form
License Notice
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain
one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular file,
then You may include the notice in a location (such as a LICENSE file in a
relevant directory) where a recipient would be likely to look for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
This Source Code Form is "Incompatible With Secondary Licenses", as defined
by the Mozilla Public License, v. 2.0.

107
Makefile
View File

@ -1,7 +1,108 @@
V ?= 0
Q = $(if $(filter 1, $V),, @)
BINDIR=$(CURDIR)/bin
PREFIX=/usr
DESTDIR=bin
BUILDDIR=$(CURDIR)/build
MANDIR=$(PREFIX)/share/man/man1
PKGREVISION=1
DEBBUILDDIR=$(BUILDDIR)/deb
DEBTMPLDIR=$(CURDIR)/packaging/debian
DEBDATE=$(shell date -R)
DEBORIGSRC=lume_$(DEBVERSION).orig.tar.xz
DEBORIGSRCDIR=lume-$(DEBVERSION)
DEBREVISION=$(PKGREVISION)
RPMVERSION=$(subst -,_,$(LUME_VERSION))
RPMBUILDDIR=$(BUILDDIR)/rpm
RPMTMPLDIR=$(CURDIR)/packaging/rpm
RPMDATE=$(shell date "+%a %b %d %Y")
RPMORIGSRC=lume-$(RPMVERSION).tar.xz
RPMORIGSRCDIR=lume-$(RPMVERSION)
RPMREVISION=$(PKGREVISION)
ifeq ($(OS), Windows_NT)
EXE=$(BINDIR)/lume.exe
RM=del /f /q
BUILD_DATE=$(shell powershell Get-Date -Format "yyyy-MM-ddThh:mm:sszzz")
else
EXE=$(BINDIR)/lume
RM=rm -f
BUILD_DATE=$(shell date --iso-8601=seconds)
endif
LUME_VERSION ?= $(shell git describe --tags --always)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_TAG=$(shell git describe --tags --abbrev=0)
LDFLAGS = \
-X git.kill0.net/chill9/lume/cmd.Version=$(LUME_VERSION) \
-X git.kill0.net/chill9/lume/cmd.BuildDate=$(BUILD_DATE) \
-X git.kill0.net/chill9/lume/cmd.GitCommit=$(GIT_COMMIT)
ifneq (,$(findstring -,$(LUME_VERSION)))
DEBVERSION=$(GIT_TAG)+git$(shell date +%Y%m%d)+$(GIT_COMMIT)
else
DEBVERSION=$(LUME_VERSION)
endif
.PHONY: build
build:
go build -o lume ./cmd/lume
$(Q) go build -o $(EXE) -ldflags="$(LDFLAGS)" ./cmd/lume
.PHONY: clean
clean:
rm -f ./lifx
clean: deb-clean rpm-clean
$(Q) $(RM) $(EXE)
install-man:
install -p -D -m 0644 lume.1 $(DESTDIR)$(MANDIR)/lume.1
.PHONY: install
install: install-man
$(Q) install -p -D -m 0755 $(EXE) $(DESTDIR)${PREFIX}/bin/lume
$(Q) install -p -D -m 0644 .lumerc.sample $(DESTDIR)${PREFIX}/share/lume/lumerc
.PHONY: deb
deb: deb-clean
$(Q) mkdir -p $(DEBBUILDDIR)
$(Q) git archive --format tar --prefix lume-$(DEBVERSION)/ $(LUME_VERSION) | xz > $(DEBBUILDDIR)/$(DEBORIGSRC)
$(Q) tar xf $(DEBBUILDDIR)/$(DEBORIGSRC) -C $(DEBBUILDDIR)
$(Q) mkdir $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian
$(Q) mkdir $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/source
$(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' -e 's/__REVISION__/$(DEBREVISION)/g' $(DEBTMPLDIR)/rules > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/rules
$(Q) chmod 0755 $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/rules
$(Q) sed -e 's/__VERSION__/$(DEBVERSION)/g' -e 's/__DATE__/$(DEBDATE)/g' -e 's/__REVISION__/$(DEBREVISION)/g' $(DEBTMPLDIR)/changelog > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/changelog
$(Q) echo 10 > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/compat
$(Q) echo "3.0 (quilt)" > $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/source/format
$(Q) cp $(DEBTMPLDIR)/control $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/control
$(Q) cp $(DEBTMPLDIR)/copyright $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/copyright
$(Q) cp $(DEBTMPLDIR)/lume.manpages $(DEBBUILDDIR)/$(DEBORIGSRCDIR)/debian/lume.manpages
$(Q) cd $(DEBBUILDDIR)/$(DEBORIGSRCDIR) && dpkg-buildpackage -us -uc
$(Q) mv $(DEBBUILDDIR)/*.dsc $(BUILDDIR)
$(Q) mv $(DEBBUILDDIR)/*.changes $(BUILDDIR)
$(Q) mv $(DEBBUILDDIR)/*.buildinfo $(BUILDDIR)
$(Q) mv $(DEBBUILDDIR)/*.deb $(BUILDDIR)
$(Q) mv $(DEBBUILDDIR)/*.tar.* $(BUILDDIR)
.PHONY: rpm
rpm: rpm-clean
$(Q) mkdir -p $(RPMBUILDDIR)/SPECS
$(Q) mkdir -p $(RPMBUILDDIR)/SOURCES
$(Q) sed -e 's/__VERSION__/$(RPMVERSION)/g' -e 's/__DATE__/$(RPMDATE)/g' -e 's/__REVISION__/$(RPMREVISION)/g' $(RPMTMPLDIR)/lume.spec > $(RPMBUILDDIR)/SPECS/lume.spec
$(Q) git archive --format tar --prefix $(RPMORIGSRCDIR)/ $(LUME_VERSION) | xz > $(RPMBUILDDIR)/SOURCES/$(RPMORIGSRC)
$(Q) rpmbuild --define "_topdir $(RPMBUILDDIR)" -ba $(RPMBUILDDIR)/SPECS/lume.spec
$(Q) mv $(RPMBUILDDIR)/RPMS/*/*.rpm $(BUILDDIR)
$(Q) mv $(RPMBUILDDIR)/SRPMS/*.rpm $(BUILDDIR)
deb-clean:
$(Q) rm -rf $(DEBBUILDDIR)
$(Q) rm -f $(BUILDDIR)/*.dsc
$(Q) rm -f $(BUILDDIR)/*.changes
$(Q) rm -f $(BUILDDIR)/*.buildinfo
$(Q) rm -f $(BUILDDIR)/*.deb
$(Q) rm -f $(BUILDDIR)/*.tar.*
rpm-clean:
$(Q) rm -rf $(RPMBUILDDIR)
$(Q) rm -f $(BUILDDIR)/*.rpm

295
client.go
View File

@ -1,295 +0,0 @@
package lifx
import (
//"crypto/tls"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
const UserAgent = "lume"
type (
Client struct {
accessToken string
Client *http.Client
}
Result struct {
Id string `json:"id"`
Label string `json:"label"`
Status Status `json:"status"`
}
Error struct {
Field string `json:"field"`
Message []string `json:"message"`
}
Warning struct {
Warning string `json:"warning"`
}
RateLimit struct {
Limit int
Remaining int
Reset time.Time
}
Response struct {
StatusCode int
Header http.Header
Body io.ReadCloser
RateLimit RateLimit
}
LifxResponse struct {
Error string `json:"error"`
Errors []Error `json:"errors"`
Warnings []Warning `json:"warnings"`
Results []Result `json:"results"`
}
)
var errorMap = map[int]error{
http.StatusNotFound: errors.New("Selector did not match any lights"),
http.StatusUnauthorized: errors.New("Bad access token"),
http.StatusForbidden: errors.New("Bad OAuth scope"),
http.StatusUnprocessableEntity: errors.New("Missing or malformed parameters"),
http.StatusUpgradeRequired: errors.New("HTTP was used to make the request instead of HTTPS. Repeat the request using HTTPS instead"),
http.StatusTooManyRequests: errors.New("The request exceeded a rate limit"),
http.StatusInternalServerError: errors.New("Something went wrong on LIFX's end"),
http.StatusBadGateway: errors.New("Something went wrong on LIFX's end"),
http.StatusServiceUnavailable: errors.New("Something went wrong on LIFX's end"),
523: errors.New("Something went wrong on LIFX's end"),
}
func NewClient(accessToken string) *Client {
tr := &http.Transport{
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &Client{
accessToken: accessToken,
Client: &http.Client{Transport: tr},
}
}
func NewResponse(r *http.Response) (*Response, error) {
resp := Response{
StatusCode: r.StatusCode,
Header: r.Header,
Body: r.Body,
}
if t := r.Header.Get("X-RateLimit-Limit"); t != "" {
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
resp.RateLimit.Limit = int(n)
} else {
return nil, err
}
}
if t := r.Header.Get("X-RateLimit-Remaining"); t != "" {
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
resp.RateLimit.Remaining = int(n)
} else {
return nil, err
}
}
if t := r.Header.Get("X-RateLimit-Reset"); t != "" {
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
resp.RateLimit.Reset = time.Unix(n, 0)
} else {
return nil, err
}
}
return &resp, nil
}
func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, url, body)
if err != nil {
return
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", UserAgent)
return
}
func (c *Client) setState(selector string, state State) (*Response, error) {
var (
err error
j []byte
req *http.Request
r *http.Response
resp *Response
)
if j, err = json.Marshal(state); err != nil {
return nil, err
}
if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) setStates(selector string, states States) (*Response, error) {
var (
err error
j []byte
req *http.Request
r *http.Response
resp *Response
)
if j, err = json.Marshal(states); err != nil {
return nil, err
}
if req, err = c.NewRequest("PUT", EndpointStates(), bytes.NewBuffer(j)); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) toggle(selector string, duration float64) (*Response, error) {
var (
err error
j []byte
req *http.Request
r *http.Response
resp *Response
)
if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil {
return nil, err
}
if req, err = c.NewRequest("POST", EndpointToggle(selector), bytes.NewBuffer(j)); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) validateColor(color Color) (*Response, error) {
var (
err error
req *http.Request
r *http.Response
resp *Response
q url.Values
)
if req, err = c.NewRequest("GET", EndpointColor(), nil); err != nil {
return nil, err
}
q = req.URL.Query()
q.Set("string", color.ColorString())
req.URL.RawQuery = q.Encode()
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) listLights(selector string) (*Response, error) {
var (
err error
req *http.Request
r *http.Response
resp *Response
)
if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *Client) stateDelta(selector string, delta StateDelta) (*Response, error) {
var (
err error
j []byte
req *http.Request
r *http.Response
resp *Response
)
if j, err = json.Marshal(delta); err != nil {
return nil, err
}
if req, err = c.NewRequest("POST", EndpointStateDelta(selector), bytes.NewBuffer(j)); err != nil {
return nil, err
}
if r, err = c.Client.Do(req); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
return resp, nil
}

109
cmd/breathe.go Normal file
View File

@ -0,0 +1,109 @@
package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdBreathe() Command {
return Command{
Name: "breathe",
Func: BreatheCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("breathe", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
color := fs.String("color", defaultColor, "The color to use for the breathe effect")
fs.StringVar(color, "c", defaultColor, "The color to use for the breathe effect")
fs.String("from-color", defaultColor, "The color to start the effect from")
fs.Float64("period", lifx.DefaultBreathePeriod, "The time in seconds for one cycle of the effect")
fs.Float64("cycles", lifx.DefaultBreatheCycles, "The number of times to repeat the effect")
fs.Bool("persist", lifx.DefaultBreathePersist, "If false set the light back to its previous value when effect ends, if true leave the last effect color")
fs.Bool("power-on", lifx.DefaultBreathePowerOn, "If true, turn the bulb on if it is not already on")
fs.Float64("peak", lifx.DefaultBreathePeak, "Defines where in a period the target color is at its maximum (min: 0.0, max: 1.0)")
return fs
}(),
Use: "[--selector <selector>] --color <color> [--from-color <color>] [--period <period>] [--cycles <cycles>] [--persist <persist>] [--power-on] [--peak <peak>]",
Short: "The breathe effect",
}
}
func BreatheCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
breathe := lifx.NewBreathe()
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
color := ctx.Flags.String("color")
if color != "" {
breathe.Color = lifx.NamedColor(color)
}
from_color := ctx.Flags.String("from-color")
if from_color != "" {
breathe.FromColor = lifx.NamedColor(from_color)
}
periodFlag := ctx.Flags.String("period")
if periodFlag != "" {
period := ctx.Flags.Float64("period")
breathe.Period = period
}
cyclesFlag := ctx.Flags.String("cycles")
if cyclesFlag != "" {
cycles := ctx.Flags.Float64("cycles")
breathe.Cycles = cycles
}
persist := ctx.Flags.Bool("persist")
breathe.Persist = persist
power_on := ctx.Flags.Bool("power-on")
breathe.PowerOn = power_on
peakFlag := ctx.Flags.String("peak")
if peakFlag != "" {
peak := ctx.Flags.Float64("peak")
breathe.Peak = peak
}
if color == "" {
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
if err := breathe.Valid(); err != nil {
return ExitFailure, err
}
r, err := c.Breathe(selector, breathe)
if err != nil {
return ExitFailure, err
}
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
return ExitSuccess, nil
}

View File

@ -1,22 +1,25 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"strconv"
"git.kill0.net/chill9/lume"
"git.kill0.net/chill9/lifx-go"
)
type Config struct {
AccessToken string
Colors map[string][]float32 `toml:"colors"`
}
const (
ExitSuccess = iota
ExitFailure
)
type CmdArgs struct {
type Context struct {
Flags Flags
Args []string
Client *lifx.Client
Config Config
Name string
}
type Flags struct {
@ -25,26 +28,30 @@ type Flags struct {
type Command struct {
Name string
Func func(CmdArgs) (int, error)
Func func(Context) (int, error)
Flags *flag.FlagSet
Use string
Short string
Long string
}
var commandRegistry = make(map[string]Command)
var (
defaultSelector string = "all"
defaultDuration float64 = 1.0
defaultPower string = ""
defaultColor string = ""
defaultBrightness string = ""
defaultInfrared string = ""
defaultFast bool = false
defaultWhiteKelvin string = ""
defaultWhiteName string = ""
defaultHue string = ""
defaultSaturation string = ""
defaultRGB string = ""
defaultName string = ""
defaultSelector string = "all"
defaultDuration float64 = 1.0
defaultPower string = ""
defaultColor string = ""
defaultBrightness string = ""
defaultInfrared string = ""
defaultFast bool = false
defaultWhiteKelvin string = ""
defaultWhiteName string = ""
defaultHue string = ""
defaultSaturation string = ""
defaultRGB string = ""
defaultName string = ""
defaultOutputFormat string = ""
)
func (f Flags) String(name string) string {
@ -71,11 +78,18 @@ func (f Flags) Bool(name string) bool {
return val
}
func RegisterCommand(name string, cmd Command) error {
if _, ok := commandRegistry[name]; ok {
func RegisterCommand(cmd Command) error {
if _, ok := commandRegistry[cmd.Name]; ok {
return fmt.Errorf("%s command is already registered")
}
commandRegistry[name] = cmd
if cmd.Flags == nil {
cmd.Flags = flag.NewFlagSet(cmd.Name, flag.ExitOnError)
}
mergeGlobalFlags(cmd.Flags)
commandRegistry[cmd.Name] = cmd
return nil
}
@ -83,3 +97,26 @@ func GetCommand(name string) (Command, bool) {
cmd, ok := commandRegistry[name]
return cmd, ok
}
func mergeGlobalFlags(fs *flag.FlagSet) {
fs.Bool("debug", false, "Enable debug mode")
formatTable := fs.Bool("table", false, "Format output as an ASCII table")
fs.BoolVar(formatTable, "t", false, "Format output as an ASCII table")
fs.Bool("simple", false, "Format output simply")
}
func getOutputFormatFromFlags(fs Flags) (string, error) {
formatSimple := fs.Bool("simple")
formatTable := fs.Bool("table")
switch {
case formatSimple && formatTable:
return "", errors.New("only one output format permitted")
case formatTable:
return "table", nil
default:
return "simple", nil
}
}

157
cmd/config.go Normal file
View File

@ -0,0 +1,157 @@
package lumecmd
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/BurntSushi/toml"
)
const lumercFile string = ".lumerc"
const lumeConfigFile string = "lume.conf"
const defaultPowerIndicator rune = '●'
type Config struct {
AccessToken string `toml:"access_token"`
OutputFormat string `toml:"output_format"`
Colors map[string][]float32 `toml:"colors"`
userAgent string
Debug bool `toml:"debug"`
Indicator string `toml:"indicator"`
}
var (
DefaultConfig = Config{
userAgent: initUserAgent(),
}
globalConfig *Config = NewConfig()
)
func NewConfig() *Config {
c := new(Config)
c.userAgent = initUserAgent()
c.Debug = false
c.OutputFormat = "simple"
c.Indicator = string(defaultPowerIndicator)
return c
}
func GetConfig() *Config {
return globalConfig
}
// Validate configuration struct
func (c *Config) Validate() error {
var err error
if c.AccessToken == "" {
return errors.New("access_token is not set")
}
if len([]rune(c.Indicator)) != 1 {
return errors.New("indicator must be a single rune")
}
if err = c.validateColors(); err != nil {
return err
}
return err
}
func (c *Config) validateColors() (err error) {
if len(c.Colors) > 0 {
for name, hsb := range c.Colors {
if len(hsb) != 3 {
return fmt.Errorf("color '%s' needs three values", name)
}
h, s, b := hsb[0], hsb[1], hsb[2]
if h < 0 || h > 360 {
return fmt.Errorf("color '%s' hue value must be between 0.0-360.0", name)
}
if s < 0 || b > 1 {
return fmt.Errorf("color '%s' saturation value must be between 0.0-1.0", name)
}
if b < 0 || b > 1 {
return fmt.Errorf("color '%s' brightness value must be between 0.0-1.0", name)
}
}
}
return err
}
func (c *Config) MergeWithEnv() {
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
c.AccessToken = envAccessToken
}
}
func LoadConfig(s string) (*Config, error) {
var err error
var c *Config = GetConfig()
if _, err := toml.Decode(s, &c); err != nil {
err = fmt.Errorf("fatal: failed to parse; %w", err)
}
return c, err
}
func LoadConfigFile(configPath string) (*Config, error) {
var err error
var c *Config = GetConfig()
if _, err := toml.DecodeFile(configPath, &c); err != nil {
err = fmt.Errorf("fatal: failed to parse %s; %w", configPath, err)
}
return c, err
}
func getConfigPath() string {
var tryPath, configPath, homeDir, cwd string
var err error
// ~/.lumerc
homeDir, err = os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
// ~/.config/lume/lume.conf
homeDir, err = os.UserHomeDir()
if err == nil {
tryPath = path.Join(homeDir, ".config/lume", lumeConfigFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
// ./.lumerc
cwd, err = os.Getwd()
if err == nil {
tryPath = path.Join(cwd, lumercFile)
if _, err := os.Stat(tryPath); !os.IsNotExist(err) {
configPath = tryPath
}
}
return configPath
}
func initUserAgent() string {
var b strings.Builder
b.WriteString("lume")
b.WriteRune('/')
b.WriteString(Version)
return b.String()
}

87
cmd/help.go Normal file
View File

@ -0,0 +1,87 @@
package lumecmd
import (
"fmt"
"sort"
"strings"
)
func NewCmdHelp() Command {
return Command{
Name: "help",
Func: HelpCmd,
Use: "<command>",
Short: "Show help for a command",
}
}
func HelpCmd(ctx Context) (int, error) {
if len(ctx.Args) == 0 {
fmt.Print(printHelp(commandRegistry))
} else if len(ctx.Args) >= 1 {
if cmdHelp, err := printCmdHelp(ctx.Args[0]); err == nil {
fmt.Print(cmdHelp)
} else {
fmt.Print(err)
}
}
return ExitSuccess, nil
}
func printHelp(commands map[string]Command) string {
var b strings.Builder
var maxLen, cmdLen int
var keys []string
for _, c := range commands {
keys = append(keys, c.Name)
cmdLen = len(c.Name)
if cmdLen > maxLen {
maxLen = cmdLen
}
}
fmt.Fprintf(&b, "usage:\n lume <command> [<args...>]")
fmt.Fprintln(&b)
fmt.Fprintln(&b, "\ncommands:")
sort.Strings(keys)
for _, k := range keys {
c := commands[k]
fmt.Fprintf(&b, " %-*s %s\n", maxLen, c.Name, c.Short)
}
return b.String()
}
func printCmdHelp(name string) (string, error) {
var b strings.Builder
subCmd, ok := commandRegistry[name]
if !ok {
return "", fmt.Errorf("unknown commnnd: %s\n", name)
}
if subCmd.Use != "" {
fmt.Fprintf(&b, "usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
} else {
fmt.Fprintf(&b, "usage:\n lume %s\n", subCmd.Name)
}
if subCmd.Flags != nil {
out := subCmd.Flags.Output()
defer subCmd.Flags.SetOutput(out)
fmt.Fprintln(&b)
fmt.Fprint(&b, "flags:\n")
subCmd.Flags.SetOutput(&b)
subCmd.Flags.PrintDefaults()
}
return b.String(), nil
}

View File

@ -2,31 +2,48 @@ package lumecmd
import (
"flag"
"fmt"
)
var (
idWidth, locationWidth, groupWidth, labelWidth, lastSeenWidth, powerWidth int
)
func NewCmdLs() Command {
return Command{
Name: "ls",
Func: LsCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("ls", flag.ExitOnError)
func init() {
var cmdName string = "ls"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
RegisterCommand(cmdName, Command{
Func: LsCmd,
Flags: fs,
})
}
func LsCmd(args CmdArgs) (int, error) {
c := args.Client
selector := args.Flags.String("selector")
lights, err := c.ListLights(selector)
if err != nil {
return 1, err
return fs
}(),
Use: "[--selector=<selector>]",
Short: "List the lights",
}
PrintLights(lights)
return 0, nil
}
func LsCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
lights, err := c.ListLights(selector)
if err != nil {
return ExitFailure, err
}
p = NewPrinter(format)
fmt.Print(p.Lights(lights))
return ExitSuccess, nil
}

19
cmd/lume/init_windows.go Normal file
View File

@ -0,0 +1,19 @@
// +build windows
// https://stackoverflow.com/a/52579002
package main
import (
"os"
"golang.org/x/sys/windows"
)
func init() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}

View File

@ -1,54 +1,11 @@
package main
import (
"flag"
"fmt"
"os"
"path"
"git.kill0.net/chill9/lume"
lumecmd "git.kill0.net/chill9/lume/cmd"
"github.com/BurntSushi/toml"
)
const lumercFile = ".lumerc"
func main() {
var config lumecmd.Config
homeDir, err := os.UserHomeDir()
_, err = toml.DecodeFile(path.Join(homeDir, lumercFile), &config)
if os.IsNotExist(err) {
config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN")
}
if config.AccessToken == "" {
fmt.Println("access token is not set")
os.Exit(1)
}
flag.Parse()
command := flag.Arg(0)
c := lifx.NewClient(config.AccessToken)
cmdArgs := lumecmd.CmdArgs{
Client: c,
Config: config,
}
cmd, ok := lumecmd.GetCommand(command)
if !ok {
fmt.Printf("lume: '%s' is not lume command. See 'lume' --help.'\n", command)
os.Exit(1)
}
fs := cmd.Flags
fs.Parse(os.Args[2:])
cmdArgs.Flags = lumecmd.Flags{fs}
exitCode, err := cmd.Func(cmdArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
}
os.Exit(exitCode)
lumecmd.ExitWithCode(lumecmd.Main(os.Args))
}

88
cmd/main.go Normal file
View File

@ -0,0 +1,88 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"git.kill0.net/chill9/lifx-go"
)
func init() {
RegisterCommand(NewCmdHelp())
RegisterCommand(NewCmdLs())
RegisterCommand(NewCmdPoweroff())
RegisterCommand(NewCmdPoweron())
RegisterCommand(NewCmdSetColor())
RegisterCommand(NewCmdSetState())
RegisterCommand(NewCmdSetWhite())
RegisterCommand(NewCmdShow())
RegisterCommand(NewCmdToggle())
RegisterCommand(NewCmdVersion())
RegisterCommand(NewCmdBreathe())
RegisterCommand(NewCmdValidate())
}
var Version string
var BuildDate string
var GitCommit string
func Main(args []string) (int, error) {
var config *Config = GetConfig()
var err error
flag.Parse()
if len(args) == 1 {
args = append(args, "help")
}
configPath := getConfigPath()
if configPath == "" {
err = errors.New("fatal: ~/.lumerc was not found")
return ExitFailure, err
}
if config, err = LoadConfigFile(configPath); err != nil {
return ExitFailure, err
}
config.MergeWithEnv()
if err = config.Validate(); err != nil {
return ExitFailure, fmt.Errorf("fatal: %s", err)
}
command := args[1]
c := lifx.NewClient(
config.AccessToken,
lifx.WithUserAgent(config.userAgent),
lifx.WithDebug(config.Debug),
)
ctx := Context{
Client: c,
Config: *config,
Args: args[2:],
}
cmd, ok := GetCommand(command)
if !ok {
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
return ExitFailure, err
}
fs := cmd.Flags
if fs != nil {
fs.Parse(args[2:])
ctx.Flags = Flags{FlagSet: fs}
}
ctx.Name = command
exitCode, err := cmd.Func(ctx)
if err != nil {
err = fmt.Errorf("fatal: %s", err)
}
return exitCode, err
}

55
cmd/poweroff.go Normal file
View File

@ -0,0 +1,55 @@
package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdPoweroff() Command {
return Command{
Name: "poweroff",
Func: PoweroffCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweroff", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
}
}
func PoweroffCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector")
state := lifx.State{Power: "off", Duration: duration}
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
r, err := c.SetState(selector, state)
if err != nil {
return ExitFailure, err
}
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
return ExitSuccess, nil
}

55
cmd/poweron.go Normal file
View File

@ -0,0 +1,55 @@
package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdPoweron() Command {
return Command{
Name: "poweron",
Func: PoweronCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("poweron", flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Power on",
}
}
func PoweronCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector")
state := lifx.State{Power: "on", Duration: duration}
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
r, err := c.SetState(selector, state)
if err != nil {
return ExitFailure, err
}
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
return ExitSuccess, nil
}

195
cmd/print.go Normal file
View File

@ -0,0 +1,195 @@
package lumecmd
import (
"fmt"
"io"
"strings"
"time"
"git.kill0.net/chill9/lifx-go"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
type Printer interface {
Results(results []lifx.Result) string
Lights(lights []lifx.Light) string
}
type defaultPrinter struct{}
type tablePrinter struct{}
func NewPrinter(format string) Printer {
switch format {
case "table":
return &tablePrinter{}
default:
return &defaultPrinter{}
}
}
func (dp *defaultPrinter) Results(results []lifx.Result) string {
var b strings.Builder
sortResults(results)
table := tablewriter.NewWriter(&b)
_, rows := makeResultsTable(results)
for _, v := range rows {
table.Append(v)
}
fmt.Fprintf(&b, "total %d\n", len(results))
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false)
table.SetBorder(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetHeaderLine(false)
table.SetNoWhiteSpace(true)
table.SetRowSeparator("")
table.SetTablePadding(" ")
table.Render()
return b.String()
}
func (tp *tablePrinter) Results(results []lifx.Result) string {
var b strings.Builder
sortResults(results)
table := tablewriter.NewWriter(&b)
hdr, rows := makeResultsTable(results)
for _, v := range rows {
table.Append(v)
}
table.SetHeader(hdr)
table.Render()
return b.String()
}
func (dp *defaultPrinter) Lights(lights []lifx.Light) string {
var b strings.Builder
sortLights(lights)
table := tablewriter.NewWriter(&b)
_, rows := makeLightsTable(lights)
for _, v := range rows {
table.Append(v)
}
fmt.Fprintf(&b, "total %d\n", len(lights))
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false)
table.SetBorder(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetHeaderLine(false)
table.SetNoWhiteSpace(true)
table.SetRowSeparator("")
table.SetTablePadding(" ")
table.Render()
return b.String()
}
func (tp *tablePrinter) Lights(lights []lifx.Light) string {
var b strings.Builder
sortLights(lights)
table := tablewriter.NewWriter(&b)
hdr, rows := makeLightsTable(lights)
for _, v := range rows {
table.Append(v)
}
table.SetHeader(hdr)
table.Render()
return b.String()
}
func ColorizeIndicator(s string) string {
c := color.New(color.FgRed)
if s == "on" {
c = color.New(color.FgGreen)
}
return c.Sprint(GetConfig().Indicator)
}
func ColorizePower(s string) string {
c := color.New(color.FgRed)
if s == "on" {
c = color.New(color.FgGreen)
}
return c.Sprint(s)
}
func ColorizeStatus(s lifx.Status) string {
c := color.New(color.FgRed)
if s == "ok" {
c = color.New(color.FgGreen)
}
return c.Sprint(s)
}
func PrintWithIndent(indent int, s string) {
fmt.Printf("%*s%s", indent, "", s)
}
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
format = fmt.Sprintf("%*s%s", indent, "", format)
return fmt.Printf(format, a...)
}
func FprintfWithIndent(w io.Writer, indent int, format string, a ...interface{}) (n int, err error) {
format = fmt.Sprintf("%*s%s", indent, "", format)
return fmt.Fprintf(w, format, a...)
}
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
hdr = []string{"", "ID", "Location", "Group", "Label", "Last Seen", "Power"}
for _, l := range lights {
rows = append(rows, []string{
fmt.Sprint(ColorizeIndicator(l.Power)),
fmt.Sprint(l.Id),
fmt.Sprint(l.Location.Name),
fmt.Sprint(l.Group.Name),
fmt.Sprint(l.Label),
fmt.Sprint(l.LastSeen.Local().Format(time.RFC3339)),
fmt.Sprint(ColorizePower(l.Power)),
})
}
return
}
func makeResultsTable(results []lifx.Result) (hdr []string, rows [][]string) {
hdr = []string{"ID", "Label", "Status"}
for _, r := range results {
rows = append(rows, []string{
fmt.Sprint(r.Id),
fmt.Sprint(r.Label),
fmt.Sprint(ColorizeStatus(r.Status)),
})
}
return
}

View File

@ -4,74 +4,90 @@ import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
"git.kill0.net/chill9/lifx-go"
)
func init() {
var cmdName string = "set-color"
func NewCmdSetColor() Command {
return Command{
Name: "set-color",
Func: SetColorCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
hue := fs.String("hue", defaultHue, "hue level")
fs.StringVar(hue, "H", defaultHue, "hue level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
saturation := fs.String("saturation", defaultSaturation, "saturation level")
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
rgb := fs.String("rgb", defaultRGB, "RGB value")
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
name := fs.String("name", defaultName, "named color")
fs.StringVar(name, "n", defaultName, "named color")
name := fs.String("name", defaultName, "named color")
fs.StringVar(name, "n", defaultName, "named color")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
RegisterCommand(cmdName, Command{
Func: SetColorCmd,
Flags: fs,
})
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
Short: "Set the color",
}
}
func SetColorCmd(args CmdArgs) (int, error) {
c := args.Client
func SetColorCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
state := lifx.State{}
selector := args.Flags.String("selector")
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
fmt.Println(args.Config)
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
power := args.Flags.String("power")
power := ctx.Flags.String("power")
if power != "" {
state.Power = power
}
hueFlag := args.Flags.String("hue")
saturationFlag := args.Flags.String("saturation")
rgbFlag := args.Flags.String("rgb")
name := args.Flags.String("name")
hueFlag := ctx.Flags.String("hue")
saturationFlag := ctx.Flags.String("saturation")
rgbFlag := ctx.Flags.String("rgb")
name := ctx.Flags.String("name")
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
if hueFlag != "" || saturationFlag != "" {
color := lifx.HSBKColor{}
if hueFlag != "" {
hue := args.Flags.Float32("hue")
hue := ctx.Flags.Float32("hue")
color.H = lifx.Float32Ptr(hue)
}
if saturationFlag != "" {
saturation := args.Flags.Float32("saturation")
saturation := ctx.Flags.Float32("saturation")
color.S = lifx.Float32Ptr(saturation)
}
state.Color = color
@ -79,42 +95,43 @@ func SetColorCmd(args CmdArgs) (int, error) {
} else if rgbFlag != "" {
color, err := parseRGB(rgbFlag)
if err != nil {
return 1, err
return ExitFailure, err
}
state.Color = color
} else if name != "" {
hsb, ok := args.Config.Colors[name]
hsb, ok := ctx.Config.Colors[name]
if !ok {
return 1, fmt.Errorf("%s is not a defined color", name)
return ExitFailure, fmt.Errorf("%s is not a defined color", name)
}
color, err := lifx.NewHSBColor(hsb[0], hsb[1], hsb[2])
if err != nil {
return 1, err
return ExitFailure, err
}
state.Color = color
}
brightnessFlag := args.Flags.String("brightness")
brightnessFlag := ctx.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
brightness := ctx.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
duration := ctx.Flags.Float64("duration")
state.Duration = duration
fast := args.Flags.Bool("fast")
fast := ctx.Flags.Bool("fast")
state.Fast = fast
r, err := c.SetState(selector, state)
if err != nil {
fmt.Printf("fatal: %s\n", err)
return 1, err
return ExitFailure, err
}
if !fast {
PrintResults(r.Results)
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
}
return 0, nil
return ExitSuccess, nil
}

View File

@ -2,83 +2,103 @@ package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
"git.kill0.net/chill9/lifx-go"
)
func init() {
var cmdName string = "set-state"
func NewCmdSetState() Command {
return Command{
Name: "set-state",
Func: SetStateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
color := fs.String("color", defaultColor, "color state")
fs.StringVar(color, "c", defaultColor, "color state")
color := fs.String("color", defaultColor, "color state")
fs.StringVar(color, "c", defaultColor, "color state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
RegisterCommand(cmdName, Command{
Func: SetStateCmd,
Flags: fs,
})
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
Short: "Set various state attributes",
}
}
func SetStateCmd(args CmdArgs) (int, error) {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
func SetStateCmd(ctx Context) (int, error) {
var p Printer
power := args.Flags.String("power")
c := ctx.Client
state := lifx.State{}
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
power := ctx.Flags.String("power")
if power != "" {
state.Power = power
}
color := args.Flags.String("color")
color := ctx.Flags.String("color")
if color != "" {
state.Color = lifx.NamedColor(color)
}
brightnessFlag := args.Flags.String("brightness")
brightnessFlag := ctx.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
brightness := ctx.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
duration := ctx.Flags.Float64("duration")
state.Duration = duration
infraredFlag := args.Flags.String("infrared")
infraredFlag := ctx.Flags.String("infrared")
if infraredFlag != "" {
infrared := args.Flags.Float64("infrared")
infrared := ctx.Flags.Float64("infrared")
state.Infrared = infrared
}
fast := args.Flags.Bool("fast")
fast := ctx.Flags.Bool("fast")
state.Fast = fast
if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" {
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
r, err := c.SetState(selector, state)
if err != nil {
return 1, err
return ExitFailure, err
}
if !fast {
PrintResults(r.Results)
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
}
return 0, nil
return ExitSuccess, nil
}

View File

@ -2,101 +2,121 @@ package lumecmd
import (
"flag"
"fmt"
"git.kill0.net/chill9/lume"
"git.kill0.net/chill9/lifx-go"
)
func init() {
var cmdName string = "set-white"
func NewCmdSetWhite() Command {
return Command{
Name: "set-white",
Func: SetWhiteCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
selector := fs.String("selector", "all", "the selector")
fs.StringVar(selector, "s", "all", "the selector")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
power := fs.String("power", defaultPower, "power state")
fs.StringVar(power, "p", defaultPower, "power state")
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
name := fs.String("name", defaultWhiteName, "named white level")
fs.StringVar(name, "n", defaultWhiteName, "named white level")
name := fs.String("name", defaultWhiteName, "named white level")
fs.StringVar(name, "n", defaultWhiteName, "named white level")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
brightness := fs.String("brightness", defaultBrightness, "brightness state")
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
duration := fs.Float64("duration", defaultDuration, "duration state")
fs.Float64Var(duration, "d", defaultDuration, "duration state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
infrared := fs.String("infrared", defaultInfrared, "infrared state")
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
fast := fs.Bool("fast", defaultFast, "fast state")
fs.BoolVar(fast, "f", defaultFast, "fast state")
RegisterCommand(cmdName, Command{
Func: SetWhiteCmd,
Flags: fs,
})
return fs
}(),
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
Short: "Set the white level",
}
}
func SetWhiteCmd(args CmdArgs) (int, error) {
c := args.Client
state := lifx.State{}
selector := args.Flags.String("selector")
func SetWhiteCmd(ctx Context) (int, error) {
var p Printer
power := args.Flags.String("power")
c := ctx.Client
state := lifx.State{}
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
power := ctx.Flags.String("power")
if power != "" {
state.Power = power
}
kelvinFlag := args.Flags.String("kelvin")
kelvinFlag := ctx.Flags.String("kelvin")
if kelvinFlag != "" {
kelvin := args.Flags.Int16("kelvin")
kelvin := ctx.Flags.Int16("kelvin")
color, err := lifx.NewWhite(kelvin)
if err != nil {
return 1, err
return ExitFailure, err
}
state.Color = color
}
name := args.Flags.String("name")
name := ctx.Flags.String("name")
if name != "" {
name := args.Flags.String("name")
name := ctx.Flags.String("name")
color, err := lifx.NewWhiteString(name)
if err != nil {
return 1, err
return ExitFailure, err
}
state.Color = color
}
brightnessFlag := args.Flags.String("brightness")
brightnessFlag := ctx.Flags.String("brightness")
if brightnessFlag != "" {
brightness := args.Flags.Float64("brightness")
brightness := ctx.Flags.Float64("brightness")
state.Brightness = brightness
}
duration := args.Flags.Float64("duration")
duration := ctx.Flags.Float64("duration")
state.Duration = duration
infraredFlag := args.Flags.String("infrared")
infraredFlag := ctx.Flags.String("infrared")
if infraredFlag != "" {
infrared := args.Flags.Float64("infrared")
infrared := ctx.Flags.Float64("infrared")
state.Infrared = infrared
}
fast := args.Flags.Bool("fast")
fast := ctx.Flags.Bool("fast")
state.Fast = fast
if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" {
printCmdHelp(ctx.Name)
return ExitFailure, nil
}
r, err := c.SetState(selector, state)
if err != nil {
return 1, err
return ExitFailure, err
}
if !fast {
PrintResults(r.Results)
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
}
return 0, nil
return ExitSuccess, nil
}

99
cmd/show.go Normal file
View File

@ -0,0 +1,99 @@
package lumecmd
import (
"flag"
"fmt"
"strings"
)
const Tabstop int = 2
func NewCmdShow() Command {
return Command{
Name: "show",
Func: ShowCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("show", flag.ExitOnError)
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
return fs
}(),
Use: "[--selector=<selector>]",
Short: "Show details about the lights",
}
}
func ShowCmd(ctx Context) (int, error) {
var indent int
var b strings.Builder
c := ctx.Client
selector := ctx.Flags.String("selector")
lights, err := c.ListLights(selector)
if err != nil {
return ExitFailure, err
}
sortLights(lights)
for i, l := range lights {
indent = 0
fmt.Fprintf(
&b,
"%s Light ID: %s, %s, Power: %s\n",
ColorizeIndicator(l.Power),
l.Id,
connected(l.Connected),
ColorizePower(l.Power),
)
indent += Tabstop + 2
FprintfWithIndent(&b, indent, "Label: %s, ID: %s\n", l.Label, l.Id)
FprintfWithIndent(&b, indent, "UUID: %s\n", l.UUID)
FprintfWithIndent(&b, indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id)
FprintfWithIndent(&b, indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id)
FprintfWithIndent(&b, indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n",
*l.Color.H, *l.Color.S, *l.Color.K)
FprintfWithIndent(&b, indent, "Brightness: %.1f%%\n", l.Brightness*100)
if l.Effect != "" {
FprintfWithIndent(&b, indent, "Effect: %s\n", l.Effect)
}
FprintfWithIndent(&b, indent, "Product: %s\n", l.Product.Name)
FprintfWithIndent(&b, indent, "Capabilities: ")
fmt.Fprintf(&b, "Color: %s, ", YesNo(l.Product.Capabilities.HasColor))
fmt.Fprintf(&b, "Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp))
fmt.Fprintf(&b, "IR: %s, ", YesNo(l.Product.Capabilities.HasIR))
fmt.Fprintf(&b, "Chain: %s, ", YesNo(l.Product.Capabilities.HasChain))
fmt.Fprintf(&b, "Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone))
fmt.Fprintf(&b, "Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin)
fmt.Fprintf(&b, "Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin)
fmt.Fprintln(&b)
// List applicable selectors (most to least specific)
FprintfWithIndent(&b, indent, "Selectors:\n")
indent += Tabstop
FprintfWithIndent(&b, indent, "id:%s\n", l.Id)
FprintfWithIndent(&b, indent, "label:%s\n", l.Label)
FprintfWithIndent(&b, indent, "group_id:%s\n", l.Group.Id)
FprintfWithIndent(&b, indent, "group:%s\n", l.Group.Name)
FprintfWithIndent(&b, indent, "location_id:%s\n", l.Location.Id)
FprintfWithIndent(&b, indent, "location:%s\n", l.Location.Name)
indent -= Tabstop
FprintfWithIndent(&b, indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen)
if i < len(lights)-1 {
fmt.Fprintln(&b)
}
fmt.Print(b.String())
}
return ExitSuccess, nil
}
func connected(c bool) string {
if c {
return "Connected"
}
return "Disconnected"
}

View File

@ -2,33 +2,51 @@ package lumecmd
import (
"flag"
"fmt"
)
func init() {
var cmdName string = "toggle"
func NewCmdToggle() Command {
return Command{
Name: "toggle",
Func: ToggleCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
fs := flag.NewFlagSet(cmdName, flag.ExitOnError)
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
duration := fs.Float64("duration", defaultDuration, "Set the duration")
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
selector := fs.String("selector", defaultSelector, "Set the selector")
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
RegisterCommand(cmdName, Command{
Func: ToggleCmd,
Flags: fs,
})
return fs
}(),
Use: "[--selector <selector>] [--duration <sec>]",
Short: "Toggle the power on/off",
}
}
func ToggleCmd(args CmdArgs) (int, error) {
c := args.Client
duration := args.Flags.Float64("duration")
selector := args.Flags.String("selector")
func ToggleCmd(ctx Context) (int, error) {
var p Printer
c := ctx.Client
duration := ctx.Flags.Float64("duration")
selector := ctx.Flags.String("selector")
format, err := getOutputFormatFromFlags(ctx.Flags)
if err != nil {
return ExitFailure, err
}
if format == "" && ctx.Config.OutputFormat != "" {
format = ctx.Config.OutputFormat
}
r, err := c.Toggle(selector, duration)
if err != nil {
return 1, err
return ExitFailure, err
}
PrintResults(r.Results)
return 0, nil
p = NewPrinter(format)
fmt.Print(p.Results(r.Results))
return ExitSuccess, nil
}

View File

@ -2,108 +2,14 @@ package lumecmd
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"git.kill0.net/chill9/lume"
"git.kill0.net/chill9/lifx-go"
)
func powerColor(s string) string {
fs := "\033[1;31m%s\033[0m"
if s == "on" {
fs = "\033[1;32m%s\033[0m"
}
return fmt.Sprintf(fs, s)
}
func statusColor(s lifx.Status) string {
fs := "\033[1;31m%s\033[0m"
if s == "ok" {
fs = "\033[1;32m%s\033[0m"
}
return fmt.Sprintf(fs, s)
}
func PrintResults(res []lifx.Result) {
var length, idWidth, labelWidth, statusWidth int
for _, r := range res {
length = len(r.Id)
if idWidth < length {
idWidth = length
}
length = len(r.Label)
if labelWidth < length {
labelWidth = length
}
length = len(r.Status)
if statusWidth < length {
statusWidth = length
}
}
for _, r := range res {
fmt.Printf("%*s %*s %*s\n",
idWidth, r.Id,
labelWidth, r.Label,
statusWidth, statusColor(r.Status))
}
}
func PrintLights(lights []lifx.Light) {
var length int
for _, l := range lights {
length = len(l.Id)
if idWidth < length {
idWidth = length
}
length = len(l.Location.Name)
if locationWidth < length {
locationWidth = length
}
length = len(l.Group.Name)
if groupWidth < length {
groupWidth = length
}
length = len(l.Label)
if labelWidth < length {
labelWidth = length
}
length = len(l.LastSeen.Local().Format(time.RFC3339))
if lastSeenWidth < length {
lastSeenWidth = length
}
length = len(l.Power)
if powerWidth < length {
powerWidth = length
}
}
fmt.Printf("total %d\n", len(lights))
for _, l := range lights {
fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n",
idWidth, l.Id,
locationWidth, l.Location.Name,
groupWidth, l.Group.Name,
labelWidth, l.Label,
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
powerWidth, powerColor(l.Power),
)
}
}
func parseRGB(s string) (lifx.RGBColor, error) {
var c lifx.RGBColor
rgb := strings.SplitN(s, ",", 3)
@ -121,3 +27,42 @@ func parseRGB(s string) (lifx.RGBColor, error) {
}
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
}
func sortLights(lights []lifx.Light) {
sort.Slice(lights, func(i, j int) bool {
if lights[i].Group.Name < lights[j].Group.Name {
return true
}
if lights[i].Group.Name > lights[j].Group.Name {
return false
}
return lights[i].Label < lights[j].Label
})
}
func sortResults(res []lifx.Result) {
sort.Slice(res, func(i, j int) bool {
return res[i].Label < res[j].Label
})
}
func ExitWithCode(code int, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Exit(code)
}
func YesNo(v bool) string {
if v {
return "yes"
}
return "no"
}
func Debugf(format string, a ...interface{}) {
if GetConfig().Debug {
fmt.Printf(format, a...)
}
}

51
cmd/validate.go Normal file
View File

@ -0,0 +1,51 @@
package lumecmd
import (
"errors"
"flag"
"fmt"
"strings"
"git.kill0.net/chill9/lifx-go"
)
func NewCmdValidate() Command {
return Command{
Name: "validate",
Func: ValidateCmd,
Flags: func() *flag.FlagSet {
fs := flag.NewFlagSet("validate", flag.ExitOnError)
return fs
}(),
Use: "<command>",
Short: "Validate a color string",
}
}
func ValidateCmd(ctx Context) (int, error) {
var b strings.Builder
c := ctx.Client
if len(ctx.Args) != 1 {
fmt.Print(printCmdHelp(ctx.Name))
return ExitFailure, nil
}
color := lifx.NamedColor(ctx.Args[0])
i, err := c.ValidateColor(color)
if err != nil {
return ExitFailure, err
}
if validColor, ok := i.(*lifx.HSBKColor); ok {
fmt.Fprintln(&b, validColor)
} else {
return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
}
fmt.Print(b.String())
return ExitSuccess, nil
}

38
cmd/version.go Normal file
View File

@ -0,0 +1,38 @@
package lumecmd
import (
"fmt"
"runtime"
"strings"
)
func NewCmdVersion() Command {
return Command{
Name: "version",
Func: VersionCmd,
Flags: nil,
Use: "",
Short: "Show version",
}
}
func VersionCmd(ctx Context) (int, error) {
var b strings.Builder
fmt.Fprintf(&b, "lume, version %s\n", Version)
if GitCommit != "" {
fmt.Fprintf(&b, " revision: %s\n", GitCommit)
}
if BuildDate != "" {
fmt.Fprintf(&b, " build date: %s\n", BuildDate)
}
fmt.Fprintf(&b, " go version: %s\n", runtime.Version())
fmt.Fprintf(&b, " platform: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Print(b.String())
return ExitSuccess, nil
}

224
color.go
View File

@ -1,224 +0,0 @@
package lifx
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
type (
Color interface {
ColorString() string
}
)
type (
RGBColor struct {
R, G, B uint8
}
HSBKColor struct {
H *float32 `json:"hue"`
S *float32 `json:"saturation"`
B *float32 `json:"brightness"`
K *int16 `json:"kelvin"`
}
NamedColor string
)
const (
KelvinCandlelight = 1500
KelvinSunset = 2000
KelvinUltraWarm = 2500
KelvinIncandescent = 2700
KelvinWarm = 3000
KelvinCool = 4000
KelvinCoolDaylight = 4500
KelvinSoftDaylight = 5000
KelvinDaylight = 5600
KelvinNoonDaylight = 6000
KelvinBrightDaylight = 6500
KelvinCloudDaylight = 7000
KelvinBlueDaylight = 7500
KelvinBlueOvercast = 8000
KelvinBlueIce = 9000
HueWhite = 0
HueRed = 0
HueOrange = 36
HueYellow = 60
HueGreen = 120
HueCyan = 180
HueBlue = 250
HuePurple = 280
HuePink = 325
)
var (
DefaultWhites = map[string]int{
"candlelight": KelvinCandlelight,
"sunset": KelvinSunset,
"ultrawarm": KelvinUltraWarm,
"incandescent": KelvinIncandescent,
"warm": KelvinWarm,
"cool": KelvinCool,
"cooldaylight": KelvinCoolDaylight,
"softdaylight": KelvinSoftDaylight,
"daylight": KelvinDaylight,
"noondaylight": KelvinNoonDaylight,
"brightdaylight": KelvinBrightDaylight,
"clouddaylight": KelvinCloudDaylight,
"bluedaylight": KelvinBlueDaylight,
"blueovercast": KelvinBlueOvercast,
"blueice": KelvinBlueIce,
}
)
func NewRGBColor(r, g, b uint8) (RGBColor, error) {
var c RGBColor
if (r < 0 || r > 255) && (g < 0 || r > 255) && (b < 0 || b > 255) {
return c, errors.New("values must be between 0-255")
}
return RGBColor{R: r, G: g, B: b}, nil
}
func NewHSColor(h, s float32) (HSBKColor, error) {
var c HSBKColor
if h < 0 || h > 360 {
return c, errors.New("hue must be between 0.0-360.0")
}
if s < 0 || s > 1 {
return c, errors.New("saturation must be between 0.0-1.0")
}
c = HSBKColor{
H: Float32Ptr(h),
S: Float32Ptr(s),
}
return c, nil
}
func NewHSBColor(h, s, b float32) (HSBKColor, error) {
var c HSBKColor
if h < 0 || h > 360 {
return c, errors.New("hue must be between 0.0-360.0")
}
if s < 0 || s > 1 {
return c, errors.New("saturation must be between 0.0-1.0")
}
if b < 0 || b > 1 {
return c, errors.New("brightness must be between 0.0-1.0")
}
c = HSBKColor{
H: Float32Ptr(h),
S: Float32Ptr(s),
B: Float32Ptr(b),
}
return c, nil
}
func NewRed() (HSBKColor, error) { return NewHSColor(HueRed, 1) }
func NewOrange() (HSBKColor, error) { return NewHSColor(HueOrange, 1) }
func NewYellow() (HSBKColor, error) { return NewHSColor(HueYellow, 1) }
func NewGreen() (HSBKColor, error) { return NewHSColor(HueGreen, 1) }
func NewCyan() (HSBKColor, error) { return NewHSColor(HueCyan, 1) }
func NewPurple() (HSBKColor, error) { return NewHSColor(HuePurple, 1) }
func NewPink() (HSBKColor, error) { return NewHSColor(HuePink, 1) }
func NewWhite(k int16) (HSBKColor, error) {
var c HSBKColor
if k < 1500 || k > 9000 {
return c, errors.New("kelvin must be between 1500-9000")
}
c = HSBKColor{
H: Float32Ptr(HueWhite),
S: Float32Ptr(0.0),
K: Int16Ptr(k),
}
return c, nil
}
func NewWhiteString(s string) (HSBKColor, error) {
k, ok := DefaultWhites[s]
if !ok {
return HSBKColor{}, fmt.Errorf("'%s' is not a valid default white", s)
}
return NewWhite(int16(k))
}
func (c RGBColor) ColorString() string {
return fmt.Sprintf("rgb:%d,%d,%d", c.R, c.G, c.B)
}
func (c RGBColor) Hex() string {
return fmt.Sprintf("#%x%x%x", c.R, c.G, c.B)
}
func (c HSBKColor) ColorString() string {
var s []string
if c.H != nil {
s = append(s, fmt.Sprintf("hue:%g", *c.H))
}
if c.S != nil {
s = append(s, fmt.Sprintf("saturation:%g", *c.S))
}
if c.B != nil {
s = append(s, fmt.Sprintf("brightness:%g", *c.B))
}
if c.K != nil {
s = append(s, fmt.Sprintf("kelvin:%d", *c.K))
}
return strings.Join(s, " ")
}
func (c HSBKColor) MarshalText() ([]byte, error) {
return []byte(c.ColorString()), nil
}
func (c RGBColor) MarshalText() ([]byte, error) {
return []byte(c.ColorString()), nil
}
func (c NamedColor) ColorString() string {
return string(c)
}
func (c *Client) ValidateColor(color Color) (Color, error) {
var (
err error
s *HSBKColor
r *http.Response
resp *Response
)
if resp, err = c.validateColor(color); err != nil {
return nil, err
}
resp, err = NewResponse(r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}

View File

@ -1,35 +0,0 @@
package lifx
import (
"fmt"
"net/url"
"path"
)
func BuildURL(rawurl, rawpath string) string {
u, _ := url.Parse(rawurl)
u.Path = path.Join(u.Path, rawpath)
return u.String()
}
var (
Endpoint = "https://api.lifx.com/v1"
EndpointState = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector))
}
EndpointStateDelta = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state/delta", selector))
}
EndpointListLights = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s", selector))
}
EndpointStates = func() string {
return BuildURL(Endpoint, "/lights/states")
}
EndpointColor = func() string {
return BuildURL(Endpoint, "/color")
}
EndpointToggle = func(selector string) string {
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
}
)

13
go.mod Normal file
View File

@ -0,0 +1,13 @@
module git.kill0.net/chill9/lume
go 1.15
require (
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73
github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.10.0
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181
)

33
go.sum Normal file
View File

@ -0,0 +1,33 @@
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef h1:8yyXAk+qiRvrowTeHHAOBZqGXXCr8SxWFydez61ZGp8=
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef/go.mod h1:ZFKIcwdJ4Nqlrkn/eUHbeLt0NVhFsfxBREkVoA+jzUc=
git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621 h1:koWq2W08HjmvsNm4D8kD0OjMZKCJ/3681P9XFMTj77I=
git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 h1:tN+zR5aszesrZRrhS3uOqAIWLcADCIH7GFJ6SOQS9r0=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73 h1:fteCAelwAcfam2Q8eeJFyK4+sXGOpR6Me5YMKBi+MYY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181 h1:64ChN/hjER/taL4YJuA+gpLfIMT+/NFherRZixbxOhg=
golang.org/x/sys v0.0.0-20210326220804-49726bf1d181/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

242
lights.go
View File

@ -1,242 +0,0 @@
package lifx
import (
//"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
const (
OK Status = "ok"
TimedOut Status = "timed_out"
Offline Status = "offline"
)
type (
Status string
Selector struct {
Id string `json:"id"`
Name string `json:"name"`
}
Product struct {
Name string `json:"name"`
Identifier string `json:"identifier"`
Company string `json:"company"`
Capabilities Capabilities `json:"capabilities"`
}
Capabilities struct {
HasColor bool `json:"has_color"`
HasVariableColorTemp bool `json:"has_variable_color_temp"`
HasIR bool `json:"has_ir"`
HasChain bool `json:"has_chain"`
HasMultizone bool `json:"has_multizone"`
MinKelvin float64 `json:"min_kelvin"`
MaxKelvin float64 `json:"max_kelvin"`
}
Light struct {
Id string `json:"id"`
UUID string `json:"uuid"`
Label string `json:"label"`
Connected bool `json:"connected"`
Power string `json:"power"`
Color HSBKColor `json:"color"`
Brightness float64 `json:"brightness"`
Effect string `json:"effect"`
Group Selector `json:"group"`
Location Selector `json:"location"`
Product Product `json:"product"`
LastSeen time.Time `json:"last_seen"`
SecondsLastSeen float64 `json:"seconds_last_seen"`
}
State struct {
Power string `json:"power,omitempty"`
Color Color `json:"color,omitempty"`
Brightness float64 `json:"brightness,omitempty"`
Duration float64 `json:"duration,omitempty"`
Infrared float64 `json:"infrared,omitempty"`
Fast bool `json:"fast,omitempty"`
}
StateDelta struct {
Power *string `json:"power,omitempty"`
Duration *float64 `json:"duration,omitempty"`
Infrared *float64 `json:"infrared,omitempty"`
Hue *float64 `json:"hue,omitempty"`
Saturation *float64 `json:"saturation,omitempty"`
Brightness *float64 `json:"brightness,omitempty"`
Kelvin *int `json:"kelvin,omitempty"`
}
StateWithSelector struct {
State
Selector string `json:"selector"`
}
States struct {
States []StateWithSelector `json:"states",omitempty`
Defaults State `json:"defaults",omitempty`
}
Toggle struct {
Duration float64 `json:"duration,omitempty"`
}
)
func NewApiError(resp *Response) error {
var (
s *LifxResponse
err error
)
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return err
}
return errors.New(s.Error)
}
func IsApiError(resp *Response) bool {
return resp.StatusCode > 299
}
func (s Status) Success() bool {
return s == OK
}
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.setState(selector, state); err != nil {
return nil, err
}
defer resp.Body.Close()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if state.Fast && resp.StatusCode == http.StatusAccepted {
return nil, nil
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) FastSetState(selector string, state State) (*LifxResponse, error) {
state.Fast = true
return c.SetState(selector, state)
}
func (c *Client) SetStates(selector string, states States) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.setStates(selector, states); err != nil {
return nil, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.stateDelta(selector, delta); err != nil {
return nil, err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error) {
var (
err error
s *LifxResponse
resp *Response
)
if resp, err = c.toggle(selector, duration); err != nil {
return nil, err
}
defer resp.Body.Close()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) ListLights(selector string) ([]Light, error) {
var (
err error
s []Light
resp *Response
)
if resp, err = c.listLights(selector); err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
return nil, NewApiError(resp)
}
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return nil, err
}
return s, nil
}
func (c *Client) PowerOff(selector string) (*LifxResponse, error) {
return c.SetState(selector, State{Power: "off"})
}
func (c *Client) FastPowerOff(selector string) {
c.SetState(selector, State{Power: "off", Fast: true})
}
func (c *Client) PowerOn(selector string) (*LifxResponse, error) {
return c.SetState(selector, State{Power: "on"})
}
func (c *Client) FastPowerOn(selector string) {
c.SetState(selector, State{Power: "on", Fast: true})
}

186
lume.1 Normal file
View File

@ -0,0 +1,186 @@
.Dd $Mdocdate$
.Dt lume \&1 "User Commands"
.Sh NAME
.Nm lume
.Nd CLI tool for the LIFX HTTP API
.Sh SYNOPSIS
.Nm lume
.Ar COMMAND
.Op Ar OPTIONS ...
.Sh COMMANDS
.Bl -tag -width Ds
.It Xo Ic breathe
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Fl c | Fl Fl color
.Op Fl Fl from-color Ns = Ns Ar color
.Op Fl Fl cycles Ns = Ns Ar cycles
.Op Fl Fl peak Ns = Ns Ar peak
.Op Fl Fl period Ns = Ns Ar period
.Op Fl Fl persist
.Op Fl Fl power-on
.Xc
.It Xo Ic help
.Op Ar COMMAND
.Xc
Print the help message. If a command is specified, then show the subcommand's help message.
.It Xo Ic ls
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
List the lights and their basic state
.It Xo Ic poweroff
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Op Fl d | Fl Fl duration
.Xc
Power off lights
.It Xo Ic poweron
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Op Fl d | Fl Fl duration
.Xc
Power off lights
.It Xo Ic set-color
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl H Ar hue | Fl Fl hue Ns = Ns Ar hue
.Op Fl n Ar name | Fl Fl name Ns = Ns Ar name
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl r Ar rgb | Fl Fl rgb Ns = Ns Ar rgb
.Op Fl S Ar saturation | Fl Fl saturation Ns = Ns Ar saturation
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light color
.It Xo Ic set-state
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl c | Fl Fl color
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl i Ar infrared | Fl Fl infrared Ns = Ns Ar infrared
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light properties
.It Xo Ic set-white
.Op Fl b Ar brightness | Fl Fl brightness Ns = Ns Ar brightness
.Op Fl d | Fl Fl duration
.Op Fl f Ar fast | Fl Fl fast Ns = Ns Ar fast
.Op Fl i Ar infrared | Fl Fl infrared Ns = Ns Ar infrared
.Op Fl k Ar kelvin | Fl Fl kelvin Ns = Ns Ar kelvin
.Op Fl n Ar name | Fl Fl name Ns = Ns Ar name
.Op Fl p Ar power | Fl Fl power Ns = Ns Ar power
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Set light white levels
.It Xo Ic show
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Xc
Show extended details about the lights
.It Xo Ic toggle
.Op Fl d | Fl Fl duration
.Op Fl s Ar selector | Fl Fl selector Ns = Ns Ar selector
.Op Fl Fl simple | Fl Fl table
.Xc
Toggle the power
.It Xo Ic validate
.Ar color_string
.Xc
Validate a color string
.It Xo Ic version
.Xc
Print the version
.El
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl b , Fl Fl brightness Ns = Ns Ar brightness
The brightness level from 0.0 to 1.0. Overrides any brightness set in color (if any)
.It Fl c , Fl Fl color Ns = Ns Ar color
This color to use for the action.
.Pp
When used with the
.Nm breathe
effect, this
is the "to" color.
.It Fl Fl cycles Ns = Ns Ar cycles
The number of times to repeat the effect. Defaults to 1.0 cycle.
.It Fl d , Fl Fl duration Ns = Ns Ar duration
The time in seconds to spend performing the action. Range: 0.0 3155760000.0 (100 years).
.It Fl f , Fl Fl fast
Execute the action fast without any state checks or waiting for the result from the HTTP API
.It Fl Fl from-color Ns = Ns Ar color
The color to start the
.Nm breathe
effect from. If this parameter is omitted
then the color the bulb is currently set to is used instead.
.It Fl H , Fl Fl hue Ns = Ns Ar hue
Sets the hue. Range 0 - 360.
.It Fl i , Fl Fl infrared Ns = Ns Ar infrared
Sets the maximum brightness of the infrared channel from 0.0 to 1.0.
.It Fl k , Fl Fl kelvin Ns = Ns Ar kelvin
Set the kelvin value. The saturation is automatically set to 0.
.It Fl n , Fl Fl name Ns = Ns Ar name
Set the color using a named color from
.Pa lumerc
.It Fl Fl peak Ns = Ns Ar peak
Defines where in a period the target color is at its maximum. Minimum 0.0,
maximum 1.0. Defaults to 0.5.
.It Fl Fl period Ns = Ns Ar period
The time in seconds for one cycle of the
.Nm breathe
effect. Defaults to 1.0
second.
.It Fl Fl persist
If false set the light back to its previous value when effect ends, if true
leave the last effect color.
.It Fl p , Fl Fl power Ns = Ns Ar [ on | off ]
Set the power state
.It Fl Fl power-on
If true, turn the bulb on if it is not already on.
.It Fl r , Fl Fl rgb Ns = Ns Ar R,G,B
Set the color via a comma delimited R,G,B string. Values range from 0 - 255.
.It Fl S , Fl Fl saturation Ns = Ns Ar saturation
Set the saturation. Range 0.0 - 1.0.
.It Fl s , Fl Fl selector Ns = Ns Ar selector
The selector is used to group lights together belonging in the same account
.Pp
Selectors can be in the following format: all, label:[value], id:[value], group_id:[value], group:[value], location_id:[value], location:[value], scene_id:[value]
.Pp
The default selector is "all"
.Sh FILES
.Bl -tag -width "~/.config/lume/lume.conf" -compact
.It Pa ~/.lumerc
Default
.Nm
configuration file
.It Pa ~/.config/lume/lume.conf
XDG config home
.Nm
configuration file
.Sh EXAMPLES
Sample
.Pa lumerc
file:
.Bd -literal -offset indent
access_token = "token"
# indicator = "●"
# output_format = "table"
[colors]
purple_candy = [ 280.0, 0.29, 0.71 ]
wasabi = [ 120.0, 1.0, 0.7 ]
honeydew = [ 120.0, 1.0, 0.97 ]
green_mist = [ 92.0, 0.72, 0.75 ]
pea = [ 90.0, 0.42, 0.47 ]
cat_eye = [ 76.0, 0.74, 0.61 ]
seagreen = [ 160.0, 1.0, 0.50 ]
blue_mist = [ 202.0, 0.97, 0.75 ]
.Ed

12
lume.code-workspace Normal file
View File

@ -0,0 +1,12 @@
{
"folders": [
{
"path": "."
},
{
"name": "lifx-go",
"path": "..\\lifx-go"
}
],
"settings": {}
}

View File

@ -0,0 +1,5 @@
lume (__VERSION__-__REVISION__) unstable; urgency=medium
* Package generated with make deb
-- Ryan Cavicchioni <ryan@cavi.cc> __DATE__

14
packaging/debian/control Normal file
View File

@ -0,0 +1,14 @@
Source: lume
Section: unknown
Priority: optional
Maintainer: Ryan Cavicchioni <ryan@cavi.cc>
Build-Depends: debhelper (>= 10)
Standards-Version: 4.1.2
Homepage: https://git.kill0.net/chill9/lume
Vcs-Git: https://git.kill0.net/chill9/lume.git
Vcs-Browser: https://git.kill0.net/chill9/lume.git
Package: lume
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: A CLI tool for the LIFX HTTP API

View File

@ -0,0 +1,28 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: lume
Source: https://git.kill0.net/chill9/lume
Files: *
Copyright: 2021 Ryan Cavicchioni <ryan@cavi.cc>
License: MPL-2.0
License: MPL-2.0
Licensed under the Mozilla Public License License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may
obtain a copy of the License at
.
https://www.mozilla.org/en-US/MPL/2.0/
.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.
On Debian systems, the complete text of the Mozilla Public License version 2.0
license can be found in "/usr/share/common-licenses/MPL-2.0".
# Please also look if there are files or directories which have a
# different copyright/license attached and list them here.
# Please avoid picking licenses with terms that are more restrictive than the
# packaged work, as it may make Debian's contributions unacceptable upstream.

View File

@ -0,0 +1 @@
lume.1

19
packaging/debian/rules Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/make -f
DISTRIBUTION = $(shell lsb_release -sr)
VERSION = __VERSION__
REVISION = __REVISION__
PACKAGEVERSION = $(VERSION)-$(REVISION)
%:
dh $@
override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
make
override_dh_auto_install:
make install DESTDIR=debian/lume
override_dh_gencontrol:
dh_gencontrol -- -v$(PACKAGEVERSION)

33
packaging/rpm/lume.spec Normal file
View File

@ -0,0 +1,33 @@
Name: lume
Version: __VERSION__
Release: __REVISION__%{?dist}
Summary: A CLI tool for the LIFX HTTP API
License: MPL
URL: https://git.kill0.net/chill9/lume
Source: %{name}-%{version}.tar.xz
%global debug_package %{nil}
%description
%prep
%setup
%build
%make_build
%install
%make_install DESTDIR=%{buildroot}
%files
%{_bindir}/lume
%license LICENSE
/usr/share/lume/lumerc
%doc %{_mandir}/man1/lume.1.*
%changelog
* __DATE__ Ryan Cavicchioni <ryan@cavi.cc>
- lume __VERSION__

View File

@ -1,7 +0,0 @@
package lifx
func StringPtr(v string) *string { return &v }
func Float64Ptr(v float64) *float64 { return &v }
func Float32Ptr(v float32) *float32 { return &v }
func IntPtr(v int) *int { return &v }
func Int16Ptr(v int16) *int16 { return &v }