Skip to content

Commit 330a0cc

Browse files
committed
Convert to library
1 parent 4563ec9 commit 330a0cc

File tree

10 files changed

+108
-16
lines changed

10 files changed

+108
-16
lines changed

boards.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package agent
22

33
import exec "github.com/arduino/arduino-create-agent/exec"
44

cli/agent/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of arduino-create-agent.
3+
*
4+
* arduino-create-agent is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
28+
*/
29+
package main
30+
31+
import (
32+
"flag"
33+
34+
"github.com/arduino/arduino-create-agent"
35+
)
36+
37+
func main() {
38+
var (
39+
hibernate = flag.Bool("hibernate", false, "start hibernated")
40+
)
41+
opts := agent.Opts{
42+
Hibernate: *hibernate,
43+
}
44+
agent.Start(opts)
45+
}

commands_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"github.com/arduino/arduino-create-agent/app"

connect_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"bytes"

discover_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"github.com/arduino/arduino-create-agent/app"

main.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
//go:generate go run cli/gen/main.go
3030

31-
package main
31+
package agent
3232

3333
import (
3434
"flag"
@@ -50,11 +50,22 @@ import (
5050
"github.com/kardianos/osext"
5151
)
5252

53-
func main() {
53+
// Opts is a configuration struct used to change the behaviour of the agent
54+
type Opts struct {
55+
// If Hibernate is true the agent will display a grey trayicon and won't answer
56+
// to any request
57+
Hibernate bool
58+
59+
// HTTPPort and HTTPSPort are the ports over which the agent will listen
60+
// If they are not provided the agent will choose a free port in the range 8990-8999
61+
HTTPPort, HTTPSPort int
62+
}
63+
64+
// Start a webserver listening on the provided ports
65+
func Start(opts Opts) {
5466
var (
55-
hibernate = flag.Bool("hibernate", false, "start hibernated")
56-
version = "x.x.x-dev" //don't modify it, Jenkins will take care
57-
revision = "xxxxxxxx" //don't modify it, Jenkins will take care
67+
version = "x.x.x-dev" //don't modify it, Jenkins will take care
68+
revision = "xxxxxxxx" //don't modify it, Jenkins will take care
5869
)
5970

6071
flag.Parse()
@@ -89,14 +100,22 @@ func main() {
89100
app.MountPublicController(service, public)
90101

91102
// Mount systray
92-
restart := restartFunc("", !*hibernate)
103+
restart := restartFunc("", !opts.Hibernate)
93104
shutdown := func() {
94105
os.Exit(0)
95106
}
96107

108+
// Find boards
97109
http, https := findPorts()
110+
if opts.HTTPPort != 0 {
111+
http = opts.HTTPPort
112+
}
113+
if opts.HTTPSPort != 0 {
114+
http = opts.HTTPSPort
115+
}
116+
98117
address := "http://localhost:" + strconv.Itoa(http)
99-
if !*hibernate {
118+
if !opts.Hibernate {
100119
// Start http service
101120
go func() {
102121
if err := service.ListenAndServe(":" + strconv.Itoa(http)); err != nil {
@@ -111,7 +130,7 @@ func main() {
111130
}()
112131
}
113132

114-
setupSystray(*hibernate, version, revision, address, restart, shutdown)
133+
setupSystray(opts.Hibernate, version, revision, address, restart, shutdown)
115134
}
116135

117136
// findPorts returns the first two available ports for http and https listening

public.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"github.com/goadesign/goa"

systray.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"os"

tools_v1.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
package main
1+
/*
2+
* This file is part of arduino-create-agent.
3+
*
4+
* arduino-create-agent is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
28+
*/
29+
package agent
230

331
import (
432
"github.com/arduino/arduino-create-agent/app"

upload_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* Copyright 2017 ARDUINO AG (http://www.arduino.cc/)
2828
*/
29-
package main
29+
package agent
3030

3131
import (
3232
"github.com/arduino/arduino-create-agent/app"

0 commit comments

Comments
 (0)