diff --git a/.gitignore b/.gitignore index 0dd0591bbc0..199c1fe75e4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /cmd/formatter/debug.test /arduino-cli.yaml /wiki +.idea +coverage_*.txt \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 766eaac31cb..e0792890dbf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ env: # Make sure golangci-lint is vendored. before_install: - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.16.0 + - curl -sL https://taskfile.dev/install.sh | sh install: true @@ -20,9 +21,10 @@ script: # Run linter - golangci-lint run # Build and test - - go build - - go test -timeout 20m -v -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic ./... + - ./bin/task build + - ./bin/task test after_success: - - bash <(curl -s https://codecov.io/bash) + - bash <(curl -s https://codecov.io/bash) -cF unittests,integration + diff --git a/README.md b/README.md index 41703cce189..e282ed85425 100644 --- a/README.md +++ b/README.md @@ -333,3 +333,20 @@ Because: #### How can I find the core/FQBN for a board? See: https://github.com/arduino/arduino-cli#step-4-find-and-install-the-right-core + +# Testing + +Currently Unit and Integration test are available for launch in 2 ways: + +1. classic `go test ./...` to launch both unit and integration test + +2. via [task](https://taskfile.dev) that includes the following options: + +``` +* build: Build the project +* test: Run the full testsuite +* test-integration: Run integration tests only +* test-unit: Run unit tests only +``` + +For Example to launch unit tests only run: `task test-unit` \ No newline at end of file diff --git a/Taskfile.yml b/Taskfile.yml index 9cf78a338ba..e72d85d6fcc 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,7 +4,7 @@ tasks: build: desc: Build the project cmds: - - go build -v -i + - go build -v -i {{.LDFLAGS}} test: desc: Run the full testsuite @@ -15,9 +15,26 @@ tasks: test-unit: desc: Run unit tests only cmds: - - go test -short {{ default "-v" .GOFLAGS }} {{ default "./..." .TARGETS }} + - go test -short {{ default "-v" .GOFLAGS }} -coverprofile=coverage_unit.txt {{ default "./..." .TARGETS }} test-integration: desc: Run integration tests only cmds: - - go test -run Integration {{ default "-v" .GOFLAGS }} {{ default "./..." .TARGETS }} + - go test -run Integration {{ default "-v" .GOFLAGS }} -coverprofile=coverage_integ.txt {{ default "./..." .TARGETS }} {{.TEST_LDFLAGS}} + +vars: + # build vars + VERSIONSTRING: "0.3.6-alpha.preview" + COMMIT: + sh: echo ${TRAVIS_COMMIT:-`git log -n 1 --format=%h`} + LDFLAGS: > + -ldflags '-X github.com/arduino/arduino-cli/version.versionString={{.VERSIONSTRING}} + -X github.com/arduino/arduino-cli/version.commit={{.COMMIT}}' + + # test vars + GOFLAGS: "-timeout 5m -v -coverpkg=./... -covermode=atomic" + TEST_VERSIONSTRING: "0.0.0-test.preview" + TEST_COMMIT: "deadbeef" + TEST_LDFLAGS: > + -ldflags '-X github.com/arduino/arduino-cli/version.versionString={{.TEST_VERSIONSTRING}} + -X github.com/arduino/arduino-cli/version.commit={{.TEST_COMMIT}}' \ No newline at end of file diff --git a/arduino/cores/packagemanager/download.go b/arduino/cores/packagemanager/download.go index b62488c8013..15c4b58916a 100644 --- a/arduino/cores/packagemanager/download.go +++ b/arduino/cores/packagemanager/download.go @@ -19,6 +19,7 @@ package packagemanager import ( "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/cores" "go.bug.st/downloader" @@ -102,16 +103,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc // DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader // is returned. -func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*downloader.Downloader, error) { +func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) { resource := tool.GetCompatibleFlavour() if resource == nil { return nil, fmt.Errorf("tool not available for your OS") } - return resource.Download(pm.DownloadDir) + return resource.Download(pm.DownloadDir, downloaderHeaders) } // DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a // nil Downloader is returned. -func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*downloader.Downloader, error) { - return platform.Resource.Download(pm.DownloadDir) +func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) { + return platform.Resource.Download(pm.DownloadDir, downloaderHeaders) } diff --git a/arduino/resources/helpers.go b/arduino/resources/helpers.go index 8894a630067..becdbf5b6a9 100644 --- a/arduino/resources/helpers.go +++ b/arduino/resources/helpers.go @@ -19,9 +19,10 @@ package resources import ( "fmt" + "net/http" "os" - paths "github.com/arduino/go-paths-helper" + "github.com/arduino/go-paths-helper" "go.bug.st/downloader" ) @@ -45,7 +46,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) { } // Download a DownloadResource. -func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downloader, error) { +func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders http.Header) (*downloader.Downloader, error) { cached, err := r.TestLocalArchiveIntegrity(downloadDir) if err != nil { return nil, fmt.Errorf("testing local archive integrity: %s", err) @@ -73,5 +74,7 @@ func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downlo return nil, fmt.Errorf("getting archive file info: %s", err) } - return downloader.Download(path.String(), r.URL) + downloadConfig := downloader.Config{ + RequestHeaders: downloaderHeaders} + return downloader.DownloadWithConfig(path.String(), r.URL, downloadConfig) } diff --git a/arduino/resources/helpers_test.go b/arduino/resources/helpers_test.go new file mode 100644 index 00000000000..702634f3df5 --- /dev/null +++ b/arduino/resources/helpers_test.go @@ -0,0 +1,82 @@ +/* + * This file is part of arduino-cli. + * + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) + * + * This software is released under the GNU General Public License version 3, + * which covers the main part of arduino-cli. + * The terms of this license can be found at: + * https://www.gnu.org/licenses/gpl-3.0.en.html + * + * You can be released from the requirements of the above licenses by purchasing + * a commercial license. Buying such a license is mandatory if you want to modify or + * otherwise use the software for commercial activities involving the Arduino + * software without disclosing the source code of your own applications. To purchase + * a commercial license, send an email to license@arduino.cc. + */ + +package resources + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" +) + +type EchoHandler struct{} + +// EchoHandler echos back the request as a response if used as http handler +func (h *EchoHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { + request.Write(writer) +} + +func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) { + goldUserAgentValue := fmt.Sprintf("arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111") + goldUserAgentString := "User-Agent: " + goldUserAgentValue + + tmp, err := paths.MkTempDir("", "") + require.NoError(t, err) + defer tmp.RemoveAll() + + // startup echo server + srv := httptest.NewServer(&EchoHandler{}) + defer srv.Close() + + r := &DownloadResource{ + ArchiveFileName: "echo.txt", + CachePath: "cache", + URL: srv.URL, + } + + d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}}) + require.NoError(t, err) + err = d.Run() + require.NoError(t, err) + + // leverage the download helper to download the echo for the request made by the downloader itself + // + // expect something like: + // GET /echo HTTP/1.1 + // Host: 127.0.0.1:64999 + // User-Agent: arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111 + // Accept-Encoding: gzip + + b, err := ioutil.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name + require.NoError(t, err) + + requestLines := strings.Split(string(b), "\r\n") + userAgentHeaderString := "" + for _, line := range requestLines { + if strings.Contains(line, "User-Agent: ") { + userAgentHeaderString = line + } + } + require.Equal(t, goldUserAgentString, userAgentHeaderString) + +} diff --git a/arduino/resources/resources_test.go b/arduino/resources/resources_test.go index 058e8959c0b..82404b30d54 100644 --- a/arduino/resources/resources_test.go +++ b/arduino/resources/resources_test.go @@ -20,6 +20,7 @@ package resources import ( "crypto" "encoding/hex" + "net/http" "testing" paths "github.com/arduino/go-paths-helper" @@ -43,7 +44,7 @@ func TestDownloadAndChecksums(t *testing.T) { require.NoError(t, err) downloadAndTestChecksum := func() { - d, err := r.Download(tmp) + d, err := r.Download(tmp, http.Header{}) require.NoError(t, err) err = d.Run() require.NoError(t, err) @@ -59,7 +60,7 @@ func TestDownloadAndChecksums(t *testing.T) { downloadAndTestChecksum() // Download with cached file - d, err := r.Download(tmp) + d, err := r.Download(tmp, http.Header{}) require.NoError(t, err) require.Nil(t, d) diff --git a/cli/board/attach.go b/cli/board/attach.go index df59516027e..a1fc5563a79 100644 --- a/cli/board/attach.go +++ b/cli/board/attach.go @@ -33,9 +33,9 @@ func initAttachCommand() *cobra.Command { Use: "attach | [sketchPath]", Short: "Attaches a sketch to a board.", Long: "Attaches a sketch to a board.", - Example: " " + cli.AppName + " board attach serial:///dev/tty/ACM0\n" + - " " + cli.AppName + " board attach serial:///dev/tty/ACM0 HelloWorld\n" + - " " + cli.AppName + " board attach arduino:samd:mkr1000", + Example: " " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0\n" + + " " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 HelloWorld\n" + + " " + cli.VersionInfo.Application + " board attach arduino:samd:mkr1000", Args: cobra.RangeArgs(1, 2), Run: runAttachCommand, } diff --git a/cli/board/board.go b/cli/board/board.go index 21a99b6cf71..5a29328ac32 100644 --- a/cli/board/board.go +++ b/cli/board/board.go @@ -29,9 +29,9 @@ func InitCommand() *cobra.Command { Short: "Arduino board commands.", Long: "Arduino board commands.", Example: " # Lists all connected boards.\n" + - " " + cli.AppName + " board list\n\n" + + " " + cli.VersionInfo.Application + " board list\n\n" + " # Attaches a sketch to a board.\n" + - " " + cli.AppName + " board attach serial:///dev/tty/ACM0 mySketch", + " " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 mySketch", } boardCommand.AddCommand(initAttachCommand()) boardCommand.AddCommand(initDetailsCommand()) diff --git a/cli/board/details.go b/cli/board/details.go index 86a9c588f3b..5239a9be999 100644 --- a/cli/board/details.go +++ b/cli/board/details.go @@ -35,7 +35,7 @@ func initDetailsCommand() *cobra.Command { Use: "details ", Short: "Print details about a board.", Long: "Show information about a board, in particular if the board has options to be specified in the FQBN.", - Example: " " + cli.AppName + " board details arduino:avr:nano", + Example: " " + cli.VersionInfo.Application + " board details arduino:avr:nano", Args: cobra.ExactArgs(1), Run: runDetailsCommand, } diff --git a/cli/board/list.go b/cli/board/list.go index 9bf5091b1ad..280c0eb6db8 100644 --- a/cli/board/list.go +++ b/cli/board/list.go @@ -37,7 +37,7 @@ func initListCommand() *cobra.Command { Use: "list", Short: "List connected boards.", Long: "Detects and displays a list of connected boards to the current computer.", - Example: " " + cli.AppName + " board list --timeout 10s", + Example: " " + cli.VersionInfo.Application + " board list --timeout 10s", Args: cobra.NoArgs, Run: runListCommand, } diff --git a/cli/board/listall.go b/cli/board/listall.go index e7da46c451b..533d33eeda3 100644 --- a/cli/board/listall.go +++ b/cli/board/listall.go @@ -39,8 +39,8 @@ func initListAllCommand() *cobra.Command { "List all boards that have the support platform installed. You can search\n" + "for a specific board if you specify the board name", Example: "" + - " " + cli.AppName + " board listall\n" + - " " + cli.AppName + " board listall zero", + " " + cli.VersionInfo.Application + " board listall\n" + + " " + cli.VersionInfo.Application + " board listall zero", Args: cobra.ArbitraryArgs, Run: runListAllCommand, } diff --git a/cli/cli.go b/cli/cli.go index 7e2f61a873a..54e028e0f1b 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -20,8 +20,11 @@ package cli import ( "context" "errors" + "fmt" + "net/http" "os" "path/filepath" + "runtime" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" @@ -29,7 +32,8 @@ import ( "github.com/arduino/arduino-cli/common/formatter" "github.com/arduino/arduino-cli/configs" "github.com/arduino/arduino-cli/rpc" - paths "github.com/arduino/go-paths-helper" + "github.com/arduino/arduino-cli/version" + "github.com/arduino/go-paths-helper" "github.com/sirupsen/logrus" ) @@ -48,8 +52,13 @@ const ( ErrBadArgument ) -// Version is the current CLI version -var Version = "0.3.6-alpha.preview" +// appName is the command line name of the Arduino CLI executable on the user system (users may change it) +var appName = filepath.Base(os.Args[0]) + +// VersionInfo contains all info injected during build +var VersionInfo = version.NewInfo(appName) + +var HTTPClientHeader = getHTTPClientHeader() // ErrLogrus represents the logrus instance, which has the role to // log all non info messages. @@ -61,9 +70,6 @@ var GlobalFlags struct { OutputJSON bool // true output in JSON, false output as Text } -// AppName is the command line name of the Arduino CLI executable -var AppName = filepath.Base(os.Args[0]) - var Config *configs.Configuration func packageManagerInitReq() *rpc.InitReq { @@ -83,10 +89,18 @@ func packageManagerInitReq() *rpc.InitReq { return &rpc.InitReq{Configuration: conf} } +func getHTTPClientHeader() http.Header { + userAgentValue := fmt.Sprintf("%s/%s (%s; %s; %s) Commit:%s/Build:%s", VersionInfo.Application, + VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), VersionInfo.Commit, VersionInfo.BuildDate) + downloaderHeaders := http.Header{"User-Agent": []string{userAgentValue}} + return downloaderHeaders +} + func InitInstance() *rpc.InitResp { logrus.Info("Initializing package manager") req := packageManagerInitReq() - resp, err := commands.Init(context.Background(), req, OutputProgressBar(), OutputTaskProgress()) + + resp, err := commands.Init(context.Background(), req, OutputProgressBar(), OutputTaskProgress(), HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error initializing package manager") os.Exit(ErrGeneric) @@ -116,7 +130,7 @@ func CreateInstance() *rpc.Instance { for _, err := range resp.GetPlatformsIndexErrors() { formatter.PrintError(errors.New(err), "Error loading index") } - formatter.PrintErrorMessage("Launch '" + AppName + " core update-index' to fix or download indexes.") + formatter.PrintErrorMessage("Launch '" + VersionInfo.Application + " core update-index' to fix or download indexes.") os.Exit(ErrGeneric) } return resp.GetInstance() diff --git a/cli/compile/compile.go b/cli/compile/compile.go index 0ac8ccaca88..d01274dce98 100644 --- a/cli/compile/compile.go +++ b/cli/compile/compile.go @@ -35,7 +35,7 @@ func InitCommand() *cobra.Command { Use: "compile", Short: "Compiles Arduino sketches.", Long: "Compiles Arduino sketches.", - Example: " " + cli.AppName + " compile -b arduino:avr:uno /home/user/Arduino/MySketch", + Example: " " + cli.VersionInfo.Application + " compile -b arduino:avr:uno /home/user/Arduino/MySketch", Args: cobra.MaximumNArgs(1), Run: run, } diff --git a/cli/config/config.go b/cli/config/config.go index 9a7d362da88..a03e0a0b083 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -27,7 +27,7 @@ func InitCommand() *cobra.Command { configCommand := &cobra.Command{ Use: "config", Short: "Arduino Configuration Commands.", - Example: " " + cli.AppName + " config init", + Example: " " + cli.VersionInfo.Application + " config init", } configCommand.AddCommand(initDumpCommand()) configCommand.AddCommand(initInitCommand()) diff --git a/cli/config/dump.go b/cli/config/dump.go index 3e2843aa237..c90daf246d6 100644 --- a/cli/config/dump.go +++ b/cli/config/dump.go @@ -32,7 +32,7 @@ func initDumpCommand() *cobra.Command { Use: "dump", Short: "Prints the current configuration", Long: "Prints the current configuration.", - Example: " " + cli.AppName + " config dump", + Example: " " + cli.VersionInfo.Application + " config dump", Args: cobra.NoArgs, Run: runDumpCommand, } diff --git a/cli/config/init.go b/cli/config/init.go index 5c9be35e1a6..c2068c9e3c8 100644 --- a/cli/config/init.go +++ b/cli/config/init.go @@ -33,9 +33,9 @@ func initInitCommand() *cobra.Command { Long: "Initializes a new config file into the default location ($EXE_DIR/cli-config.yml).", Example: "" + " # Creates a config file by asking questions to the user into the default location.\n" + - " " + cli.AppName + " config init\n\n" + + " " + cli.VersionInfo.Application + " config init\n\n" + " # Creates a config file with default configuration into default location.\n" + - " " + cli.AppName + " config init --default\n", + " " + cli.VersionInfo.Application + " config init --default\n", Args: cobra.NoArgs, Run: runInitCommand, } diff --git a/cli/core/core.go b/cli/core/core.go index 024c4155c34..b8aa1f297c7 100644 --- a/cli/core/core.go +++ b/cli/core/core.go @@ -28,7 +28,7 @@ func InitCommand() *cobra.Command { Use: "core", Short: "Arduino Core operations.", Long: "Arduino Core operations.", - Example: " " + cli.AppName + " core update-index", + Example: " " + cli.VersionInfo.Application + " core update-index", } coreCommand.AddCommand(initDownloadCommand()) coreCommand.AddCommand(initInstallCommand()) diff --git a/cli/core/download.go b/cli/core/download.go index 2d8ad13fe4c..0808baf6af0 100644 --- a/cli/core/download.go +++ b/cli/core/download.go @@ -35,8 +35,8 @@ func initDownloadCommand() *cobra.Command { Short: "Downloads one or more cores and corresponding tool dependencies.", Long: "Downloads one or more cores and corresponding tool dependencies.", Example: "" + - " " + cli.AppName + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" + - " " + cli.AppName + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).", + " " + cli.VersionInfo.Application + " core download arduino:samd # to download the latest version of arduino SAMD core.\n" + + " " + cli.VersionInfo.Application + " core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).", Args: cobra.MinimumNArgs(1), Run: runDownloadCommand, } @@ -49,12 +49,14 @@ func runDownloadCommand(cmd *cobra.Command, args []string) { platformsRefs := parsePlatformReferenceArgs(args) for i, platformRef := range platformsRefs { - _, err := core.PlatformDownload(context.Background(), &rpc.PlatformDownloadReq{ + platformDownloadreq := &rpc.PlatformDownloadReq{ Instance: instance, PlatformPackage: platformRef.Package, Architecture: platformRef.Architecture, Version: platformRef.Version, - }, cli.OutputProgressBar()) + } + _, err := core.PlatformDownload(context.Background(), platformDownloadreq, cli.OutputProgressBar(), + cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error downloading "+args[i]) os.Exit(cli.ErrNetwork) diff --git a/cli/core/install.go b/cli/core/install.go index 4a06cb8dd0b..0a74d5afc72 100644 --- a/cli/core/install.go +++ b/cli/core/install.go @@ -35,9 +35,9 @@ func initInstallCommand() *cobra.Command { Short: "Installs one or more cores and corresponding tool dependencies.", Long: "Installs one or more cores and corresponding tool dependencies.", Example: " # download the latest version of arduino SAMD core.\n" + - " " + cli.AppName + " core install arduino:samd\n\n" + + " " + cli.VersionInfo.Application + " core install arduino:samd\n\n" + " # download a specific version (in this case 1.6.9).\n" + - " " + cli.AppName + " core install arduino:samd@1.6.9", + " " + cli.VersionInfo.Application + " core install arduino:samd@1.6.9", Args: cobra.MinimumNArgs(1), Run: runInstallCommand, } @@ -51,12 +51,14 @@ func runInstallCommand(cmd *cobra.Command, args []string) { platformsRefs := parsePlatformReferenceArgs(args) for _, platformRef := range platformsRefs { - _, err := core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{ + plattformInstallReq := &rpc.PlatformInstallReq{ Instance: instance, PlatformPackage: platformRef.Package, Architecture: platformRef.Architecture, Version: platformRef.Version, - }, cli.OutputProgressBar(), cli.OutputTaskProgress()) + } + _, err := core.PlatformInstall(context.Background(), plattformInstallReq, cli.OutputProgressBar(), + cli.OutputTaskProgress(), cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error during install") os.Exit(cli.ErrGeneric) diff --git a/cli/core/list.go b/cli/core/list.go index cd56ef41682..91efcd6c4cf 100644 --- a/cli/core/list.go +++ b/cli/core/list.go @@ -37,7 +37,7 @@ func initListCommand() *cobra.Command { Use: "list", Short: "Shows the list of installed platforms.", Long: "Shows the list of installed platforms.", - Example: " " + cli.AppName + " core list", + Example: " " + cli.VersionInfo.Application + " core list", Args: cobra.NoArgs, Run: runListCommand, } diff --git a/cli/core/search.go b/cli/core/search.go index 432b75be97b..0f752189357 100644 --- a/cli/core/search.go +++ b/cli/core/search.go @@ -38,7 +38,7 @@ func initSearchCommand() *cobra.Command { Use: "search ", Short: "Search for a core in the package index.", Long: "Search for a core in the package index using the specified keywords.", - Example: " " + cli.AppName + " core search MKRZero -v", + Example: " " + cli.VersionInfo.Application + " core search MKRZero -v", Args: cobra.MinimumNArgs(1), Run: runSearchCommand, } diff --git a/cli/core/uninstall.go b/cli/core/uninstall.go index e593a7559dd..90e0472731d 100644 --- a/cli/core/uninstall.go +++ b/cli/core/uninstall.go @@ -35,7 +35,7 @@ func initUninstallCommand() *cobra.Command { Use: "uninstall PACKAGER:ARCH ...", Short: "Uninstalls one or more cores and corresponding tool dependencies if no more used.", Long: "Uninstalls one or more cores and corresponding tool dependencies if no more used.", - Example: " " + cli.AppName + " core uninstall arduino:samd\n", + Example: " " + cli.VersionInfo.Application + " core uninstall arduino:samd\n", Args: cobra.MinimumNArgs(1), Run: runUninstallCommand, } diff --git a/cli/core/update_index.go b/cli/core/update_index.go index 90dadd596b5..13d832343f1 100644 --- a/cli/core/update_index.go +++ b/cli/core/update_index.go @@ -34,7 +34,7 @@ func initUpdateIndexCommand() *cobra.Command { Use: "update-index", Short: "Updates the index of cores.", Long: "Updates the index of cores to the latest version.", - Example: " " + cli.AppName + " core update-index", + Example: " " + cli.VersionInfo.Application + " core update-index", Args: cobra.NoArgs, Run: runUpdateIndexCommand, } diff --git a/cli/core/upgrade.go b/cli/core/upgrade.go index 6c061700818..cbf74215ffa 100644 --- a/cli/core/upgrade.go +++ b/cli/core/upgrade.go @@ -36,9 +36,9 @@ func initUpgradeCommand() *cobra.Command { Long: "Upgrades one or all installed platforms to the latest version.", Example: "" + " # upgrade everything to the latest version\n" + - " " + cli.AppName + " core upgrade\n\n" + + " " + cli.VersionInfo.Application + " core upgrade\n\n" + " # upgrade arduino:samd to the latest version\n" + - " " + cli.AppName + " core upgrade arduino:samd", + " " + cli.VersionInfo.Application + " core upgrade arduino:samd", Run: runUpgradeCommand, } return upgradeCommand @@ -60,7 +60,8 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) { Instance: instance, PlatformPackage: platformRef.Package, Architecture: platformRef.Architecture, - }, cli.OutputProgressBar(), cli.OutputTaskProgress()) + }, cli.OutputProgressBar(), cli.OutputTaskProgress(), + cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error during upgrade") os.Exit(cli.ErrGeneric) diff --git a/cli/daemon/daemon.go b/cli/daemon/daemon.go index 5c5dc656e6a..69452e8503f 100644 --- a/cli/daemon/daemon.go +++ b/cli/daemon/daemon.go @@ -21,6 +21,8 @@ import ( "fmt" "log" "net" + "net/http" + "runtime" "github.com/arduino/arduino-cli/cli" "github.com/arduino/arduino-cli/daemon" @@ -35,7 +37,7 @@ func InitCommand() *cobra.Command { Use: "daemon", Short: "Run as a daemon", Long: "Running as a daemon the initialization of cores and libraries is done only once.", - Example: " " + cli.AppName + " daemon", + Example: " " + cli.VersionInfo.Application + " daemon", Args: cobra.NoArgs, Run: runDaemonCommand, Hidden: true, @@ -53,7 +55,13 @@ func runDaemonCommand(cmd *cobra.Command, args []string) { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() - rpc.RegisterArduinoCoreServer(s, &daemon.ArduinoCoreServerImpl{}) + + userAgentValue := fmt.Sprintf("%s/%s daemon (%s; %s; %s) Commit:%s/Build:%s", cli.VersionInfo.Application, + cli.VersionInfo.VersionString, runtime.GOARCH, runtime.GOOS, runtime.Version(), cli.VersionInfo.Commit, cli.VersionInfo.BuildDate) + headers := http.Header{"User-Agent": []string{userAgentValue}} + + coreServer := daemon.ArduinoCoreServerImpl{DownloaderHeaders: headers} + rpc.RegisterArduinoCoreServer(s, &coreServer) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } diff --git a/cli/generatedocs/generatedocs.go b/cli/generatedocs/generatedocs.go index 01982897af4..000537b811f 100644 --- a/cli/generatedocs/generatedocs.go +++ b/cli/generatedocs/generatedocs.go @@ -33,7 +33,7 @@ func InitCommand() *cobra.Command { Use: "generate-docs", Short: "Generates bash completion and command manpages.", Long: "Generates bash completion and command manpages.", - Example: " " + cli.AppName + " generate-docs bash-completions", + Example: " " + cli.VersionInfo.Application + " generate-docs bash-completions", } command.PersistentFlags().StringVarP(&outputDir, "output-dir", "o", "", "Directory where to save generated files. Default is './docs', the directory must exist.") diff --git a/cli/lib/download.go b/cli/lib/download.go index 7598029ce65..3778f32cb94 100644 --- a/cli/lib/download.go +++ b/cli/lib/download.go @@ -21,11 +21,10 @@ import ( "context" "os" - "github.com/arduino/arduino-cli/common/formatter" - "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/cli" "github.com/arduino/arduino-cli/commands/lib" + "github.com/arduino/arduino-cli/common/formatter" "github.com/arduino/arduino-cli/rpc" "github.com/spf13/cobra" ) @@ -36,8 +35,8 @@ func initDownloadCommand() *cobra.Command { Short: "Downloads one or more libraries without installing them.", Long: "Downloads one or more libraries without installing them.", Example: "" + - " " + cli.AppName + " lib download AudioZero # for the latest version.\n" + - " " + cli.AppName + " lib download AudioZero@1.0.0 # for a specific version.", + " " + cli.VersionInfo.Application + " lib download AudioZero # for the latest version.\n" + + " " + cli.VersionInfo.Application + " lib download AudioZero@1.0.0 # for a specific version.", Args: cobra.MinimumNArgs(1), Run: runDownloadCommand, } @@ -52,11 +51,13 @@ func runDownloadCommand(cmd *cobra.Command, args []string) { os.Exit(cli.ErrBadArgument) } for _, library := range pairs { - _, err := lib.LibraryDownload(context.Background(), &rpc.LibraryDownloadReq{ + libraryDownloadReq := &rpc.LibraryDownloadReq{ Instance: instance, Name: library.Name, Version: library.Version.String(), - }, cli.OutputProgressBar()) + } + _, err := lib.LibraryDownload(context.Background(), libraryDownloadReq, cli.OutputProgressBar(), + cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error downloading "+library.String()) os.Exit(cli.ErrNetwork) diff --git a/cli/lib/install.go b/cli/lib/install.go index ccac393ee3a..e534e6e2164 100644 --- a/cli/lib/install.go +++ b/cli/lib/install.go @@ -21,12 +21,11 @@ import ( "context" "os" - "github.com/arduino/arduino-cli/commands/lib" - "github.com/arduino/arduino-cli/rpc" - "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/cli" + "github.com/arduino/arduino-cli/commands/lib" "github.com/arduino/arduino-cli/common/formatter" + "github.com/arduino/arduino-cli/rpc" "github.com/spf13/cobra" ) @@ -36,8 +35,8 @@ func initInstallCommand() *cobra.Command { Short: "Installs one of more specified libraries into the system.", Long: "Installs one or more specified libraries into the system.", Example: "" + - " " + cli.AppName + " lib install AudioZero # for the latest version.\n" + - " " + cli.AppName + " lib install AudioZero@1.0.0 # for the specific version.", + " " + cli.VersionInfo.Application + " lib install AudioZero # for the latest version.\n" + + " " + cli.VersionInfo.Application + " lib install AudioZero@1.0.0 # for the specific version.", Args: cobra.MinimumNArgs(1), Run: runInstallCommand, } @@ -52,11 +51,13 @@ func runInstallCommand(cmd *cobra.Command, args []string) { os.Exit(cli.ErrBadArgument) } for _, library := range refs { - err := lib.LibraryInstall(context.Background(), &rpc.LibraryInstallReq{ + libraryInstallReq := &rpc.LibraryInstallReq{ Instance: instance, Name: library.Name, Version: library.Version.String(), - }, cli.OutputProgressBar(), cli.OutputTaskProgress()) + } + err := lib.LibraryInstall(context.Background(), libraryInstallReq, cli.OutputProgressBar(), + cli.OutputTaskProgress(), cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error installing "+library.String()) os.Exit(cli.ErrGeneric) diff --git a/cli/lib/lib.go b/cli/lib/lib.go index 6a6d41096b6..148ddd1da30 100644 --- a/cli/lib/lib.go +++ b/cli/lib/lib.go @@ -29,8 +29,8 @@ func InitCommand() *cobra.Command { Short: "Arduino commands about libraries.", Long: "Arduino commands about libraries.", Example: "" + - " " + cli.AppName + " lib install AudioZero\n" + - " " + cli.AppName + " lib update-index", + " " + cli.VersionInfo.Application + " lib install AudioZero\n" + + " " + cli.VersionInfo.Application + " lib update-index", } libCommand.AddCommand(initDownloadCommand()) libCommand.AddCommand(initInstallCommand()) diff --git a/cli/lib/list.go b/cli/lib/list.go index 4ef1da30679..5766ad19fa8 100644 --- a/cli/lib/list.go +++ b/cli/lib/list.go @@ -36,7 +36,7 @@ func initListCommand() *cobra.Command { Use: "list", Short: "Shows a list of all installed libraries.", Long: "Shows a list of all installed libraries.", - Example: " " + cli.AppName + " lib list", + Example: " " + cli.VersionInfo.Application + " lib list", Args: cobra.NoArgs, Run: runListCommand, } diff --git a/cli/lib/search.go b/cli/lib/search.go index 9f422917576..674f018eb01 100644 --- a/cli/lib/search.go +++ b/cli/lib/search.go @@ -22,7 +22,6 @@ import ( "fmt" "os" "sort" - "strings" "github.com/arduino/arduino-cli/cli" @@ -39,7 +38,7 @@ func initSearchCommand() *cobra.Command { Use: "search [LIBRARY_NAME]", Short: "Searchs for one or more libraries data.", Long: "Search for one or more libraries data (case insensitive search).", - Example: " " + cli.AppName + " lib search audio", + Example: " " + cli.VersionInfo.Application + " lib search audio", Args: cobra.ArbitraryArgs, Run: runSearchCommand, } diff --git a/cli/lib/uninstall.go b/cli/lib/uninstall.go index b790665d56d..6c38a93c9a6 100644 --- a/cli/lib/uninstall.go +++ b/cli/lib/uninstall.go @@ -35,7 +35,7 @@ func initUninstallCommand() *cobra.Command { Use: "uninstall LIBRARY_NAME(S)", Short: "Uninstalls one or more libraries.", Long: "Uninstalls one or more libraries.", - Example: " " + cli.AppName + " lib uninstall AudioZero", + Example: " " + cli.VersionInfo.Application + " lib uninstall AudioZero", Args: cobra.MinimumNArgs(1), Run: runUninstallCommand, } diff --git a/cli/lib/update_index.go b/cli/lib/update_index.go index 74198f5878f..6c7f22a3b62 100644 --- a/cli/lib/update_index.go +++ b/cli/lib/update_index.go @@ -33,7 +33,7 @@ func initUpdateIndexCommand() *cobra.Command { Use: "update-index", Short: "Updates the libraries index.", Long: "Updates the libraries index to the latest version.", - Example: " " + cli.AppName + " lib update-index", + Example: " " + cli.VersionInfo.Application + " lib update-index", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { instance := cli.CreateInstaceIgnorePlatformIndexErrors() diff --git a/cli/lib/upgrade.go b/cli/lib/upgrade.go index 7deecda5acc..95c59310b41 100644 --- a/cli/lib/upgrade.go +++ b/cli/lib/upgrade.go @@ -35,7 +35,7 @@ func initUpgradeCommand() *cobra.Command { Short: "Upgrades installed libraries.", Long: "This command ungrades all installed libraries to the latest available version." + "To upgrade a single library use the 'install' command.", - Example: " " + cli.AppName + " lib upgrade", + Example: " " + cli.VersionInfo.Application + " lib upgrade", Args: cobra.NoArgs, Run: runUpgradeCommand, } @@ -47,7 +47,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) { err := lib.LibraryUpgradeAll(context.Background(), &rpc.LibraryUpgradeAllReq{ Instance: instance, - }, cli.OutputProgressBar(), cli.OutputTaskProgress()) + }, cli.OutputProgressBar(), cli.OutputTaskProgress(), cli.HTTPClientHeader) if err != nil { formatter.PrintError(err, "Error upgrading libraries") os.Exit(cli.ErrGeneric) diff --git a/cli/output.go b/cli/output.go index fdd0f78be27..bcbcf940142 100644 --- a/cli/output.go +++ b/cli/output.go @@ -22,10 +22,9 @@ import ( "fmt" "os" - "github.com/arduino/arduino-cli/output" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/common/formatter" + "github.com/arduino/arduino-cli/output" "github.com/arduino/arduino-cli/rpc" ) diff --git a/cli/root/root.go b/cli/root/root.go index ee6cabcd2bb..894011b57db 100644 --- a/cli/root/root.go +++ b/cli/root/root.go @@ -35,7 +35,7 @@ import ( "github.com/arduino/arduino-cli/cli/version" "github.com/arduino/arduino-cli/common/formatter" "github.com/arduino/arduino-cli/configs" - paths "github.com/arduino/go-paths-helper" + "github.com/arduino/go-paths-helper" "github.com/mattn/go-colorable" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -48,7 +48,7 @@ func Init() *cobra.Command { Use: "arduino-cli", Short: "Arduino CLI.", Long: "Arduino Command Line Interface (arduino-cli).", - Example: " " + cli.AppName + " [flags...]", + Example: " " + cli.VersionInfo.Application + " [flags...]", PersistentPreRun: preRun, } command.PersistentFlags().BoolVar(&cli.GlobalFlags.Debug, "debug", false, "Enables debug output (super verbose, used to debug the CLI).") @@ -91,7 +91,7 @@ func preRun(cmd *cobra.Command, args []string) { } initConfigs() - logrus.Info(cli.AppName + "-" + cli.Version) + logrus.Info(cli.VersionInfo.Application + "-" + cli.VersionInfo.VersionString) logrus.Info("Starting root command preparation (`arduino`)") switch outputFormat { case "text": diff --git a/cli/sketch/new.go b/cli/sketch/new.go index 685aafbfbcf..7ed9f20e739 100644 --- a/cli/sketch/new.go +++ b/cli/sketch/new.go @@ -30,7 +30,7 @@ func initNewCommand() *cobra.Command { Use: "new", Short: "Create a new Sketch", Long: "Create a new Sketch", - Example: " " + cli.AppName + " sketch new MultiBlinker", + Example: " " + cli.VersionInfo.Application + " sketch new MultiBlinker", Args: cobra.ExactArgs(1), Run: runNewCommand, } diff --git a/cli/sketch/sketch.go b/cli/sketch/sketch.go index c9035762ec8..2c735778c52 100644 --- a/cli/sketch/sketch.go +++ b/cli/sketch/sketch.go @@ -28,7 +28,7 @@ func InitCommand() *cobra.Command { Use: "sketch", Short: "Arduino CLI Sketch Commands.", Long: "Arduino CLI Sketch Commands.", - Example: " " + cli.AppName + " sketch new MySketch", + Example: " " + cli.VersionInfo.Application + " sketch new MySketch", } sketchCommand.AddCommand(initNewCommand()) //sketchCommand.AddCommand(initSyncCommand()) diff --git a/cli/upload/upload.go b/cli/upload/upload.go index d27bf3a441f..23f264dab70 100644 --- a/cli/upload/upload.go +++ b/cli/upload/upload.go @@ -35,7 +35,7 @@ func InitCommand() *cobra.Command { Use: "upload", Short: "Upload Arduino sketches.", Long: "Upload Arduino sketches.", - Example: " " + cli.AppName + " upload /home/user/Arduino/MySketch", + Example: " " + cli.VersionInfo.Application + " upload /home/user/Arduino/MySketch", Args: cobra.MaximumNArgs(1), Run: run, } diff --git a/cli/version/version.go b/cli/version/version.go index b17afce7880..824460d38cc 100644 --- a/cli/version/version.go +++ b/cli/version/version.go @@ -30,7 +30,7 @@ func InitCommand() *cobra.Command { Use: "version", Short: "Shows version number of arduino CLI.", Long: "Shows version number of arduino CLI which is installed on your system.", - Example: " " + cli.AppName + " version", + Example: " " + cli.VersionInfo.Application + " version", Args: cobra.NoArgs, Run: run, } @@ -38,16 +38,20 @@ func InitCommand() *cobra.Command { } type versionOutput struct { - Command string `json:"command"` - Version string `json:"version"` + Command string `json:"command"` + Version string `json:"version"` + Commit string `json:"commit"` + BuildDate string `json:"build_date"` } func run(cmd *cobra.Command, args []string) { res := &versionOutput{ - Command: cmd.Parent().Name(), - Version: cli.Version, + Command: cmd.Parent().Name(), + Version: cli.VersionInfo.VersionString, + Commit: cli.VersionInfo.Commit, + BuildDate: cli.VersionInfo.BuildDate.String(), } if cli.OutputJSONOrElse(res) { - fmt.Printf("%s version %s\n", res.Command, res.Version) + fmt.Printf("%s\n", cli.VersionInfo) } } diff --git a/commands/bundled_tools.go b/commands/bundled_tools.go index 88cd451d6e5..41b430858f1 100644 --- a/commands/bundled_tools.go +++ b/commands/bundled_tools.go @@ -19,6 +19,7 @@ package commands import ( "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" @@ -26,8 +27,9 @@ import ( ) // DownloadToolRelease downloads a ToolRelease -func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, downloadCB DownloadProgressCB) error { - resp, err := pm.DownloadToolRelease(toolRelease) +func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, + downloadCB DownloadProgressCB, downloaderHeaders http.Header) error { + resp, err := pm.DownloadToolRelease(toolRelease, downloaderHeaders) if err != nil { return err } diff --git a/commands/compile/compile.go b/commands/compile/compile.go index b68e4889fa1..08a0dfb2cb9 100644 --- a/commands/compile/compile.go +++ b/commands/compile/compile.go @@ -76,7 +76,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream io.Writer, errS // TODO: Move this error message in `cli` module // errorMessage := fmt.Sprintf( // "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+ - // cli.AppName+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch) + // version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch) // formatter.PrintErrorMessage(errorMessage) return nil, fmt.Errorf("platform not installed") } diff --git a/commands/core/download.go b/commands/core/download.go index ae07ffabdfc..86e389b20f8 100644 --- a/commands/core/download.go +++ b/commands/core/download.go @@ -21,6 +21,7 @@ import ( "context" "errors" "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" @@ -28,7 +29,8 @@ import ( "github.com/arduino/arduino-cli/rpc" ) -func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResp, error) { +func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloadCB commands.DownloadProgressCB, + downloaderHeaders http.Header) (*rpc.PlatformDownloadResp, error) { pm := commands.GetPackageManager(req) if pm == nil { return nil, errors.New("invalid instance") @@ -48,13 +50,13 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa return nil, fmt.Errorf("find platform dependencies: %s", err) } - err = downloadPlatform(pm, platform, downloadCB) + err = downloadPlatform(pm, platform, downloadCB, downloaderHeaders) if err != nil { return nil, err } for _, tool := range tools { - err := downloadTool(pm, tool, downloadCB) + err := downloadTool(pm, tool, downloadCB, downloaderHeaders) if err != nil { return nil, fmt.Errorf("downloading tool %s: %s", tool, err) } @@ -63,20 +65,22 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, downloa return &rpc.PlatformDownloadResp{}, nil } -func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, downloadCB commands.DownloadProgressCB) error { +func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, + downloadCB commands.DownloadProgressCB, downloaderHeaders http.Header) error { // Download platform - resp, err := pm.DownloadPlatformRelease(platformRelease) + resp, err := pm.DownloadPlatformRelease(platformRelease, downloaderHeaders) if err != nil { return err } return commands.Download(resp, platformRelease.String(), downloadCB) } -func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB) error { +func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, downloadCB commands.DownloadProgressCB, + downloaderHeaders http.Header) error { // Check if tool has a flavor available for the current OS if tool.GetCompatibleFlavour() == nil { return fmt.Errorf("tool %s not available for the current OS", tool) } - return commands.DownloadToolRelease(pm, tool, downloadCB) + return commands.DownloadToolRelease(pm, tool, downloadCB, downloaderHeaders) } diff --git a/commands/core/install.go b/commands/core/install.go index 4963172a7cc..d418aafa3ec 100644 --- a/commands/core/install.go +++ b/commands/core/install.go @@ -21,6 +21,7 @@ import ( "context" "errors" "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/cores" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" @@ -29,7 +30,7 @@ import ( ) func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResp, error) { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) (*rpc.PlatformInstallResp, error) { pm := commands.GetPackageManager(req) if pm == nil { @@ -50,7 +51,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, return nil, fmt.Errorf("finding platform dependencies: %s", err) } - err = installPlatform(pm, platform, tools, downloadCB, taskCB) + err = installPlatform(pm, platform, tools, downloadCB, taskCB, downloaderHeaders) if err != nil { return nil, err } @@ -65,7 +66,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error { log := pm.Log.WithField("platform", platformRelease) // Prerequisite checks before install @@ -87,9 +88,9 @@ func installPlatform(pm *packagemanager.PackageManager, // Package download taskCB(&rpc.TaskProgress{Name: "Downloading packages"}) for _, tool := range toolsToInstall { - downloadTool(pm, tool, downloadCB) + downloadTool(pm, tool, downloadCB, downloaderHeaders) } - downloadPlatform(pm, platformRelease, downloadCB) + downloadPlatform(pm, platformRelease, downloadCB, downloaderHeaders) taskCB(&rpc.TaskProgress{Completed: true}) // Install tools first diff --git a/commands/core/upgrade.go b/commands/core/upgrade.go index 12e3715290c..6099ccfc59e 100644 --- a/commands/core/upgrade.go +++ b/commands/core/upgrade.go @@ -21,6 +21,7 @@ import ( "context" "errors" "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" @@ -28,7 +29,7 @@ import ( ) func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformUpgradeResp, error) { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) (*rpc.PlatformUpgradeResp, error) { pm := commands.GetPackageManager(req) if pm == nil { @@ -40,7 +41,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, Package: req.PlatformPackage, PlatformArchitecture: req.Architecture, } - if err := upgradePlatform(pm, ref, downloadCB, taskCB); err != nil { + if err := upgradePlatform(pm, ref, downloadCB, taskCB, downloaderHeaders); err != nil { return nil, err } @@ -52,7 +53,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, } func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error { if platformRef.PlatformVersion != nil { return fmt.Errorf("upgrade doesn't accept parameters with version") } @@ -79,7 +80,7 @@ func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemana if err != nil { return fmt.Errorf("platform %s is not installed", platformRef) } - err = installPlatform(pm, platform, tools, downloadCB, taskCB) + err = installPlatform(pm, platform, tools, downloadCB, taskCB, downloaderHeaders) if err != nil { return err } diff --git a/commands/instances.go b/commands/instances.go index 21915337b2c..e26852e1ac3 100644 --- a/commands/instances.go +++ b/commands/instances.go @@ -21,6 +21,7 @@ import ( "context" "fmt" "io/ioutil" + "net/http" "net/url" "path" "time" @@ -82,12 +83,13 @@ func GetDiscoveries(req InstanceContainer) []*discovery.Discovery { return i.discoveries } -func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, downloadCB DownloadProgressCB, taskCB TaskProgressCB) (bool, error) { +func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, downloadCB DownloadProgressCB, + taskCB TaskProgressCB, downloaderHeaders http.Header) (bool, error) { if tool.IsInstalled() { return false, nil } taskCB(&rpc.TaskProgress{Name: "Downloading missing tool " + tool.String()}) - if err := DownloadToolRelease(instance.pm, tool, downloadCB); err != nil { + if err := DownloadToolRelease(instance.pm, tool, downloadCB, downloaderHeaders); err != nil { return false, fmt.Errorf("downloading %s tool: %s", tool, err) } taskCB(&rpc.TaskProgress{Completed: true}) @@ -97,17 +99,18 @@ func (instance *CoreInstance) installToolIfMissing(tool *cores.ToolRelease, down return true, nil } -func (instance *CoreInstance) checkForBuiltinTools(downloadCB DownloadProgressCB, taskCB TaskProgressCB) error { +func (instance *CoreInstance) checkForBuiltinTools(downloadCB DownloadProgressCB, taskCB TaskProgressCB, + downloaderHeaders http.Header) error { // Check for ctags tool ctags, _ := getBuiltinCtagsTool(instance.pm) - ctagsInstalled, err := instance.installToolIfMissing(ctags, downloadCB, taskCB) + ctagsInstalled, err := instance.installToolIfMissing(ctags, downloadCB, taskCB, downloaderHeaders) if err != nil { return err } // Check for bultin serial-discovery tool serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(instance.pm) - serialDiscoveryInstalled, err := instance.installToolIfMissing(serialDiscoveryTool, downloadCB, taskCB) + serialDiscoveryInstalled, err := instance.installToolIfMissing(serialDiscoveryTool, downloadCB, taskCB, downloaderHeaders) if err != nil { return err } @@ -146,7 +149,7 @@ func (instance *CoreInstance) startDiscoveries() error { return nil } -func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB) (*rpc.InitResp, error) { +func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, taskCB TaskProgressCB, downloaderHeaders http.Header) (*rpc.InitResp, error) { inConfig := req.GetConfiguration() if inConfig == nil { return nil, fmt.Errorf("invalid request") @@ -182,7 +185,7 @@ func Init(ctx context.Context, req *rpc.InitReq, downloadCB DownloadProgressCB, instancesCount++ instances[handle] = instance - if err := instance.checkForBuiltinTools(downloadCB, taskCB); err != nil { + if err := instance.checkForBuiltinTools(downloadCB, taskCB, downloaderHeaders); err != nil { fmt.Println(err) return nil, err } diff --git a/commands/lib/download.go b/commands/lib/download.go index 96310d6d142..a46bddd4c85 100644 --- a/commands/lib/download.go +++ b/commands/lib/download.go @@ -20,6 +20,7 @@ package lib import ( "context" "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" @@ -28,7 +29,8 @@ import ( "github.com/sirupsen/logrus" ) -func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadCB commands.DownloadProgressCB) (*rpc.LibraryDownloadResp, error) { +func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadCB commands.DownloadProgressCB, + downloaderHeaders http.Header) (*rpc.LibraryDownloadResp, error) { logrus.Info("Executing `arduino lib download`") lm := commands.GetLibraryManager(req) @@ -40,7 +42,7 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadC return nil, fmt.Errorf("looking for library: %s", err) } - if err := downloadLibrary(lm, lib, downloadCB, func(*rpc.TaskProgress) {}); err != nil { + if err := downloadLibrary(lm, lib, downloadCB, func(*rpc.TaskProgress) {}, downloaderHeaders); err != nil { return nil, err } @@ -48,10 +50,10 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadReq, downloadC } func downloadLibrary(lm *librariesmanager.LibrariesManager, libRelease *librariesindex.Release, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error { taskCB(&rpc.TaskProgress{Name: "Downloading " + libRelease.String()}) - if d, err := libRelease.Resource.Download(lm.DownloadsDir); err != nil { + if d, err := libRelease.Resource.Download(lm.DownloadsDir, downloaderHeaders); err != nil { return err } else if err := commands.Download(d, libRelease.String(), downloadCB); err != nil { return err diff --git a/commands/lib/install.go b/commands/lib/install.go index c39d4fb2087..dac7d74f3eb 100644 --- a/commands/lib/install.go +++ b/commands/lib/install.go @@ -20,6 +20,7 @@ package lib import ( "context" "fmt" + "net/http" "github.com/arduino/arduino-cli/arduino/libraries/librariesindex" "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" @@ -29,7 +30,7 @@ import ( ) func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq, - downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { + downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error { lm := commands.GetLibraryManager(req) @@ -38,7 +39,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallReq, return fmt.Errorf("looking for library: %s", err) } - if err := downloadLibrary(lm, libRelease, downloadCB, taskCB); err != nil { + if err := downloadLibrary(lm, libRelease, downloadCB, taskCB, downloaderHeaders); err != nil { return fmt.Errorf("downloading library: %s", err) } diff --git a/commands/lib/upgrade.go b/commands/lib/upgrade.go index 1181f32a5fe..ae0e4be1cd5 100644 --- a/commands/lib/upgrade.go +++ b/commands/lib/upgrade.go @@ -20,19 +20,21 @@ package lib import ( "context" "fmt" + "net/http" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/rpc" ) -func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) error { +func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downloadCB commands.DownloadProgressCB, + taskCB commands.TaskProgressCB, downloaderHeaders http.Header) error { lm := commands.GetLibraryManager(req) // Obtain the list of upgradable libraries list := listLibraries(lm, true, true) for _, upgradeDesc := range list { - if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB); err != nil { + if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB, downloaderHeaders); err != nil { return err } } diff --git a/daemon/daemon.go b/daemon/daemon.go index 52a7bfdfa9a..db9e8efdac0 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -23,8 +23,7 @@ import ( "context" "fmt" "io" - "log" - "net" + "net/http" "github.com/arduino/arduino-cli/cli" "github.com/arduino/arduino-cli/commands" @@ -34,29 +33,12 @@ import ( "github.com/arduino/arduino-cli/commands/lib" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/rpc" - "github.com/spf13/cobra" - "google.golang.org/grpc" ) -const ( - port = ":50051" -) - -func runDaemonCommand(cmd *cobra.Command, args []string) { - lis, err := net.Listen("tcp", port) - if err != nil { - log.Fatalf("failed to listen: %v", err) - } - s := grpc.NewServer() - rpc.RegisterArduinoCoreServer(s, &ArduinoCoreServerImpl{}) - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } - fmt.Println("Done serving") +type ArduinoCoreServerImpl struct { + DownloaderHeaders http.Header } -type ArduinoCoreServerImpl struct{} - func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.BoardDetailsReq) (*rpc.BoardDetailsResp, error) { return board.Details(ctx, req) } @@ -112,6 +94,7 @@ func (s *ArduinoCoreServerImpl) Init(req *rpc.InitReq, stream rpc.ArduinoCore_In resp, err := commands.Init(stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.InitResp{DownloadProgress: p}) }, func(p *rpc.TaskProgress) { stream.Send(&rpc.InitResp{TaskProgress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -121,7 +104,7 @@ func (s *ArduinoCoreServerImpl) Init(req *rpc.InitReq, stream rpc.ArduinoCore_In } func (s *ArduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq) (*rpc.VersionResp, error) { - return &rpc.VersionResp{Version: cli.Version}, nil + return &rpc.VersionResp{Version: cli.VersionInfo.VersionString}, nil } func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoCore_CompileServer) error { @@ -141,6 +124,7 @@ func (s *ArduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallReq, str stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.PlatformInstallResp{Progress: p}) }, func(p *rpc.TaskProgress) { stream.Send(&rpc.PlatformInstallResp{TaskProgress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -152,6 +136,7 @@ func (s *ArduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadReq, s resp, err := core.PlatformDownload( stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.PlatformDownloadResp{Progress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -175,6 +160,7 @@ func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeReq, str stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.PlatformUpgradeResp{Progress: p}) }, func(p *rpc.TaskProgress) { stream.Send(&rpc.PlatformUpgradeResp{TaskProgress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -221,6 +207,7 @@ func (s *ArduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadReq, str resp, err := lib.LibraryDownload( stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryDownloadResp{Progress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -233,6 +220,7 @@ func (s *ArduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallReq, strea stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryInstallResp{Progress: p}) }, func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryInstallResp{TaskProgress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err @@ -254,6 +242,7 @@ func (s *ArduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllReq, err := lib.LibraryUpgradeAll(stream.Context(), req, func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{Progress: p}) }, func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeAllResp{TaskProgress: p}) }, + s.DownloaderHeaders, ) if err != nil { return err diff --git a/go.mod b/go.mod index ad88c2c7426..963669b19cf 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,12 @@ module github.com/arduino/arduino-cli go 1.12 require ( - bou.ke/monkey v1.0.1 // indirect + bou.ke/monkey v1.0.1 github.com/arduino/board-discovery v0.0.0-20180823133458-1ba29327fb0c github.com/arduino/go-paths-helper v0.0.0-20190214132331-c3c98d1bf2e1 github.com/arduino/go-properties-orderedmap v0.0.0-20181003091528-89278049acd3 github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b - github.com/bouk/monkey v1.0.1 github.com/cmaglie/pb v1.0.27 github.com/codeclysm/cc v1.2.2 // indirect github.com/codeclysm/extract v2.2.0+incompatible @@ -44,7 +43,8 @@ require ( go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 golang.org/x/net v0.0.0-20190311183353-d8887717615a - golang.org/x/text v0.3.2 + golang.org/x/text v0.3.0 + google.golang.org/appengine v1.4.0 // indirect google.golang.org/genproto v0.0.0-20190327125643-d831d65fe17d // indirect google.golang.org/grpc v1.21.1 gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect diff --git a/go.sum b/go.sum index b0bc99981bf..ba5493edcea 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,6 @@ github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwG github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b h1:3PjgYG5gVPA7cipp7vIR2lF96KkEJIFBJ+ANnuv6J20= github.com/arduino/go-win32-utils v0.0.0-20180330194947-ed041402e83b/go.mod h1:iIPnclBMYm1g32Q5kXoqng4jLhMStReIP7ZxaoUC2y8= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/bouk/monkey v1.0.1 h1:82kWEtyEjyfkRZb0DaQ5+7O5dJfe3GzF/o97+yUo5d0= -github.com/bouk/monkey v1.0.1/go.mod h1:PG/63f4XEUlVyW1ttIeOJmJhhe1+t9EC/je3eTjvFhE= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cmaglie/pb v1.0.27 h1:ynGj8vBXR+dtj4B7Q/W/qGt31771Ux5iFfRQBnwdQiA= github.com/cmaglie/pb v1.0.27/go.mod h1:GilkKZMXYjBA4NxItWFfO+lwkp59PLHQ+IOW/b/kmZI= @@ -133,7 +131,6 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -141,20 +138,14 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522 h1:Ve1ORMCxvRmSXBwJK+t3Oy+V2vRW2OetUQBq4rJIkZE= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000000..c59cc26c398 --- /dev/null +++ b/version/version.go @@ -0,0 +1,58 @@ +/* + * This file is part of arduino-cli. + * + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) + * + * This software is released under the GNU General Public License defaultVersionString 3, + * which covers the main part of arduino-cli. + * The terms of this license can be found at: + * https://www.gnu.org/licenses/gpl-3.0.en.html + * + * You can be released from the requirements of the above licenses by purchasing + * a commercial license. Buying such a license is mandatory if you want to modify or + * otherwise use the software for commercial activities involving the Arduino + * software without disclosing the source code of your own applications. To purchase + * a commercial license, send an email to license@arduino.cc. + */ + +package version + +import ( + "fmt" + "time" +) + +var ( + defaultVersionString = "0.3.6-alpha.preview" + versionString = "" + commit = "" + buildDate = time.Time{} +) + +type Info struct { + Application string `json:"Application"` + VersionString string `json:"VersionString"` + Commit string `json:"Commit"` + BuildDate time.Time `json:"BuildDate"` +} + +func NewInfo(application string) *Info { + return &Info{ + Application: application, + VersionString: versionString, + Commit: commit, + BuildDate: buildDate, + } +} + +func (i *Info) String() string { + return fmt.Sprintf("%s Version: %s Commit: %s BuildDate: %s", i.Application, i.VersionString, i.Commit, i.BuildDate) +} + +//nolint:gochecknoinits +func init() { + if versionString == "" { + versionString = defaultVersionString + } + buildDate = time.Now().UTC() +} diff --git a/version/version_test.go b/version/version_test.go new file mode 100644 index 00000000000..118174fb214 --- /dev/null +++ b/version/version_test.go @@ -0,0 +1,49 @@ +/* + * This file is part of arduino-cli. + * + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) + * + * This software is released under the GNU General Public License defaultVersionString 3, + * which covers the main part of arduino-cli. + * The terms of this license can be found at: + * https://www.gnu.org/licenses/gpl-3.0.en.html + * + * You can be released from the requirements of the above licenses by purchasing + * a commercial license. Buying such a license is mandatory if you want to modify or + * otherwise use the software for commercial activities involving the Arduino + * software without disclosing the source code of your own applications. To purchase + * a commercial license, send an email to license@arduino.cc. + */ + +package version + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +// TestIntegrationBuildInjectedInfo is an integration test that aims to test the Info strings passed to the binary at build time +// in order to have this test green launch your testing using the provided task (see /Taskfile.yml) or use: +// go test -run Integration -v ./... -ldflags ' +// -X github.com/arduino/arduino-cli/version.versionString=0.0.0-test.preview +// -X github.com/arduino/arduino-cli/version.commit=deadbeef' +func TestIntegrationBuildInjectedInfo(t *testing.T) { + if testing.Short() { + t.Skip("skip integration test") + } + goldenAppName := "arduino-cli" + goldenInfo := Info{ + Application: goldenAppName, + VersionString: "0.0.0-test.preview", + Commit: "deadbeef", + BuildDate: time.Time{}, + } + info := NewInfo(goldenAppName) + require.Equal(t, goldenInfo.Application, info.Application) + require.Equal(t, goldenInfo.VersionString, info.VersionString) + require.Equal(t, goldenInfo.Commit, info.Commit) + require.IsType(t, time.Time{}, info.BuildDate) + require.False(t, info.BuildDate.IsZero()) +}