-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.go
168 lines (150 loc) · 5.11 KB
/
init.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// This file is part of arduino-cloud-cli.
//
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package credentials
import (
"errors"
"fmt"
"os"
"strings"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cloud-cli/arduino"
"github.com/arduino/arduino-cloud-cli/config"
"github.com/arduino/go-paths-helper"
"github.com/manifoldco/promptui"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type initFlags struct {
destDir string
overwrite bool
format string
}
func initInitCommand() *cobra.Command {
flags := &initFlags{}
initCommand := &cobra.Command{
Use: "init",
Short: "Initialize a credentials file",
Long: "Initialize an Arduino IoT Cloud CLI credentials file",
Run: func(cmd *cobra.Command, args []string) {
if err := runInitCommand(flags); err != nil {
feedback.Errorf("Error during credentials init: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
},
}
initCommand.Flags().StringVar(&flags.destDir, "dest-dir", "", "Sets where to save the credentials file")
initCommand.Flags().BoolVar(&flags.overwrite, "overwrite", false, "Overwrite existing credentials file")
initCommand.Flags().StringVar(&flags.format, "file-format", "yaml", "Format of the credentials file, can be {yaml|json}")
return initCommand
}
func runInitCommand(flags *initFlags) error {
logrus.Info("Initializing credentials file")
// Get default destination directory if it's not passed
if flags.destDir == "" {
credPath, err := arduino.DataDir()
if err != nil {
return fmt.Errorf("cannot retrieve arduino default directory: %w", err)
}
// Create arduino default directory if it does not exist
if credPath.NotExist() {
if err = credPath.MkdirAll(); err != nil {
return fmt.Errorf("cannot create arduino default directory %s: %w", credPath, err)
}
}
flags.destDir = credPath.String()
}
// Validate format flag
flags.format = strings.ToLower(flags.format)
if flags.format != "json" && flags.format != "yaml" {
return fmt.Errorf("format is not valid, provide 'json' or 'yaml'")
}
// Check that the destination directory is valid and build the credentials file path
credPath, err := paths.New(flags.destDir).Abs()
if err != nil {
return fmt.Errorf("cannot retrieve absolute path of %s: %w", flags.destDir, err)
}
if !credPath.IsDir() {
return fmt.Errorf("%s is not a valid directory", credPath)
}
credFile := credPath.Join(config.CredentialsFilename + "." + flags.format)
if !flags.overwrite && credFile.Exist() {
return fmt.Errorf("%s already exists, use '--overwrite' to overwrite it", credFile)
}
// Take needed credentials starting an interactive mode
feedback.Print("To obtain your API credentials visit https://app.arduino.cc/api-keys")
id, key, org, err := paramsPrompt()
if err != nil {
return fmt.Errorf("cannot take credentials params: %w", err)
}
// Write the credentials file
newSettings := viper.New()
newSettings.SetConfigPermissions(os.FileMode(0600))
newSettings.Set("client", id)
newSettings.Set("secret", key)
newSettings.Set("organization", org)
if err := newSettings.WriteConfigAs(credFile.String()); err != nil {
return fmt.Errorf("cannot write credentials file: %w", err)
}
feedback.Printf("Credentials file successfully initialized at: %s", credFile)
return nil
}
func paramsPrompt() (id, key, org string, err error) {
prompt := promptui.Prompt{
Label: "Please enter the Client ID",
Validate: func(s string) error {
if len(s) != config.ClientIDLen {
return errors.New("client-id not valid")
}
return nil
},
}
id, err = prompt.Run()
if err != nil {
return "", "", "", fmt.Errorf("client prompt fail: %w", err)
}
prompt = promptui.Prompt{
Mask: '*',
Label: "Please enter the Client Secret",
Validate: func(s string) error {
if len(s) != config.ClientSecretLen {
return errors.New("client secret not valid")
}
return nil
},
}
key, err = prompt.Run()
if err != nil {
return "", "", "", fmt.Errorf("client secret prompt fail: %w", err)
}
prompt = promptui.Prompt{
Mask: '*',
Label: "Please enter the Organization ID - if any - Leave empty otherwise",
Validate: func(s string) error {
if len(s) != 0 && len(s) != config.OrganizationLen {
return errors.New("organization id not valid")
}
return nil
},
}
org, err = prompt.Run()
if err != nil {
return "", "", "", fmt.Errorf("organization id prompt fail: %w", err)
}
return id, key, org, nil
}