-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
main.go
95 lines (83 loc) · 3.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// This file is part of arduino-cli.
//
// Copyright 2020 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 [email protected].
package main
import (
"context"
"fmt"
"io"
"os"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli"
"github.com/arduino/arduino-cli/internal/cli/config"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/i18n"
"github.com/arduino/arduino-cli/internal/locales"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func main() {
// Disable logging until it is setup in the arduino-cli pre-run
logrus.SetOutput(io.Discard)
// Search for the configuration file in the command line arguments and in the environment
configFile := configuration.FindConfigFlagsInArgsOrFallbackOnEnv(os.Args)
ctx := config.SetConfigFile(context.Background(), configFile)
// Create a new ArduinoCoreServer
srv := commands.NewArduinoCoreServer()
// Read the settings from the configuration file
openReq := &rpc.ConfigurationOpenRequest{SettingsFormat: "yaml"}
var configFileLoadingWarnings []string
if configData, err := paths.New(configFile).ReadFile(); err == nil {
openReq.EncodedSettings = string(configData)
} else if !os.IsNotExist(err) {
feedback.FatalError(fmt.Errorf("couldn't read configuration file: %w", err), feedback.ErrGeneric)
}
if resp, err := srv.ConfigurationOpen(ctx, openReq); err != nil {
feedback.FatalError(fmt.Errorf("couldn't load configuration: %w", err), feedback.ErrGeneric)
} else if warnings := resp.GetWarnings(); len(warnings) > 0 {
// Save the warnings to show them later when the feedback package is fully initialized
configFileLoadingWarnings = warnings
}
// Get the current settings from the server
resp, err := srv.ConfigurationGet(ctx, &rpc.ConfigurationGetRequest{})
if err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
config := resp.GetConfiguration()
// Setup i18n
locales.Init(config.GetLocale())
// Setup command line parser with the server and settings
arduinoCmd := cli.NewCommand(srv)
parentPreRun := arduinoCmd.PersistentPreRun
arduinoCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if parentPreRun != nil {
parentPreRun(cmd, args)
}
// In Text mode print the warnings about the configuration file
// only if we are inside the "config ..." command. In JSON mode always
// output the warning.
if feedback.GetFormat() != feedback.Text || (cmd.HasParent() && cmd.Parent().Name() == "config") {
for _, warning := range configFileLoadingWarnings {
feedback.Warning(fmt.Sprintf("%s: %s", i18n.Tr("Invalid value in configuration"), warning))
}
}
}
// Execute the command line
if err := arduinoCmd.ExecuteContext(ctx); err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
}
}